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>TopicRequestor</CODE> helper class simplifies
  7. * making service requests.
  8. *
  9. * <P>The <CODE>TopicRequestor</CODE> constructor is given a non-transacted
  10. * <CODE>TopicSession</CODE> and a destination <CODE>Topic</CODE>. It creates a
  11. * <CODE>TemporaryTopic</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.QueueRequestor
  24. */
  25. public class TopicRequestor {
  26. TopicSession session; // The topic session the topic belongs to.
  27. Topic topic; // The topic to perform the request/reply on.
  28. TemporaryTopic tempTopic;
  29. TopicPublisher publisher;
  30. TopicSubscriber subscriber;
  31. /** Constructor for the <CODE>TopicRequestor</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>TopicSession</CODE> the topic belongs to
  38. * @param topic the topic to perform the request/reply call on
  39. *
  40. * @exception JMSException if the JMS provider fails to create the
  41. * <CODE>TopicRequestor</CODE> due to some internal
  42. * error.
  43. * @exception InvalidDestinationException if an invalid topic is specified.
  44. */
  45. public
  46. TopicRequestor(TopicSession session, Topic topic) throws JMSException {
  47. this.session = session;
  48. this.topic = topic;
  49. tempTopic = session.createTemporaryTopic();
  50. publisher = session.createPublisher(topic);
  51. subscriber = session.createSubscriber(tempTopic);
  52. }
  53. /** Sends a request and waits for a reply. The temporary topic is used for
  54. * the <CODE>JMSReplyTo</CODE> destination; the first reply is returned,
  55. * and any following replies are discarded.
  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(tempTopic);
  67. publisher.publish(message);
  68. return(subscriber.receive());
  69. }
  70. /** Closes the <CODE>TopicRequestor</CODE> and its session.
  71. *
  72. * <P>Since a provider may allocate some resources on behalf of a
  73. * <CODE>TopicRequestor</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>TopicSession</CODE> object
  79. * passed to the <CODE>TopicRequestor</CODE> constructor.
  80. *
  81. * @exception JMSException if the JMS provider fails to close the
  82. * <CODE>TopicRequestor</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. tempTopic.delete();
  90. }
  91. }