001: /*
002: * @(#)QueueRequestor.java 1.21 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>QueueRequestor</CODE> helper class simplifies
015: * making service requests.
016: *
017: * <P>The <CODE>QueueRequestor</CODE> constructor is given a non-transacted
018: * <CODE>QueueSession</CODE> and a destination <CODE>Queue</CODE>. It creates a
019: * <CODE>TemporaryQueue</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.TopicRequestor
032: */
033:
034: public class QueueRequestor {
035:
036: QueueSession session; // The queue session the queue belongs to.
037: Queue queue; // The queue to perform the request/reply on.
038: TemporaryQueue tempQueue;
039: QueueSender sender;
040: QueueReceiver receiver;
041:
042: /** Constructor for the <CODE>QueueRequestor</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>QueueSession</CODE> the queue belongs to
049: * @param queue the queue to perform the request/reply call on
050: *
051: * @exception JMSException if the JMS provider fails to create the
052: * <CODE>QueueRequestor</CODE> due to some internal
053: * error.
054: * @exception InvalidDestinationException if an invalid queue is specified.
055: */
056:
057: public QueueRequestor(QueueSession session, Queue queue)
058: throws JMSException {
059: this .session = session;
060: this .queue = queue;
061: tempQueue = session.createTemporaryQueue();
062: sender = session.createSender(queue);
063: receiver = session.createReceiver(tempQueue);
064: }
065:
066: /** Sends a request and waits for a reply. The temporary queue is used for
067: * the <CODE>JMSReplyTo</CODE> destination, and only one reply per request
068: * is expected.
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(tempQueue);
080: sender.send(message);
081: return (receiver.receive());
082: }
083:
084: /** Closes the <CODE>QueueRequestor</CODE> and its session.
085: *
086: * <P>Since a provider may allocate some resources on behalf of a
087: * <CODE>QueueRequestor</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>QueueSession</CODE> object
093: * passed to the <CODE>QueueRequestor</CODE> constructor.
094: *
095: * @exception JMSException if the JMS provider fails to close the
096: * <CODE>QueueRequestor</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: tempQueue.delete();
105: }
106: }
|