001: /*
002: * @(#)TopicRequestor.java 1.20 02/04/09
003: *
004: * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL.
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license terms.
009: *
010: */
011:
012: package javax.jms;
013:
014: /** The <CODE>TopicRequestor</CODE> helper class simplifies
015: * making service requests.
016: *
017: * <P>The <CODE>TopicRequestor</CODE> constructor is given a non-transacted
018: * <CODE>TopicSession</CODE> and a destination <CODE>Topic</CODE>. It creates a
019: * <CODE>TemporaryTopic</CODE> for the responses and provides a
020: * <CODE>request</CODE> method that sends the request message and waits
021: * for its reply.
022: *
023: * <P>This is a basic request/reply abstraction that should be sufficient
024: * for most uses. JMS providers and clients are free to create more
025: * sophisticated versions.
026: *
027: * @version 1.0 - 8 July 1998
028: * @author Mark Hapner
029: * @author Rich Burridge
030: *
031: * @see javax.jms.QueueRequestor
032: */
033:
034: public class TopicRequestor {
035:
036: TopicSession session; // The topic session the topic belongs to.
037: Topic topic; // The topic to perform the request/reply on.
038: TemporaryTopic tempTopic;
039: TopicPublisher publisher;
040: TopicSubscriber subscriber;
041:
042: /** Constructor for the <CODE>TopicRequestor</CODE> class.
043: *
044: * <P>This implementation assumes the session parameter to be non-transacted,
045: * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
046: * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
047: *
048: * @param session the <CODE>TopicSession</CODE> the topic belongs to
049: * @param topic the topic to perform the request/reply call on
050: *
051: * @exception JMSException if the JMS provider fails to create the
052: * <CODE>TopicRequestor</CODE> due to some internal
053: * error.
054: * @exception InvalidDestinationException if an invalid topic is specified.
055: */
056:
057: public TopicRequestor(TopicSession session, Topic topic)
058: throws JMSException {
059: this .session = session;
060: this .topic = topic;
061: tempTopic = session.createTemporaryTopic();
062: publisher = session.createPublisher(topic);
063: subscriber = session.createSubscriber(tempTopic);
064: }
065:
066: /** Sends a request and waits for a reply. The temporary topic is used for
067: * the <CODE>JMSReplyTo</CODE> destination; the first reply is returned,
068: * and any following replies are discarded.
069: *
070: * @param message the message to send
071: *
072: * @return the reply message
073: *
074: * @exception JMSException if the JMS provider fails to complete the
075: * request due to some internal error.
076: */
077:
078: public Message request(Message message) throws JMSException {
079: message.setJMSReplyTo(tempTopic);
080: publisher.publish(message);
081: return (subscriber.receive());
082: }
083:
084: /** Closes the <CODE>TopicRequestor</CODE> and its session.
085: *
086: * <P>Since a provider may allocate some resources on behalf of a
087: * <CODE>TopicRequestor</CODE> outside the Java virtual machine, clients
088: * should close them when they
089: * are not needed. Relying on garbage collection to eventually reclaim
090: * these resources may not be timely enough.
091: *
092: * <P>Note that this method closes the <CODE>TopicSession</CODE> object
093: * passed to the <CODE>TopicRequestor</CODE> constructor.
094: *
095: * @exception JMSException if the JMS provider fails to close the
096: * <CODE>TopicRequestor</CODE> due to some internal
097: * error.
098: */
099:
100: public void close() throws JMSException {
101:
102: // publisher and consumer created by constructor are implicitly closed.
103: session.close();
104: tempTopic.delete();
105: }
106: }
|