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. /** A client uses a <CODE>MessageConsumer</CODE> object to receive messages
  7. * from a destination. A <CODE>MessageConsumer</CODE> object is created by
  8. * passing a <CODE>Destination</CODE> object to a message-consumer creation
  9. * method supplied by a session.
  10. *
  11. * <P><CODE>MessageConsumer</CODE> is the parent interface for all message
  12. * consumers.
  13. *
  14. * <P>A message consumer can be created with a message selector. A message
  15. * selector allows
  16. * the client to restrict the messages delivered to the message consumer to
  17. * those that match the selector.
  18. *
  19. * <P>A client may either synchronously receive a message consumer's messages
  20. * or have the consumer asynchronously deliver them as they arrive.
  21. *
  22. * <P>For synchronous receipt, a client can request the next message from a
  23. * message consumer using one of its <CODE>receive</CODE> methods. There are
  24. * several variations of <CODE>receive</CODE> that allow a
  25. * client to poll or wait for the next message.
  26. *
  27. * <P>For asynchronous delivery, a client can register a
  28. * <CODE>MessageListener</CODE> object with a message consumer.
  29. * As messages arrive at the message consumer, it delivers them by calling the
  30. * <CODE>MessageListener</CODE>'s <CODE>onMessage</CODE> method.
  31. *
  32. * <P>It is a client programming error for a <CODE>MessageListener</CODE> to
  33. * throw an exception.
  34. *
  35. * @version 1.0 - 13 March 1998
  36. * @author Mark Hapner
  37. * @author Rich Burridge
  38. *
  39. * @see javax.jms.QueueReceiver
  40. * @see javax.jms.TopicSubscriber
  41. * @see javax.jms.Session
  42. */
  43. public interface MessageConsumer {
  44. /** Gets this message consumer's message selector expression.
  45. *
  46. * @return this message consumer's message selector, or null if no
  47. * message selector exists for the message consumer (that is, if
  48. * the message selector was not set or was set to null or the
  49. * empty string)
  50. *
  51. * @exception JMSException if the JMS provider fails to get the message
  52. * selector due to some internal error.
  53. */
  54. String
  55. getMessageSelector() throws JMSException;
  56. /** Gets the message consumer's <CODE>MessageListener</CODE>.
  57. *
  58. * @return the listener for the message consumer, or null if no listener
  59. * is set
  60. *
  61. * @exception JMSException if the JMS provider fails to get the message
  62. * listener due to some internal error.
  63. * @see javax.jms.MessageConsumer#setMessageListener
  64. */
  65. MessageListener
  66. getMessageListener() throws JMSException;
  67. /** Sets the message consumer's <CODE>MessageListener</CODE>.
  68. *
  69. * <P>Setting the message listener to null is the equivalent of
  70. * unsetting the message listener for the message consumer.
  71. *
  72. * <P>The effect of calling <CODE>MessageConsumer.setMessageListener</CODE>
  73. * while messages are being consumed by an existing listener
  74. * or the consumer is being used to consume messages synchronously
  75. * is undefined.
  76. *
  77. * @param listener the listener to which the messages are to be
  78. * delivered
  79. *
  80. * @exception JMSException if the JMS provider fails to set the message
  81. * listener due to some internal error.
  82. * @see javax.jms.MessageConsumer#getMessageListener
  83. */
  84. void
  85. setMessageListener(MessageListener listener) throws JMSException;
  86. /** Receives the next message produced for this message consumer.
  87. *
  88. * <P>This call blocks indefinitely until a message is produced
  89. * or until this message consumer is closed.
  90. *
  91. * <P>If this <CODE>receive</CODE> is done within a transaction, the
  92. * consumer retains the message until the transaction commits.
  93. *
  94. * @return the next message produced for this message consumer, or
  95. * null if this message consumer is concurrently closed
  96. *
  97. * @exception JMSException if the JMS provider fails to receive the next
  98. * message due to some internal error.
  99. *
  100. */
  101. Message
  102. receive() throws JMSException;
  103. /** Receives the next message that arrives within the specified
  104. * timeout interval.
  105. *
  106. * <P>This call blocks until a message arrives, the
  107. * timeout expires, or this message consumer is closed.
  108. * A <CODE>timeout</CODE> of zero never expires, and the call blocks
  109. * indefinitely.
  110. *
  111. * @param timeout the timeout value (in milliseconds)
  112. *
  113. * @return the next message produced for this message consumer, or
  114. * null if the timeout expires or this message consumer is concurrently
  115. * closed
  116. *
  117. * @exception JMSException if the JMS provider fails to receive the next
  118. * message due to some internal error.
  119. */
  120. Message
  121. receive(long timeout) throws JMSException;
  122. /** Receives the next message if one is immediately available.
  123. *
  124. * @return the next message produced for this message consumer, or
  125. * null if one is not available
  126. *
  127. * @exception JMSException if the JMS provider fails to receive the next
  128. * message due to some internal error.
  129. */
  130. Message
  131. receiveNoWait() throws JMSException;
  132. /** Closes the message consumer.
  133. *
  134. * <P>Since a provider may allocate some resources on behalf of a
  135. * <CODE>MessageConsumer</CODE> outside the Java virtual machine, clients
  136. * should close them when they
  137. * are not needed. Relying on garbage collection to eventually reclaim
  138. * these resources may not be timely enough.
  139. *
  140. * <P>This call blocks until a <CODE>receive</CODE> or message listener in
  141. * progress has completed. A blocked message consumer <CODE>receive</CODE>
  142. * call
  143. * returns null when this message consumer is closed.
  144. *
  145. * @exception JMSException if the JMS provider fails to close the consumer
  146. * due to some internal error.
  147. */
  148. void
  149. close() throws JMSException;
  150. }