KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > juc > SimpleFanOut


1 package net.walend.somnifugi.juc;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Set JavaDoc;
6 import java.util.HashSet JavaDoc;
7
8 import javax.naming.Referenceable JavaDoc;
9 import javax.naming.Reference JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11 import javax.naming.Context JavaDoc;
12
13 import javax.jms.JMSException JavaDoc;
14 import javax.jms.Message JavaDoc;
15
16 import net.walend.somnifugi.SomniMessageSelector;
17 import net.walend.somnifugi.SomniNamingException;
18 import net.walend.somnifugi.SomniMessageSelectorException;
19 import net.walend.somnifugi.SomniRuntimeException;
20 import net.walend.somnifugi.SomniMessage;
21
22 import net.walend.somnifugi.channel.Channel;
23 import net.walend.somnifugi.channel.Puttable;
24 import net.walend.somnifugi.channel.FanOut;
25 import net.walend.somnifugi.channel.Takable;
26 import net.walend.somnifugi.channel.ChannelFactory;
27
28 /**
29 A Puttable that sends the message to many Channels.
30 <p>
31 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
32 @since alpha-0-16
33  */

34
35 public class SimpleFanOut
36     implements FanOut<Message JavaDoc>
37 {
38     
39     private Map JavaDoc<String JavaDoc,SomniMessageSelector> subscriberNamesToMessageSelectors = new HashMap JavaDoc<String JavaDoc,SomniMessageSelector>();
40
41     private Map JavaDoc<String JavaDoc,Puttable<Message JavaDoc>> subscriberNamesToPuttables = new HashMap JavaDoc<String JavaDoc,Puttable<Message JavaDoc>>();
42     private Map JavaDoc<String JavaDoc,Takable<Message JavaDoc>> subscriberNamesToTakables = new HashMap JavaDoc<String JavaDoc,Takable<Message JavaDoc>>();
43
44     private Map JavaDoc<String JavaDoc,String JavaDoc> subscriberNamesToConnectionClientIDsForNoLocalMessages = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
45
46     private final Object JavaDoc subscriberNamesToMapsGuard = new Object JavaDoc();
47
48     private Set JavaDoc<String JavaDoc> durableSubscribers = new HashSet JavaDoc<String JavaDoc>(3);
49     
50     private String JavaDoc copyMode;
51     
52     private ChannelFactory<Message JavaDoc> factory;
53     private String JavaDoc topicName;
54     private Context JavaDoc context;
55     
56     public SimpleFanOut(String JavaDoc copyMode,ChannelFactory<Message JavaDoc> factory,String JavaDoc topicName,Context JavaDoc context)
57     {
58         this.copyMode = copyMode;
59         this.factory = factory;
60         this.topicName = topicName;
61         this.context = context;
62     }
63
64     public Takable<Message JavaDoc> addSubscriber(String JavaDoc subscriber,boolean durable,SomniMessageSelector messageSelector,boolean noLocal,String JavaDoc subscriberConnectionClientID)
65         throws SomniNamingException
66     {
67         synchronized(subscriberNamesToMapsGuard)
68         {
69             if(subscriberNamesToTakables.containsKey(subscriber))
70             {
71                 return subscriberNamesToTakables.get(subscriber);
72             }
73             else
74             {
75                 Channel<Message JavaDoc> channel = factory.createChannel(topicName,context);
76                 Puttable<Message JavaDoc> puttable = channel.getPuttable();
77                 subscriberNamesToPuttables.put(subscriber,puttable);
78                 
79                 Takable<Message JavaDoc> takable = channel.getTakable();
80                 subscriberNamesToTakables.put(subscriber,takable);
81
82                 if(durable)
83                 {
84                     durableSubscribers.add(subscriber);
85                 }
86                 if(messageSelector!=null)
87                 {
88                     subscriberNamesToMessageSelectors.put(subscriber,messageSelector);
89                 }
90                 if(noLocal)
91                 {
92                     subscriberNamesToConnectionClientIDsForNoLocalMessages.put(subscriber,subscriberConnectionClientID);
93                 }
94                 return takable;
95             }
96         }
97     }
98     
99     public void removeSubscriber(String JavaDoc subscriber)
100     {
101         synchronized(subscriberNamesToMapsGuard)
102         {
103             if(!durableSubscribers.contains(subscriber))
104             {
105                 subscriberNamesToPuttables.remove(subscriber);
106                 subscriberNamesToTakables.remove(subscriber);
107                 subscriberNamesToMessageSelectors.remove(subscriber);
108                 subscriberNamesToConnectionClientIDsForNoLocalMessages.remove(subscriber);
109             }
110         }
111     }
112
113     public void removeDurableSubscriber(String JavaDoc subscriber)
114     {
115         synchronized(subscriberNamesToMapsGuard)
116         {
117             durableSubscribers.remove(subscriber);
118             subscriberNamesToPuttables.remove(subscriber);
119             subscriberNamesToTakables.remove(subscriber);
120             subscriberNamesToMessageSelectors.remove(subscriber);
121             subscriberNamesToConnectionClientIDsForNoLocalMessages.remove(subscriber);
122         }
123     }
124
125     private boolean shouldPut(String JavaDoc subscriberName,Message JavaDoc message)
126     {
127         try
128         {
129             SomniMessageSelector messageSelector = (SomniMessageSelector)subscriberNamesToMessageSelectors.get(subscriberName);
130
131             if(message instanceof SomniMessage)
132             {
133                 SomniMessage somniMessage = (SomniMessage)message;
134                 if(subscriberNamesToConnectionClientIDsForNoLocalMessages.containsKey(subscriberName)
135                     &&(somniMessage.getSomniProducerConnectionClientID().equals(subscriberNamesToConnectionClientIDsForNoLocalMessages.get(subscriberName))))
136                 {
137                     return false;
138                 }
139             }
140             if(messageSelector==null)
141             {
142                 return true;
143             }
144             if(messageSelector.matches(message))
145             {
146                 return true;
147             }
148             return false;
149         }
150         catch(SomniMessageSelectorException smse)
151         {
152             throw new SomniRuntimeException("Message selector had a problem.",smse);
153         }
154     }
155     
156     //Puttable interface
157
public void put(Message JavaDoc item)
158         throws InterruptedException JavaDoc
159     {
160         SomniMessage message = (SomniMessage)item;
161
162         synchronized(subscriberNamesToMapsGuard)
163         {
164             for(String JavaDoc key : subscriberNamesToPuttables.keySet())
165             {
166                 if(shouldPut(key,message))
167                 {
168                     message = message.copy(copyMode);
169                     Puttable<Message JavaDoc> putter = subscriberNamesToPuttables.get(key);
170                     putter.put(message);
171                 }
172             }
173         }
174     }
175
176     //todo review timeout issues within offer
177
public boolean offer(Message JavaDoc item,long msecs)
178         throws InterruptedException JavaDoc
179     {
180         synchronized(subscriberNamesToMapsGuard)
181         {
182             SomniMessage message = (SomniMessage)item;
183             
184             boolean result = true;
185             for(String JavaDoc key : subscriberNamesToPuttables.keySet())
186             {
187                 SomniMessageSelector messageSelector = (SomniMessageSelector)subscriberNamesToMessageSelectors.get(key);
188                 if(shouldPut(key,message))
189                 {
190                     message = message.copy(copyMode);
191                     Puttable<Message JavaDoc> putter = subscriberNamesToPuttables.get(key);
192                     result &= putter.offer(message,msecs);
193                 }
194             }
195             return result;
196         }
197     }
198 }
199
200 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
201 All rights reserved.
202
203 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
204
205 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
206
207 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
208
209 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
210
211 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
212
213 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
214 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
215
216 =================================================================================
217
218 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
219  */

220
Popular Tags