KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.walend.somnifugi;
2
3 import javax.naming.Context JavaDoc;
4 import javax.naming.NamingException JavaDoc;
5
6 import javax.jms.QueueSession JavaDoc;
7 import javax.jms.Queue JavaDoc;
8 import javax.jms.JMSException JavaDoc;
9 import javax.jms.QueueReceiver JavaDoc;
10 import javax.jms.QueueSender JavaDoc;
11 import javax.jms.QueueBrowser JavaDoc;
12 import javax.jms.TemporaryQueue JavaDoc;
13 import javax.jms.Destination JavaDoc;
14 import javax.jms.MessageProducer JavaDoc;
15 import javax.jms.MessageConsumer JavaDoc;
16 import javax.jms.Topic JavaDoc;
17 import javax.jms.TopicSubscriber JavaDoc;
18 import javax.jms.TemporaryTopic JavaDoc;
19 import javax.jms.InvalidDestinationException JavaDoc;
20
21 /**
22 A Session for SomniQueues.
23
24 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
25 @author @pwang@ added support for client acknowledgement.
26  */

27
28 public class SomniQueueSession
29     extends SomniSession
30     implements QueueSession JavaDoc
31 {
32     private int tempCount=0;
33
34     protected SomniQueueSession(String JavaDoc name,SomniExceptionListener exceptionLisetener,boolean started,Context JavaDoc context,int acknowledgeMode,String JavaDoc connectionClientID)
35     {
36         super(name,exceptionLisetener,started,context,acknowledgeMode,connectionClientID);
37     }
38
39     /** Creates a queue identity given a <CODE>Queue</CODE> name.
40       *
41 <P>This facility is provided for the rare cases where clients need to
42 dynamically manipulate queue identity. It allows the creation of a
43 queue identity with a provider-specific name. Clients that depend
44 on this ability are not portable.
45       *
46 <P>Note that this method is not for creating the physical queue.
47 The physical creation of queues is an administrative task and is not
48 to be initiated by the JMS API. The one exception is the
49 creation of temporary queues, which is accomplished with the
50 <CODE>createTemporaryQueue</CODE> method.
51       *
52 @param queueName the name of this <CODE>Queue</CODE>
53       *
54 @return a <CODE>Queue</CODE> with the given name
55       *
56 @exception JMSException if the session fails to create a queue
57                         due to some internal error.
58 @exception UnsupportedOperationException because it isn't implemented.
59       */

60     public Queue JavaDoc createQueue(String JavaDoc queueName)
61         throws JMSException JavaDoc
62     {
63         return SomniQueueCache.IT.getQueue(queueName,getContext());
64     }
65
66     /** Creates a <CODE>QueueReceiver</CODE> object to receive messages from the
67 specified queue.
68       *
69 @param queue the <CODE>Queue</CODE> to access
70       *
71 @exception JMSException if the session fails to create a receiver
72                         due to some internal error.
73 @exception InvalidDestinationException if an invalid queue is specified.
74       */

75     public QueueReceiver JavaDoc createReceiver(Queue JavaDoc queue)
76         throws JMSException JavaDoc
77     {
78         SomniQueue aqueue = (SomniQueue)queue;
79
80         synchronized(guard)
81         {
82             checkClosed();
83             String JavaDoc consumerName = createConsumerName(aqueue.getName(),"Receiver");
84
85             SomniQueueReceiver result = new SomniQueueReceiver((SomniQueue)queue,consumerName,getExceptionListener(),this);
86             addConsumer(result);
87             return result;
88         }
89     }
90
91     /** Creates a <CODE>QueueReceiver</CODE> object to receive messages from the
92 specified queue using a message selector.
93  
94 @param queue the <CODE>Queue</CODE> to access
95 @param messageSelector only messages with properties matching the
96 message selector expression are delivered. A value of null or
97 an empty string indicates that there is no message selector
98 for the message consumer.
99  
100 @exception JMSException if the session fails to create a receiver
101                         due to some internal error.
102 @exception InvalidDestinationException if an invalid queue is specified.
103 @exception InvalidSelectorException if the message selector is invalid.
104 @exception UnsupportedOperationException because it isn't implemented.
105       *
106       */

107     public QueueReceiver JavaDoc createReceiver(Queue JavaDoc queue,String JavaDoc messageSelectorString)
108         throws JMSException JavaDoc
109     {
110         SomniQueue aqueue = (SomniQueue)queue;
111         SomniMessageSelector messageSelector = new SQL92MessageSelector(messageSelectorString);
112
113         synchronized(guard)
114         {
115             checkClosed();
116             String JavaDoc consumerName = createConsumerName(aqueue.getName(),"Receiver");
117
118             SomniQueueReceiver result = new SomniQueueReceiver((SomniQueue)queue,consumerName,getExceptionListener(),messageSelector,this);
119             addConsumer(result);
120             return result;
121         }
122     }
123
124     /** Creates a <CODE>QueueSender</CODE> object to send messages to the
125 specified queue.
126       *
127 @param queue the <CODE>Queue</CODE> to access, or null if this is an
128 unidentified producer
129       *
130 @exception JMSException if the session fails to create a sender
131                         due to some internal error.
132 @exception InvalidDestinationException if an invalid queue is specified.
133       */

134     public QueueSender JavaDoc createSender(Queue JavaDoc queue)
135         throws JMSException JavaDoc
136     {
137         synchronized(guard)
138             {
139                 checkClosed();
140                 SomniQueueSender result = new SomniQueueSender((SomniQueue)queue,createProducerName(queue.getQueueName(),"Sender"),getConnectionClientID());
141                 addProducer(result);
142                 return result;
143             }
144     }
145
146     /** Creates a <CODE>QueueBrowser</CODE> object to peek at the messages on
147 the specified queue.
148       *
149 @param queue the <CODE>Queue</CODE> to access
150       *
151 @exception JMSException if the session fails to create a browser
152                         due to some internal error.
153 @exception InvalidDestinationException if an invalid queue is specified.
154       */

155     public QueueBrowser JavaDoc createBrowser(Queue JavaDoc queue)
156         throws JMSException JavaDoc
157     {
158         return new SomniQueueBrowser((SomniQueue)queue);
159     }
160
161     /** Creates a <CODE>QueueBrowser</CODE> object to peek at the messages on
162 the specified queue using a message selector.
163  
164 @param queue the <CODE>Queue</CODE> to access
165 @param messageSelector only messages with properties matching the
166 message selector expression are delivered. A value of null or
167 an empty string indicates that there is no message selector
168 for the message consumer.
169  
170 @exception JMSException if the session fails to create a browser
171                         due to some internal error.
172 @exception InvalidDestinationException if an invalid queue is specified.
173 @exception InvalidSelectorException if the message selector is invalid.
174 @exception UnsupportedOperationException because it isn't implemented.
175       */

176     public QueueBrowser JavaDoc createBrowser(Queue JavaDoc queue,String JavaDoc messageSelectorString)
177         throws JMSException JavaDoc
178     {
179         return new SomniQueueBrowser((SomniQueue)queue,messageSelectorString);
180     }
181
182     /** Creates a <CODE>TemporaryQueue</CODE> object. Its lifetime will be that
183 of the <CODE>QueueConnection</CODE> unless it is deleted earlier.
184       *
185 @return a temporary queue identity
186       *
187 @exception JMSException if the session fails to create a temporary queue
188                         due to some internal error.
189       */

190     public TemporaryQueue JavaDoc createTemporaryQueue()
191         throws JMSException JavaDoc
192     {
193         String JavaDoc tempName;
194         synchronized(guard)
195             {
196                 tempName = getName()+":temp"+tempCount;
197                 tempCount++;
198             }
199         SomniTemporaryQueue queue = new SomniTemporaryQueue(tempName,ChannelFactoryCache.IT.getChannelFactory(tempName,getContext(),true),getContext());
200         SomniQueueCache.IT.putTemporaryQueue(queue);
201         
202         return queue;
203     }
204
205     //new Session methods from jms 1.1
206

207     /** Creates a <CODE>MessageProducer</CODE> to send messages to the specified
208       * destination.
209       *
210       * <P>A client uses a <CODE>MessageProducer</CODE> object to send
211       * messages to a destination. Since <CODE>Queue</CODE> and <CODE>Topic</CODE>
212       * both inherit from <CODE>Destination</CODE>, they can be used in
213       * the destination parameter to create a <CODE>MessageProducer</CODE> object.
214       *
215       * @param destination the <CODE>Destination</CODE> to send to,
216       * or null if this is a producer which does not have a specified
217       * destination.
218       *
219       * @exception JMSException if the session fails to create a MessageProducer
220       * due to some internal error.
221       * @exception InvalidDestinationException if an invalid destination
222       * is specified.
223       *
224       * @since 1.1
225       *
226      */

227
228     public MessageProducer JavaDoc createProducer(Destination JavaDoc destination) throws JMSException JavaDoc
229     {
230         try
231             {
232                 return createSender((Queue JavaDoc)destination);
233             }
234         catch(ClassCastException JavaDoc cce)
235             {
236                 throw new InvalidDestinationException JavaDoc("destination must be a Queue, not a "+destination.getClass().getName(),cce.getMessage());
237             }
238     }
239     
240     
241        /** Creates a <CODE>MessageConsumer</CODE> for the specified destination.
242       * Since <CODE>Queue</CODE> and <CODE>Topic</CODE>
243       * both inherit from <CODE>Destination</CODE>, they can be used in
244       * the destination parameter to create a <CODE>MessageConsumer</CODE>.
245       *
246       * @param destination the <CODE>Destination</CODE> to access.
247       *
248       * @exception JMSException if the session fails to create a consumer
249       * due to some internal error.
250       * @exception InvalidDestinationException if an invalid destination
251       * is specified.
252       *
253       * @since 1.1
254       */

255
256     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination) throws JMSException JavaDoc
257     {
258         try
259             {
260                 return createReceiver((Queue JavaDoc)destination);
261             }
262         catch(ClassCastException JavaDoc cce)
263             {
264                 throw new InvalidDestinationException JavaDoc("destination must be a Queue, not a "+destination.getClass().getName(),cce.getMessage());
265             }
266     }
267     
268  
269        /** Creates a <CODE>MessageConsumer</CODE> for the specified destination,
270       * using a message selector.
271       * Since <CODE>Queue</CODE> and <CODE>Topic</CODE>
272       * both inherit from <CODE>Destination</CODE>, they can be used in
273       * the destination parameter to create a <CODE>MessageConsumer</CODE>.
274       *
275       * <P>A client uses a <CODE>MessageConsumer</CODE> object to receive
276       * messages that have been sent to a destination.
277       *
278       *
279       * @param destination the <CODE>Destination</CODE> to access
280       * @param messageSelector only messages with properties matching the
281       * message selector expression are delivered. A value of null or
282       * an empty string indicates that there is no message selector
283       * for the message consumer.
284       *
285       *
286       * @exception JMSException if the session fails to create a MessageConsumer
287       * due to some internal error.
288       * @exception InvalidDestinationException if an invalid destination
289        * is specified.
290      
291       * @exception InvalidSelectorException if the message selector is invalid.
292       *
293       * @since 1.1
294       */

