1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2000-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License ("CDDL") (collectively, the "License"). You may
9 * not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or mq/legal/LICENSE.txt. See the License for the specific language
12 * governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at mq/legal/LICENSE.txt. Sun designates
16 * this particular file as subject to the "Classpath" exception as provided by
17 * Sun in the GPL Version 2 section of the License file that accompanied this
18 * code. If applicable, add the following below the License Header, with the
19 * fields enclosed by brackets [] replaced by your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
21 *
22 * Contributor(s):
23 *
24 * If you wish your version of this file to be governed by only the CDDL or
25 * only the GPL Version 2, indicate your decision by adding "[Contributor]
26 * elects to include this software in this distribution under the [CDDL or GPL
27 * Version 2] license." If you don't indicate a single choice of license, a
28 * recipient has the option to distribute your version of this file under
29 * either the CDDL, the GPL Version 2 or to extend the choice of license to
30 * its licensees as provided above. However, if you add GPL Version 2 code
31 * and therefore, elected the GPL Version 2 license, then the option applies
32 * only if the new code is made subject to such option by the copyright holder.
33 */
34
35 /*
36 * @(#)QueueRequestor.java 1.23 07/02/07
37 */
38
39 package javax.jms;
40
41 /** The <CODE>QueueRequestor</CODE> helper class simplifies
42 * making service requests.
43 *
44 * <P>The <CODE>QueueRequestor</CODE> constructor is given a non-transacted
45 * <CODE>QueueSession</CODE> and a destination <CODE>Queue</CODE>. It creates a
46 * <CODE>TemporaryQueue</CODE> for the responses and provides a
47 * <CODE>request</CODE> method that sends the request message and waits
48 * for its reply.
49 *
50 * <P>This is a basic request/reply abstraction that should be sufficient
51 * for most uses. JMS providers and clients are free to create more
52 * sophisticated versions.
53 *
54 * @see javax.jms.TopicRequestor
55 */
56
57 public class QueueRequestor {
58
59 QueueSession session; // The queue session the queue belongs to.
60 Queue queue; // The queue to perform the request/reply on.
61 TemporaryQueue tempQueue;
62 QueueSender sender;
63 QueueReceiver receiver;
64
65
66 /** Constructor for the <CODE>QueueRequestor</CODE> class.
67 *
68 * <P>This implementation assumes the session parameter to be non-transacted,
69 * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
70 * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
71 *
72 * @param session the <CODE>QueueSession</CODE> the queue belongs to
73 * @param queue the queue to perform the request/reply call on
74 *
75 * @exception JMSException if the JMS provider fails to create the
76 * <CODE>QueueRequestor</CODE> due to some internal
77 * error.
78 * @exception InvalidDestinationException if an invalid queue is specified.
79 */
80
81 public
82 QueueRequestor(QueueSession session, Queue queue) throws JMSException {
83 this.session = session;
84 this.queue = queue;
85 tempQueue = session.createTemporaryQueue();
86 sender = session.createSender(queue);
87 receiver = session.createReceiver(tempQueue);
88 }
89
90
91 /** Sends a request and waits for a reply. The temporary queue is used for
92 * the <CODE>JMSReplyTo</CODE> destination, and only one reply per request
93 * is expected.
94 *
95 * @param message the message to send
96 *
97 * @return the reply message
98 *
99 * @exception JMSException if the JMS provider fails to complete the
100 * request due to some internal error.
101 */
102
103 public Message
104 request(Message message) throws JMSException {
105 message.setJMSReplyTo(tempQueue);
106 sender.send(message);
107 return (receiver.receive());
108 }
109
110
111 /** Closes the <CODE>QueueRequestor</CODE> and its session.
112 *
113 * <P>Since a provider may allocate some resources on behalf of a
114 * <CODE>QueueRequestor</CODE> outside the Java virtual machine, clients
115 * should close them when they
116 * are not needed. Relying on garbage collection to eventually reclaim
117 * these resources may not be timely enough.
118 *
119 * <P>Note that this method closes the <CODE>QueueSession</CODE> object
120 * passed to the <CODE>QueueRequestor</CODE> constructor.
121 *
122 * @exception JMSException if the JMS provider fails to close the
123 * <CODE>QueueRequestor</CODE> due to some internal
124 * error.
125 */
126
127 public void
128 close() throws JMSException {
129
130 // publisher and consumer created by constructor are implicitly closed.
131 session.close();
132 tempQueue.delete();
133 }
134 }