KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.walend.somnifugi;
2
3 import javax.jms.QueueSession JavaDoc;
4 import javax.jms.Queue JavaDoc;
5 import javax.jms.TemporaryQueue JavaDoc;
6 import javax.jms.QueueSender JavaDoc;
7 import javax.jms.QueueReceiver JavaDoc;
8 import javax.jms.JMSException JavaDoc;
9 import javax.jms.Message JavaDoc;
10
11 /**
12  * A queue requestor
13  *
14  * @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
15  * @since alpha-0-14
16  */

17 public class SomniQueueRequestor
18 {
19     private QueueSession JavaDoc session; // The queue session the queue belongs to.
20
private Queue JavaDoc queue; // The queue to perform the request/reply on.
21
private TemporaryQueue JavaDoc replyQueue;
22     private QueueSender JavaDoc requestSender;
23     private QueueReceiver JavaDoc replyReceiver;
24
25     /** Constructor for the <CODE>QueueRequestor</CODE> class.
26       *
27       * <P>This implementation assumes the session parameter to be non-transacted,
28       * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
29       * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
30       *
31       * @param session the <CODE>QueueSession</CODE> the queue belongs to
32       * @param queue the queue to perform the request/reply call on
33       *
34       * @exception JMSException if the JMS provider fails to create the
35       * <CODE>QueueRequestor</CODE> due to some internal
36       * error.
37       * @exception InvalidDestinationException if an invalid queue is specified.
38       */

39     public SomniQueueRequestor(QueueSession JavaDoc session, Queue JavaDoc queue) throws JMSException JavaDoc
40     {
41         this.session = session;
42         this.queue = queue;
43         replyQueue = session.createTemporaryQueue();
44         requestSender = session.createSender(queue);
45         replyReceiver = session.createReceiver(replyQueue);
46     }
47
48     /** Sends a request and waits for a reply. The temporary queue is used for
49       * the <CODE>JMSReplyTo</CODE> destination, and only one reply per request
50       * is expected.
51       *
52       * @param message the message to send
53       *
54       * @return the reply message
55       *
56       * @exception JMSException if the JMS provider fails to complete the
57       * request due to some internal error.
58       */

59
60     public Message JavaDoc request(Message JavaDoc message)
61         throws JMSException JavaDoc
62     {
63         message.setJMSReplyTo(replyQueue);
64         requestSender.send(message);
65         return (replyReceiver.receive());
66     }
67
68
69     /** Closes the <CODE>QueueRequestor</CODE> and its session.
70       *
71       * <P>Since a provider may allocate some resources on behalf of a
72       * <CODE>QueueRequestor</CODE> outside the Java virtual machine, clients
73       * should close them when they
74       * are not needed. Relying on garbage collection to eventually reclaim
75       * these resources may not be timely enough.
76       *
77       * <P>Note that this method closes the <CODE>QueueSession</CODE> object
78       * passed to the <CODE>QueueRequestor</CODE> constructor.
79       *
80       * @exception JMSException if the JMS provider fails to close the
81       * <CODE>QueueRequestor</CODE> due to some internal
82       * error.
83       */

84
85     public void close() throws JMSException JavaDoc
86     {
87         // publisher and consumer created by constructor are implicitly closed.
88
session.close();
89         replyQueue.delete();
90     }
91
92     /**
93 Sends a request message to the queue and wait for a reply. If the timeout comes before the reply, return null.
94 <P>This call blocks until a message arrives, the
95 timeout expires, or the reply receiver is closed.
96 A <CODE>timeout</CODE> of zero never expires, and the call blocks
97 indefinitely.
98       *
99 @param message the request
100 @param timeout the timeout value (in milliseconds)
101       *
102 @return the next message produced for the temporary message queue, or
103 null if the timeout expires or this message queue is concurrently
104 closed
105       *
106 @exception JMSException if the JMS provider fails to receive the next
107                         message due to some internal error.
108       */

109     public Message JavaDoc request(Message JavaDoc message,long timeout)
110         throws JMSException JavaDoc
111     {
112         long timesUp = System.currentTimeMillis()+timeout;
113
114         message.setJMSExpiration(timesUp);
115         message.setJMSReplyTo(replyQueue);
116         requestSender.send(message);
117         
118         long timeLeft = timesUp - System.currentTimeMillis();
119         
120         return replyReceiver.receive(timeLeft);
121     }
122 }
123
124 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
125 All rights reserved.
126
127 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
128
129 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
130
131 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.
132
133 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.
134
135 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
136
137 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.
138 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.
139
140 =================================================================================
141
142 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>.
143  */

144
Popular Tags