KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > blocks > ScalableDispatcher


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat .
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.blocks;
47
48 import java.io.Serializable JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import javax.jms.JMSException JavaDoc;
54 import javax.jms.Message JavaDoc;
55 import javax.jms.MessageConsumer JavaDoc;
56 import javax.jms.MessageListener JavaDoc;
57 import javax.jms.MessageProducer JavaDoc;
58 import javax.jms.ObjectMessage JavaDoc;
59 import javax.jms.Session JavaDoc;
60 import javax.jms.TopicConnection JavaDoc;
61 import javax.jms.TopicConnectionFactory JavaDoc;
62 import javax.jms.TopicSession JavaDoc;
63
64 import org.apache.commons.logging.LogFactory;
65 import org.mr.api.jms.MantaTopicConnectionFactory;
66 import org.mr.core.util.Stage;
67 import org.mr.core.util.StageHandler;
68 import org.mr.core.util.StageParams;
69
70 /**
71  * This class serves as a one-to-many module decuple mediator, modules dispatch events while
72  * others handle these events, a dispatched even will be handled by all registered handlers
73  * @author Amir Shevat
74  */

75 public class ScalableDispatcher implements StageHandler, MessageListener JavaDoc{
76     /**
77      * if true this Dispatcher will use an underlying JMS topic to transfer messages
78      * else a Stage will be used
79      */

80     private boolean distributed;
81     /**
82      * the underlying structure in case distributed is false
83      */

84     private Stage stage = null;
85     /**
86      * a set of JMS objects that will be used in case distributed is true
87      */

88     private TopicSession JavaDoc sendSession = null;
89     private TopicSession JavaDoc receiveSession = null;
90     private MessageConsumer JavaDoc consumer = null;
91     private MessageProducer JavaDoc producer = null;
92     /**
93      * the list of all the handlers of this dispatcher
94      */

95     private List JavaDoc handlers = new ArrayList JavaDoc();
96     /**
97      * a sync object for getting lock on handlers
98      */

99     private Object JavaDoc handlerSync = new Object JavaDoc();
100     /**
101      * the name of the dispatcher and the name of the JMS topic if distributed is true
102      */

103     private String JavaDoc dispatcherName;
104
105     /**
106      * instance of this object should be instance with the factory method getDispatcher(..) in ScalableFactory
107      * @param name the name of the Dispatcher
108      * @param distributed if true use JMS if false use in memory Stage
109      */

110     ScalableDispatcher(String JavaDoc name,boolean distributed) {
111         this.distributed = distributed;
112         dispatcherName = name;
113         if(!distributed){
114             StageParams params = new StageParams();
115             params.setBlocking(false);
116             params.setHandler(this);
117             params.setMaxNumberOfThreads(1);
118             params.setNumberOfStartThreads(1);
119             params.setStageName(name);
120             params.setPersistent(false);
121             this.stage = new Stage(params);
122         }else{
123             
124             try {
125                 TopicConnectionFactory JavaDoc conFactory = (TopicConnectionFactory JavaDoc) new MantaTopicConnectionFactory();
126                 TopicConnection JavaDoc con = conFactory.createTopicConnection();
127                 con.start();
128                 sendSession = con.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
129                 receiveSession = con.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
130                                 
131             } catch (JMSException JavaDoc e) {
132                 if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
133                     LogFactory.getLog("ScalableDispatcher").error("Problem while init distributed Dispatcher.",e);
134                 }
135             }
136         }
137         
138     }//ScalableDispatcher
139

140     /**
141      * modules use this method to send events to all the handlers of this dispatcher
142      * @param event any Serializable object
143      */

144     public synchronized void dispatch(Serializable JavaDoc event){
145         
146         if(distributed){
147             try {
148                 if(producer == null){
149                     producer = sendSession.createProducer(sendSession.createTopic(dispatcherName));
150                 }
151                 ObjectMessage JavaDoc msg = sendSession.createObjectMessage();
152             
153                 msg.setObject(event);
154                 producer.send( msg,
155                         javax.jms.DeliveryMode.NON_PERSISTENT,
156                         javax.jms.Message.DEFAULT_PRIORITY,
157                         100000);
158             } catch (JMSException JavaDoc e) {
159                 if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
160                     LogFactory.getLog("ScalableDispatcher").error("Problem while dispatching distributed event.",e);
161                 }
162             }
163         }else{
164             stage.enqueue(event);
165         }
166         
167     }
168     
169     /**
170      * internal use, not a part of the API
171      */

172     public boolean handle(Object JavaDoc event) {
173         synchronized(handlerSync){
174             if(handlers.size() == 0){
175                 try {
176                     handlerSync.wait();
177                 } catch (InterruptedException JavaDoc e) {
178                     if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
179                         LogFactory.getLog("ScalableDispatcher").error("Problem while waiting for handlers.",e);
180                     }
181                 }
182             }
183             Iterator JavaDoc handlersIter = handlers.iterator();
184             while(handlersIter.hasNext()){
185                 ((ScalableHandler)handlersIter.next()).handle(event);
186             }
187             
188         }
189         return true;
190     }
191     
192     /**
193      * returns the list of all the handlers of this dispatcher
194      * @return a list of all the handlers of this dispatcher
195      */

196     public List JavaDoc getHandlers() {
197         return handlers;
198     }
199     
200     
201     
202     /**
203      * modules that want to be handle the events that are dispatched with dispatcher use this method to do so
204      * @param handler a ScalableHandler object that will receive the event
205      */

206     public void addHandler(ScalableHandler handler) {
207         if(handler == null)
208             return;
209         synchronized(handlerSync){
210             if(distributed){
211                 if(handlers.size() == 0 ){
212                     try {
213                         if(consumer==null)
214                             consumer =receiveSession.createConsumer(receiveSession.createTopic(dispatcherName));
215                         consumer.setMessageListener(this);
216                     
217                     } catch (JMSException JavaDoc e) {
218                         if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
219                             LogFactory.getLog("ScalableDispatcher").error("Problem creating the JMS objects.",e);
220                         }
221                     }//try
222
}//if
223
}//if
224
handlers.add(handler);
225             handlerSync.notifyAll();
226         }
227     }//setHandler
228

229     
230     /**
231      * removes the handler for the dispatcher form its handlers list
232      * @param handler the object that up untill now was a handler for events
233      */

234     public void removeHandler(StageHandler handler){
235         synchronized(handlerSync){
236             handlers.remove(handler);
237             if(handlers.size() == 0) {
238                 try {
239                     consumer.setMessageListener(null);
240                 } catch (JMSException JavaDoc e) {
241                     if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
242                         LogFactory.getLog("ScalableDispatcher").error("Problem removing JMS listener.",e);
243                     }
244                 }
245             }
246         }
247     }
248     
249     /**
250      * internal use, not a part of the API
251      */

252     public void onMessage(Message JavaDoc msg) {
253         ObjectMessage JavaDoc o = (ObjectMessage JavaDoc)msg;
254         try {
255             handle(o.getObject());
256         } catch (JMSException JavaDoc e) {
257             if(LogFactory.getLog("ScalableDispatcher").isErrorEnabled()){
258                 LogFactory.getLog("ScalableDispatcher").error("Problem handeling new message.",e);
259             }
260         }
261         
262     }
263 }
264
Popular Tags