1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.jms;
  6. /** The <CODE>QueueRequestor</CODE> helper class simplifies
  7. * making service requests.
  8. *
  9. * <P>The <CODE>QueueRequestor</CODE> constructor is given a non-transacted
  10. * <CODE>QueueSession</CODE> and a destination <CODE>Queue</CODE>. It creates a
  11. * <CODE>TemporaryQueue</CODE> for the responses and provides a
  12. * <CODE>request</CODE> method that sends the request message and waits
  13. * for its reply.
  14. *
  15. * <P>This is a basic request/reply abstraction that should be sufficient
  16. * for most uses. JMS providers and clients are free to create more
  17. * sophisticated versions.
  18. *
  19. * @version 1.0 - 8 July 1998
  20. * @author Mark Hapner
  21. * @author Rich Burridge
  22. *
  23. * @see javax.jms.TopicRequestor
  24. */
  25. public class QueueRequestor {
  26. QueueSession session; // The queue session the queue belongs to.
  27. Queue queue; // The queue to perform the request/reply on.
  28. TemporaryQueue tempQueue;
  29. QueueSender sender;
  30. QueueReceiver receiver;
  31. /** Constructor for the <CODE>QueueRequestor</CODE> class.
  32. *
  33. * <P>This implementation assumes the session parameter to be non-transacted,
  34. * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
  35. * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
  36. *
  37. * @param session the <CODE>QueueSession</CODE> the queue belongs to
  38. * @param queue the queue to perform the request/reply call on
  39. *
  40. * @exception JMSException if the JMS provider fails to create the
  41. * <CODE>QueueRequestor</CODE> due to some internal
  42. * error.
  43. * @exception InvalidDestinationException if an invalid queue is specified.
  44. */
  45. public
  46. QueueRequestor(QueueSession session, Queue queue) throws JMSException {
  47. this.session = session;
  48. this.queue = queue;
  49. tempQueue = session.createTemporaryQueue();
  50. sender = session.createSender(queue);
  51. receiver = session.createReceiver(tempQueue);
  52. }
  53. /** Sends a request and waits for a reply. The temporary queue is used for
  54. * the <CODE>JMSReplyTo</CODE> destination, and only one reply per request
  55. * is expected.
  56. *
  57. * @param message the message to send
  58. *
  59. * @return the reply message
  60. *
  61. * @exception JMSException if the JMS provider fails to complete the
  62. * request due to some internal error.
  63. */
  64. public Message
  65. request(Message message) throws JMSException {
  66. message.setJMSReplyTo(tempQueue);
  67. sender.send(message);
  68. return (receiver.receive());
  69. }
  70. /** Closes the <CODE>QueueRequestor</CODE> and its session.
  71. *
  72. * <P>Since a provider may allocate some resources on behalf of a
  73. * <CODE>QueueRequestor</CODE> outside the Java virtual machine, clients
  74. * should close them when they
  75. * are not needed. Relying on garbage collection to eventually reclaim
  76. * these resources may not be timely enough.
  77. *
  78. * <P>Note that this method closes the <CODE>QueueSession</CODE> object
  79. * passed to the <CODE>QueueRequestor</CODE> constructor.
  80. *
  81. * @exception JMSException if the JMS provider fails to close the
  82. * <CODE>QueueRequestor</CODE> due to some internal
  83. * error.
  84. */
  85. public void
  86. close() throws JMSException {
  87. // publisher and consumer created by constructor are implicitly closed.
  88. session.close();
  89. tempQueue.delete();
  90. }
  91. }