KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jms > TopicRequestor


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

45
46 public class TopicRequestor {
47
48     TopicSession JavaDoc session; // The topic session the topic belongs to.
49
Topic JavaDoc topic; // The topic to perform the request/reply on.
50
TemporaryTopic JavaDoc tempTopic;
51     TopicPublisher JavaDoc publisher;
52     TopicSubscriber JavaDoc subscriber;
53
54
55     /** Constructor for the <CODE>TopicRequestor</CODE> class.
56       *
57       * <P>This implementation assumes the session parameter to be non-transacted,
58       * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
59       * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
60       *
61       * @param session the <CODE>TopicSession</CODE> the topic belongs to
62       * @param topic the topic to perform the request/reply call on
63       *
64       * @exception JMSException if the JMS provider fails to create the
65       * <CODE>TopicRequestor</CODE> due to some internal
66       * error.
67       * @exception InvalidDestinationException if an invalid topic is specified.
68       */

69
70     public
71     TopicRequestor(TopicSession JavaDoc session, Topic JavaDoc topic) throws JMSException JavaDoc {
72     this.session = session;
73     this.topic = topic;
74         tempTopic = session.createTemporaryTopic();
75         publisher = session.createPublisher(topic);
76         subscriber = session.createSubscriber(tempTopic);
77     }
78
79
80     /** Sends a request and waits for a reply. The temporary topic is used for
81       * the <CODE>JMSReplyTo</CODE> destination; the first reply is returned,
82       * and any following replies are discarded.
83       *
84       * @param message the message to send
85       *
86       * @return the reply message
87       *
88       * @exception JMSException if the JMS provider fails to complete the
89       * request due to some internal error.
90       */

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

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