KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > TimeoutChannelFactory


1 package net.walend.somnifugi;
2
3 import java.util.Hashtable JavaDoc;
4 import java.util.Enumeration JavaDoc;
5
6 import javax.naming.Context JavaDoc;
7 import javax.naming.NamingException JavaDoc;
8
9 import javax.jms.Message JavaDoc;
10
11 import net.walend.somnifugi.channel.Puttable;
12 import net.walend.somnifugi.channel.Takable;
13 import net.walend.somnifugi.channel.Channel;
14 import net.walend.somnifugi.channel.ChannelFactory;
15
16 /**
17 A factory that wraps a Channel with code for a timeout. If the Channel doesn't put the item before the timeout, it throws an InterruptedException.
18 <p>
19 Properties available include
20 <ul>
21 <li><i>destination</i>.timeout - The timeout time in ms. (Default is ten minutes.)
22 <li>wrapped-<i>destination</i> - Any properties for the internal ChannelFactory. (Default will be the default Channel properties.)
23 </ul>
24 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
25  */

26
27 public class TimeoutChannelFactory
28     implements ChannelFactory<Message JavaDoc>
29 {
30     /**
31 Ten minutes.
32     */

33     //todo move properties to SomniProperties
34
public static final int DEFAULTTIMEOUT = 600000;
35     public static final String JavaDoc DEFAULTTIMEOUTPROP = SomniProperties.DEFAULT + "." + SomniProperties.TIMEOUTPROP;
36
37     private ChannelFactory wrappedChannelFactory = null;
38     
39     public TimeoutChannelFactory()
40     {
41         SomniLogger.IT.finer("Creating new TimeoutChannelFactory.");
42     }
43
44     /**
45 @return a Puttable for a SomniDestination.
46       */

47     public Puttable<Message JavaDoc> createPuttable(String JavaDoc destinationName,Context JavaDoc context)
48         throws SomniNamingException
49     {
50         return createChannel(destinationName,context).getPuttable();
51     }
52         
53     /**
54 @return a Takable for a SomniDestination.
55       */

56     public Takable<Message JavaDoc> createTakable(String JavaDoc destinationName,Context JavaDoc context)
57         throws SomniNamingException
58     {
59         return createChannel(destinationName,context).getTakable();
60     }
61     
62 //TODO TimeoutChannel only needs to wrap Puttable, not all of Channel.
63
@SuppressWarnings JavaDoc("unchecked")
64     public Channel<Message JavaDoc> createChannel(String JavaDoc destinationName,Context JavaDoc context)
65         throws SomniNamingException
66     {
67         try
68         {
69             Hashtable JavaDoc env = context.getEnvironment();
70             int timeout = DEFAULTTIMEOUT;
71     
72             String JavaDoc timeoutKey = destinationName + "."+SomniProperties.TIMEOUTPROP;
73     
74             String JavaDoc timeoutString = null;
75     
76             if(env.containsKey(timeoutKey))
77             {
78                 timeoutString = (String JavaDoc)env.get(timeoutKey);
79             }
80             else if(env.containsKey(DEFAULTTIMEOUTPROP))
81             {
82                 timeoutString = (String JavaDoc)env.get(DEFAULTTIMEOUTPROP);
83             }
84             if(timeoutString!=null)
85             {
86                 timeout = Integer.parseInt(timeoutString);
87             }
88             SomniLogger.IT.finest("Creating TimeoutChannel with timeout "+timeout+" for "+destinationName);
89     
90             wrappedChannelFactory = ChannelFactoryCache.IT.getChannelFactory(SomniProperties.WRAPPEDPROP+destinationName,context,true);
91             
92             Channel<Message JavaDoc> wrappedChannel =wrappedChannelFactory.createChannel(SomniProperties.WRAPPEDPROP+destinationName,context);
93     
94             return new TimeoutChannel(timeout,wrappedChannel);
95         }
96         catch(NamingException JavaDoc ne)
97         {
98             throw new SomniNamingException(ne);
99         }
100     }
101
102     private static class TimeoutChannel
103         implements Channel<Message JavaDoc>,Puttable<Message JavaDoc>,Takable<Message JavaDoc>
104     {
105         private Channel<Message JavaDoc> wrappedChannel;
106         private Puttable<Message JavaDoc> wrappedPuttable;
107         private Takable<Message JavaDoc> wrappedTakable;
108         private int timeout;
109
110         public TimeoutChannel(int timeout,Channel<Message JavaDoc> wrappedChannel)
111         {
112             this.timeout = timeout;
113             this.wrappedChannel = wrappedChannel;
114             this.wrappedPuttable = wrappedChannel.getPuttable();
115             this.wrappedTakable = wrappedChannel.getTakable();
116         }
117
118         public void put(Message JavaDoc item)
119             throws InterruptedException JavaDoc
120         {
121             boolean result = wrappedPuttable.offer(item,timeout);
122             if(!result)
123             {
124                 throw new InterruptedException JavaDoc("Attempt to offer timed out after "+timeout);
125             }
126         }
127
128         public void pushBack(Message JavaDoc item)
129             throws InterruptedException JavaDoc
130         {
131             wrappedTakable.pushBack(item);
132         }
133
134         public boolean offer(Message JavaDoc item, long msecs)
135             throws InterruptedException JavaDoc
136         {
137             return wrappedPuttable.offer(item,msecs);
138         }
139
140         public Message JavaDoc take()
141             throws InterruptedException JavaDoc
142         {
143             return wrappedTakable.take();
144         }
145
146         public Message JavaDoc poll()
147         {
148             return wrappedTakable.poll();
149         }
150
151         public Message JavaDoc poll(long msecs)
152             throws InterruptedException JavaDoc
153         {
154             return wrappedTakable.poll(msecs);
155         }
156
157         public Message JavaDoc peek()
158         {
159             return wrappedTakable.peek();
160         }
161
162         //Channel methods
163
public boolean hasRealPushback()
164         {
165             return wrappedChannel.hasRealPushback();
166         }
167         
168         public boolean supportsPriorities()
169         {
170             return wrappedChannel.supportsPriorities();
171         }
172         
173         public boolean supportsMessageSelectors()
174         {
175             return wrappedChannel.supportsMessageSelectors();
176         }
177         
178         public Enumeration JavaDoc snapShot()
179         {
180             return wrappedTakable.snapShot();
181         }
182
183         public Enumeration JavaDoc snapShot(SomniMessageSelector messageSelector)
184             throws SomniMessageSelectorException
185         {
186             return wrappedTakable.snapShot(messageSelector);
187         }
188         
189         public Puttable<Message JavaDoc> getPuttable()
190         {
191             return this;
192         }
193         
194         public Takable<Message JavaDoc> getTakable()
195         {
196             return this;
197         }
198
199         public Takable<Message JavaDoc> getTakable(SomniMessageSelector messageSelector)
200         {
201             return wrappedChannel.getTakable(messageSelector);
202         }
203
204         public int guessSize()
205         {
206             return wrappedTakable.guessSize();
207         }
208
209     }
210 }
211
212 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
213 All rights reserved.
214
215 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
216
217 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
218
219 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.
220
221 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.
222
223 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
224
225 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.
226 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.
227
228 =================================================================================
229
230 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>.
231  */

232
Popular Tags