295     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination, java.lang.String JavaDoc messageSelector)
296         throws JMSException JavaDoc
297     {
298         try
299         {
300             return createReceiver((Queue JavaDoc)destination,messageSelector);
301         }
302         catch(ClassCastException JavaDoc cce)
303         {
304             throw new InvalidDestinationException JavaDoc("destination must be a Queue, not a "+destination.getClass().getName(),cce.getMessage());
305         }
306     }
307     
308     
309      /** Creates <CODE>MessageConsumer</CODE> for the specified destination, using a
310       * message selector. This method can specify whether messages published by
311       * its own connection should be delivered to it, if the destination is a
312       * topic.
313       *<P> Since <CODE>Queue</CODE> and <CODE>Topic</CODE>
314       * both inherit from <CODE>Destination</CODE>, they can be used in
315       * the destination parameter to create a <CODE>MessageConsumer</CODE>.
316       * <P>A client uses a <CODE>MessageConsumer</CODE> object to receive
317       * messages that have been published to a destination.
318       *
319       * <P>In some cases, a connection may both publish and subscribe to a
320       * topic. The consumer <CODE>NoLocal</CODE> attribute allows a consumer
321       * to inhibit the delivery of messages published by its own connection.
322       * The default value for this attribute is False. The <CODE>noLocal</CODE>
323       * value must be supported by destinations that are topics.
324       *
325       * @param destination the <CODE>Destination</CODE> to access
326       * @param messageSelector only messages with properties matching the
327       * message selector expression are delivered. A value of null or
328       * an empty string indicates that there is no message selector
329       * for the message consumer.
330       * @param NoLocal - if true, and the destination is a topic,
331       * inhibits the delivery of messages published
332       * by its own connection. The behavior for
333       * <CODE>NoLocal</CODE> is
334       * not specified if the destination is a queue.
335       *
336       * @exception JMSException if the session fails to create a MessageConsumer
337       * due to some internal error.
338       * @exception InvalidDestinationException if an invalid destination
339        * is specified.
340      
341       * @exception InvalidSelectorException if the message selector is invalid.
342       *
343       * @since 1.1
344       *
345       */

