KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jms > QueueRequestor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package javax.jms;
26
27 /** The <CODE>QueueRequestor</CODE> helper class simplifies
28   * making service requests.
29   *
30   * <P>The <CODE>QueueRequestor</CODE> constructor is given a non-transacted
31   * <CODE>QueueSession</CODE> and a destination <CODE>Queue</CODE>. It creates a
32   * <CODE>TemporaryQueue</CODE> for the responses and provides a
33   * <CODE>request</CODE> method that sends the request message and waits
34   * for its reply.
35   *
36   * <P>This is a basic request/reply abstraction that should be sufficient
37   * for most uses. JMS providers and clients are free to create more
38   * sophisticated versions.
39   *
40   * @version 1.0 - 8 July 1998
41   * @author Mark Hapner
42   * @author Rich Burridge
43   *
44   * @see javax.jms.TopicRequestor
45   */

46
47 public class QueueRequestor {
48
49     QueueSession JavaDoc session; // The queue session the queue belongs to.
50
Queue JavaDoc queue; // The queue to perform the request/reply on.
51
TemporaryQueue JavaDoc tempQueue;
52     QueueSender JavaDoc sender;
53     QueueReceiver JavaDoc receiver;
54
55
56     /** Constructor for the <CODE>QueueRequestor</CODE> class.
57       *
58       * <P>This implementation assumes the session parameter to be non-transacted,
59       * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
60       * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
61       *
62       * @param session the <CODE>QueueSession</CODE> the queue belongs to
63       * @param queue the queue to perform the request/reply call on
64       *
65       * @exception JMSException if the JMS provider fails to create the
66       * <CODE>QueueRequestor</CODE> due to some internal
67       * error.
68       * @exception InvalidDestinationException if an invalid queue is specified.
69       */

70
71     public
72     QueueRequestor(QueueSession JavaDoc session, Queue JavaDoc queue) throws JMSException JavaDoc {
73         this.session = session;
74         this.queue = queue;
75         tempQueue = session.createTemporaryQueue();
76         sender = session.createSender(queue);
77         receiver = session.createReceiver(tempQueue);
78     }
79
80
81     /** Sends a request and waits for a reply. The temporary queue is used for
82       * the <CODE>JMSReplyTo</CODE> destination, and only one reply per request
83       * is expected.
84       *
85       * @param message the message to send
86       *
87       * @return the reply message
88       *
89       * @exception JMSException if the JMS provider fails to complete the
90       * request due to some internal error.
91       */

92
93     public Message JavaDoc
94     request(Message JavaDoc message) throws JMSException JavaDoc {
95     message.setJMSReplyTo(tempQueue);
96     sender.send(message);
97     return (receiver.receive());
98     }
99
100
101     /** Closes the <CODE>QueueRequestor</CODE> and its session.
102       *
103       * <P>Since a provider may allocate some resources on behalf of a
104       * <CODE>QueueRequestor</CODE> outside the Java virtual machine, clients
105       * should close them when they
106       * are not needed. Relying on garbage collection to eventually reclaim
107       * these resources may not be timely enough.
108       *
109       * <P>Note that this method closes the <CODE>QueueSession</CODE> object
110       * passed to the <CODE>QueueRequestor</CODE> constructor.
111       *
112       * @exception JMSException if the JMS provider fails to close the
113       * <CODE>QueueRequestor</CODE> due to some internal
114       * error.
115       */

116
117     public void
118     close() throws JMSException JavaDoc {
119
120     // publisher and consumer created by constructor are implicitly closed.
121
session.close();
122         tempQueue.delete();
123     }
124 }
125
Popular Tags