Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/advisories/ProducerAdvisor.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.activemq.advisories;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import javax.jms.Connection;
26  import javax.jms.Destination;
27  import javax.jms.JMSException;
28  import javax.jms.Message;
29  import javax.jms.MessageConsumer;
30  import javax.jms.MessageListener;
31  import javax.jms.ObjectMessage;
32  import javax.jms.Session;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.activemq.message.ActiveMQDestination;
36  import org.activemq.message.ProducerInfo;
37  import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
38  import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
39  import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
40  import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
41  /**
42   * A helper class for listening for MessageProducer advisories
43   * 
44   * * @version $Revision: 1.1.1.1 $
45   */
46  
47  public class ProducerAdvisor implements MessageListener {
48      private static final Log log = LogFactory.getLog(ProducerAdvisor.class);
49      private Connection connection;
50      private ActiveMQDestination destination;
51      private Session session;
52      private List listeners = new CopyOnWriteArrayList();
53      private SynchronizedBoolean started = new SynchronizedBoolean(false);
54      private Map activeProducers = new ConcurrentHashMap();
55      
56      /**
57       * Construct a ProducerAdvisor
58       * @param connection
59       * @param destination the destination to listen for Producer events
60       * @throws JMSException
61       */
62      public ProducerAdvisor(Connection connection, Destination destination) throws JMSException{
63          this.connection = connection;
64          this.destination = ActiveMQDestination.transformDestination(destination);
65      }
66      
67      /**
68       * start listening for advisories
69       * @throws JMSException
70       *
71       */
72      public void start() throws JMSException {
73          if (started.commit(false, true)) {
74              session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
75              MessageConsumer consumer = session.createConsumer(destination.getTopicForProducerAdvisory());
76              consumer.setMessageListener(this);
77          }
78      }
79      
80      /**
81       * stop listening for advisories
82       * @throws JMSException
83       */
84      public void stop() throws JMSException{
85          if (started.commit(true,false)){
86              if (session != null){
87                  session.close();
88              }
89          }
90      }
91      
92      /**
93       * Add a listener
94       * @param l
95       */
96      public void addListener(ProducerAdvisoryEventListener l){
97          listeners.add(l);
98      }
99      
100     /**
101      * Remove a listener
102      * @param l
103      */
104     public void removeListener(ProducerAdvisoryEventListener l){
105         listeners.remove(l);
106     }
107     
108     
109     /**
110      * returns true if there is an active producer for the destination
111      * 
112      * @param destination
113      * @return true if a producer for the destination
114      */
115     public boolean isActive(Destination destination) {
116         return activeProducers.containsKey(destination);
117     }
118 
119     /**
120      * return a set of active ProducerInfo's for a particular destination
121      * @param destination
122      * @return the set of ProducerInfo objects currently active
123      */
124     public Set activeProducers(Destination destination) {
125         Set set = (Set) activeProducers.get(destination);
126         return set != null ? set : new CopyOnWriteArraySet();
127     }
128 
129     
130     
131     
132     /**
133      * OnMessage() implementation
134      * @param msg
135      */
136     public void onMessage(Message msg){
137         if (msg instanceof ObjectMessage){
138             try {
139                 ProducerInfo info = (ProducerInfo)((ObjectMessage)msg).getObject();
140                 updateActiveProducers(info);
141                 ProducerAdvisoryEvent event = new ProducerAdvisoryEvent(info);
142                 fireEvent(event);
143             }
144             catch (JMSException e) {
145                 log.error("Failed to process message: " + msg);
146             }
147         }
148     }
149     
150     private void fireEvent(ProducerAdvisoryEvent event){
151         for (Iterator i = listeners.iterator(); i.hasNext(); ){
152             ProducerAdvisoryEventListener l = (ProducerAdvisoryEventListener)i.next();
153             l.onEvent(event);
154         }
155     }
156     
157     private void updateActiveProducers(ProducerInfo info) {
158         Set set = (Set) activeProducers.get(info.getDestination());
159         if (info.isStarted()) {
160             if (set == null) {
161                 set = new CopyOnWriteArraySet();
162                 activeProducers.put(info.getDestination(), set);
163             }
164             set.add(info);
165         }
166         else {
167             if (set != null) {
168                 set.remove(info);
169                 if (set.isEmpty()) {
170                     activeProducers.remove(set);
171                 }
172             }
173         }
174     }
175 
176 
177 }