346     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination,String JavaDoc messageSelector,boolean NoLocal)
347         throws JMSException JavaDoc
348     {
349 //todo look into supporting noLocal via connections.
350
if(NoLocal)
351         {
352             throw new UnsupportedOperationException JavaDoc("You're using NoLocal in Somnifugi? That's a pretty boring consumer.");
353         }
354         try
355         {
356             return createReceiver((Queue JavaDoc)destination,messageSelector);
357         }
358         catch(ClassCastException JavaDoc cce)
359         {
360             throw new InvalidDestinationException JavaDoc("destination must be a Queue, not a "+destination.getClass().getName(),cce.getMessage());
361         }
362     }
363     
364       /** Creates a topic identity given a <CODE>Topic</CODE> name.
365       *
366       * <P>This facility is provided for the rare cases where clients need to
367       * dynamically manipulate topic identity. This allows the creation of a
368       * topic identity with a provider-specific name. Clients that depend
369       * on this ability are not portable.
370       *
371       * <P>Note that this method is not for creating the physical topic.
372       * The physical creation of topics is an administrative task and is not
373       * to be initiated by the JMS API. The one exception is the
374       * creation of temporary topics, which is accomplished with the
375       * <CODE>createTemporaryTopic</CODE> method.
376       *
377       * @param topicName the name of this <CODE>Topic</CODE>
378       *
379       * @return a <CODE>Topic</CODE> with the given name
380       *
381       * @exception JMSException if the session fails to create a topic
382       * due to some internal error.
383       * @since 1.1
384       */

