KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > services > queues > QueueSubscriberManager


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.kernel.services.queues;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54 import org.mr.IMessageListener;
55 import org.mr.MantaAgent;
56 import org.mr.MantaAgentConstants;
57 import org.mr.MantaException;
58 import org.mr.core.protocol.MantaBusMessage;
59 import org.mr.core.protocol.MantaBusMessageConsts;
60 import org.mr.core.util.SystemTime;
61 import org.mr.kernel.BlockingMessageListener;
62 import org.mr.kernel.IncomingClientMessageRouter;
63 import org.mr.kernel.control.ControlSignal;
64 import org.mr.kernel.services.ServiceConsumer;
65
66 /**
67  * subscribers to queue need to wait until a queue coordinator is found and moved from
68  * one queue coordinator to another, this object encapsulate this functionality
69  * @author Amir Shevat
70  *
71  */

72 public class QueueSubscriberManager {
73     ArrayList JavaDoc queueSubscribers = new ArrayList JavaDoc();
74     HashMap JavaDoc subscribersToListeners = new HashMap JavaDoc();
75     HashMap JavaDoc subscribersToNumberOfReceives = new HashMap JavaDoc();
76     AbstractQueueService queue;
77     MantaAgent layer;
78     private static Log log;
79     
80     QueueSubscriberManager(AbstractQueueService queue){
81         this.queue = queue;
82         layer = MantaAgent.getInstance();
83         log = log=LogFactory.getLog("QueueSubscriberManager");
84     }
85     
86     /**
87      * if there is a queue coordinator send subscribe to him else wait for one
88      * @param consumer the service actor that holds infomation about the recipient of the messages
89      * @param listener this object will be called when message arives
90      * * @param numberOfRecive
91      * -after this number of message this agent should not be
92      * notified on and more enqueue messages if numberOfRecive == 0
93      * then this is a no wait receive
94      * @throws MantaException
95      */

96     public synchronized void subscribeToQueue(ServiceConsumer consumer ,IMessageListener listener, long numberOfReceive) throws MantaException {
97         
98         queueSubscribers.add(consumer);
99         subscribersToListeners.put(consumer,listener );
100         subscribersToNumberOfReceives.put(consumer,String.valueOf(numberOfReceive));
101 // find the destinations
102
QueueMaster master =queue.getQueueMaster();
103         // if master is no where to be found return null
104
sendSubscriptionToCoordinator(master ,consumer ,listener );
105     }
106     
107     /**
108      * Removes this agent from listening to a given remote queue
109      *
110      * @param listener
111      * the local listener
112      * @param consumer
113      * the consumer the registered to the queue
114      */

115     public synchronized void unregisterFromQueue(ServiceConsumer consumer ,IMessageListener listener ) throws MantaException {
116         queueSubscribers.remove(consumer);
117         subscribersToListeners.remove(consumer);
118         subscribersToNumberOfReceives.remove(consumer);
119         IncomingClientMessageRouter router = layer.getSingletonRepository().getIncomingClientMessageRouter();
120         router.removeIncomingClientMessageListener(queue.getServiceName()+consumer.getId(), listener);
121         
122         QueueMaster master = queue.getQueueMaster();
123         // if master is no where to be found return null
124
if(master == null)
125             return ;
126         MantaBusMessage msg = MantaBusMessage.getInstance();
127         msg.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
128
129         // insert the control payload
130
ControlSignal control = new ControlSignal(ControlSignal.OPERATION_TYPE_QUEUE_UNREGISTER);
131
132         msg.setPayload(control);
133
134
135         msg.setRecipient(master);
136
137         msg.addHeader(MantaBusMessageConsts.HEADER_NAME_LOGICAL_DESTINATION, queue.getServiceName());
138
139         layer.send(msg, consumer, MantaAgentConstants.NON_PERSISTENT, MantaAgentConstants.HIGH, MantaAgentConstants.CONTROL_MESSAGES_TTL+SystemTime.gmtCurrentTimeMillis());
140
141     }
142     
143     
144     protected synchronized void queueCoordinatorFound(QueueMaster coordinator){
145         Iterator JavaDoc subscribers = queueSubscribers.iterator();
146         while(subscribers.hasNext()){
147             ServiceConsumer consumer = (ServiceConsumer) subscribers.next();
148             try {
149                 sendSubscriptionToCoordinator(coordinator,consumer, (IMessageListener) subscribersToListeners.get(consumer));
150             } catch (MantaException e) {
151                  if(log.isErrorEnabled()){
152                     log.error("error sending subscribe to queue coordinator", e);
153                 }
154             }
155         }
156     }
157     
158     /**
159      * sends the message to the current coordinator
160      */

161     private void sendSubscriptionToCoordinator(QueueMaster master ,ServiceConsumer consumer ,IMessageListener listener ) throws MantaException{
162         if(master != null){
163             // the message to be sent to the other side
164
MantaBusMessage msg = MantaBusMessage.getInstance();
165     
166             msg.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
167     
168             // insert the control payload
169
ControlSignal control = new ControlSignal(ControlSignal.OPERATION_TYPE_QUEUE_REGISTER);
170     
171             control.getParams().put(ControlSignal.NUMBER_OF_RECEIVE_ON_QUEUE_KEY, (String JavaDoc) subscribersToNumberOfReceives.get(consumer));
172             msg.setPayload(control);
173             String JavaDoc listenerStr = queue.getServiceName() + consumer.getId();
174             // removed old listener
175
IncomingClientMessageRouter router = layer.getSingletonRepository().getIncomingClientMessageRouter();
176             router.removeIncomingClientMessageListener(listenerStr, listener);
177             if (listener instanceof BlockingMessageListener)
178                 ((BlockingMessageListener)listener).setListenerString(listenerStr);
179             router.addIncommingClientMessageListener(listenerStr, listener);
180             
181            
182     
183             msg.setRecipient(master);
184     
185             msg.addHeader(MantaBusMessageConsts.HEADER_NAME_LOGICAL_DESTINATION, queue.getServiceName());
186     
187             layer.send(msg, consumer, MantaAgentConstants.NON_PERSISTENT, MantaAgentConstants.HIGH, MantaAgentConstants.CONTROL_MESSAGES_TTL+SystemTime.gmtCurrentTimeMillis());
188         }
189     }
190
191     public synchronized void removeSubscribeToQueue(ServiceConsumer consumer) {
192         queueSubscribers.remove(consumer);
193         subscribersToListeners.remove(consumer);
194         subscribersToNumberOfReceives.remove(consumer);
195         
196     }
197     
198     
199 }
200
Popular Tags