385
386     public Topic JavaDoc createTopic(String JavaDoc topicName) throws JMSException JavaDoc
387     {
388         throw new IllegalStateException JavaDoc("Don't use a QueueSession to work with Topics");
389     }
390
391      /** Creates a <CODE>QueueBrowser</CODE> object to peek at the messages on
392       * the specified queue.
393       *
394       * @param queue the <CODE>queue</CODE> to access
395       *
396       * @exception InvalidDestinationException if an invalid destination
397       * is specified
398       *
399       * @since 1.1
400       */

401     //todo where is this method? It wasn't in Session.java
402

403       /** Creates a durable subscriber to the specified topic.
404       *
405       * <P>If a client needs to receive all the messages published on a
406       * topic, including the ones published while the subscriber is inactive,
407       * it uses a durable <CODE>TopicSubscriber</CODE>. The JMS provider
408       * retains a record of this
409       * durable subscription and insures that all messages from the topic's
410       * publishers are retained until they are acknowledged by this
411       * durable subscriber or they have expired.
412       *
413       * <P>Sessions with durable subscribers must always provide the same
414       * client identifier. In addition, each client must specify a name that
415       * uniquely identifies (within client identifier) each durable
416       * subscription it creates. Only one session at a time can have a
417       * <CODE>TopicSubscriber</CODE> for a particular durable subscription.
418       *
419       * <P>A client can change an existing durable subscription by creating
420       * a durable <CODE>TopicSubscriber</CODE> with the same name and a new
421       * topic and/or
422       * message selector. Changing a durable subscriber is equivalent to
423       * unsubscribing (deleting) the old one and creating a new one.
424       *
425       * <P>In some cases, a connection may both publish and subscribe to a
426       * topic. The subscriber <CODE>NoLocal</CODE> attribute allows a subscriber
427       * to inhibit the delivery of messages published by its own connection.
428       * The default value for this attribute is false.
429       *
430       * @param topic the non-temporary <CODE>Topic</CODE> to subscribe to
431       * @param name the name used to identify this subscription
432       *
433       * @exception JMSException if the session fails to create a subscriber
434       * due to some internal error.
435       * @exception InvalidDestinationException if an invalid topic is specified.
436       *
437       * @since 1.1
438       */

439
440     public TopicSubscriber JavaDoc createDurableSubscriber(Topic JavaDoc topic,String JavaDoc name)
441         throws JMSException JavaDoc
442     {
443         throw new IllegalStateException JavaDoc("Don't use a QueueSession to work with Topics");
444     }
445
446
447     /** Creates a durable subscriber to the specified topic, using a
448       * message selector and specifying whether messages published by its
449       * own connection should be delivered to it.
450       *
451       * <P>If a client needs to receive all the messages published on a
452       * topic, including the ones published while the subscriber is inactive,
453       * it uses a durable <CODE>TopicSubscriber</CODE>. The JMS provider
454       * retains a record of this
455       * durable subscription and insures that all messages from the topic's
456       * publishers are retained until they are acknowledged by this
457       * durable subscriber or they have expired.
458       *
459       * <P>Sessions with durable subscribers must always provide the same
460       * client identifier. In addition, each client must specify a name which
461       * uniquely identifies (within client identifier) each durable
462       * subscription it creates. Only one session at a time can have a
463       * <CODE>TopicSubscriber</CODE> for a particular durable subscription.
464       * An inactive durable subscriber is one that exists but
465       * does not currently have a message consumer associated with it.
466       *
467       * <P>A client can change an existing durable subscription by creating
468       * a durable <CODE>TopicSubscriber</CODE> with the same name and a new
469       * topic and/or
470       * message selector. Changing a durable subscriber is equivalent to
471       * unsubscribing (deleting) the old one and creating a new one.
472       *
473       * @param topic the non-temporary <CODE>Topic</CODE> to subscribe to
474       * @param name the name used to identify this subscription
475       * @param messageSelector only messages with properties matching the
476       * message selector expression are delivered. A value of null or
477       * an empty string indicates that there is no message selector
478       * for the message consumer.
479       * @param noLocal if set, inhibits the delivery of messages published
480       * by its own connection
481       *
482       * @exception JMSException if the session fails to create a subscriber
483       * due to some internal error.
484       * @exception InvalidDestinationException if an invalid topic is specified.
485       * @exception InvalidSelectorException if the message selector is invalid.
486       *
487       * @since 1.1
488       */

489  
490     public TopicSubscriber JavaDoc createDurableSubscriber(Topic JavaDoc topic,String JavaDoc name,String JavaDoc messageSelector,boolean noLocal)
491         throws JMSException JavaDoc
492     {
493         throw new IllegalStateException JavaDoc("Don't use a QueueSession to work with Topics");
494     }
495     
496
497      /** Creates a <CODE>TemporaryTopic</CODE> object. Its lifetime will be that
498       * of the <CODE>Connection</CODE> unless it is deleted earlier.
499       *
500       * @return a temporary topic identity
501       *
502       * @exception JMSException if the session fails to create a temporary
503       * topic due to some internal error.
504       *
505       * @since 1.1
506       */

507  
508     public TemporaryTopic JavaDoc createTemporaryTopic() throws JMSException JavaDoc
509     {
510         throw new IllegalStateException JavaDoc("Don't use a QueueSession to work with Topics");
511     }
512
513
514     /** Unsubscribes a durable subscription that has been created by a client.
515       *
516       * <P>This method deletes the state being maintained on behalf of the
517       * subscriber by its provider.
518       *
519       * <P>It is erroneous for a client to delete a durable subscription
520       * while there is an active <CODE>MessageConsumer</CODE>
521       * or <CODE>TopicSubscriber</CODE> for the
522       * subscription, or while a consumed message is part of a pending
523       * transaction or has not been acknowledged in the session.
524       *
525       * @param name the name used to identify this subscription
526       *
527       * @exception JMSException if the session fails to unsubscribe to the
528       * durable subscription due to some internal error.
529       * @exception InvalidDestinationException if an invalid subscription name
530       * is specified.
531       *
532       * @since 1.1
533       */

534
535     public void unsubscribe(String JavaDoc name) throws JMSException JavaDoc
536     {
537         throw new IllegalStateException JavaDoc("Don't use a QueueSession to work with Topics");
538     }
539
540
541 }
542
543 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
544 All rights reserved.
545
546 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
547
548 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
549
550 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.
551
552 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.
553
554 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
555
556 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.
557 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.
558
559 =================================================================================
560
561 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>.
562  */

563
Popular Tags