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. import java.io.Serializable;
  7. /** <P>A <CODE>Session</CODE> object is a single-threaded context for producing and consuming
  8. * messages. Although it may allocate provider resources outside the Java
  9. * virtual machine (JVM), it is considered a lightweight JMS object.
  10. *
  11. * <P>A session serves several purposes:
  12. *
  13. * <UL>
  14. * <LI>It is a factory for its message producers and consumers.
  15. * <LI>It supplies provider-optimized message factories.
  16. * <LI>It supports a single series of transactions that combine work
  17. * spanning its producers and consumers into atomic units.
  18. * <LI>It defines a serial order for the messages it consumes and
  19. * the messages it produces.
  20. * <LI>It retains messages it consumes until they have been
  21. * acknowledged.
  22. * <LI>It serializes execution of message listeners registered with
  23. * its message consumers.
  24. * </UL>
  25. *
  26. * <P>A session can create and service multiple message producers and
  27. * consumers.
  28. *
  29. * <P>One typical use is to have a thread block on a synchronous
  30. * <CODE>MessageConsumer</CODE> until a message arrives. The thread may then
  31. * use one or more of the <CODE>Session</CODE>'s <CODE>MessageProducer</CODE>s.
  32. *
  33. * <P>If a client desires to have one thread produce messages while others
  34. * consume them, the client should use a separate session for its producing
  35. * thread.
  36. *
  37. * <P>Once a connection has been started, any session with one or more
  38. * registered message listeners is dedicated to the thread of control that
  39. * delivers messages to it. It is erroneous for client code to use this session
  40. * or any of its constituent objects from another thread of control. The
  41. * only exception to this rule is the use of the session or connection
  42. * <CODE>close</CODE> method.
  43. *
  44. * <P>It should be easy for most clients to partition their work naturally
  45. * into sessions. This model allows clients to start simply and incrementally
  46. * add message processing complexity as their need for concurrency grows.
  47. *
  48. * <P>The <CODE>close</CODE> method is the only session method that can be
  49. * called while some other session method is being executed in another thread.
  50. *
  51. * <P>A session may be specified as transacted. Each transacted
  52. * session supports a single series of transactions. Each transaction groups
  53. * a set of message sends and a set of message receives into an atomic unit
  54. * of work. In effect, transactions organize a session's input message
  55. * stream and output message stream into series of atomic units. When a
  56. * transaction commits, its atomic unit of input is acknowledged and its
  57. * associated atomic unit of output is sent. If a transaction rollback is
  58. * done, the transaction's sent messages are destroyed and the session's input
  59. * is automatically recovered.
  60. *
  61. * <P>The content of a transaction's input and output units is simply those
  62. * messages that have been produced and consumed within the session's current
  63. * transaction.
  64. *
  65. * <P>A transaction is completed using either its session's <CODE>commit</CODE>
  66. * method or its session's <CODE>rollback</CODE> method. The completion of a
  67. * session's current transaction automatically begins the next. The result is
  68. * that a transacted session always has a current transaction within which its
  69. * work is done.
  70. *
  71. * <P>The Java Transaction Service (JTS) or some other transaction monitor may
  72. * be used to combine a session's transaction with transactions on other
  73. * resources (databases, other JMS sessions, etc.). Since Java distributed
  74. * transactions are controlled via the Java Transaction API (JTA), use of the
  75. * session's <CODE>commit</CODE> and <CODE>rollback</CODE> methods in
  76. * this context is prohibited.
  77. *
  78. * <P>The JMS API does not require support for JTA; however, it does define
  79. * how a provider supplies this support.
  80. *
  81. * <P>Although it is also possible for a JMS client to handle distributed
  82. * transactions directly, it is unlikely that many JMS clients will do this.
  83. * Support for JTA in the JMS API is targeted at systems vendors who will be
  84. * integrating the JMS API into their application server products.
  85. *
  86. * @version 1.0 - 6 August 1998
  87. * @author Mark Hapner
  88. * @author Rich Burridge
  89. *
  90. * @see javax.jms.QueueSession
  91. * @see javax.jms.TopicSession
  92. * @see javax.jms.XASession
  93. */
  94. public interface Session extends Runnable {
  95. /** With this acknowledgment mode, the session automatically acknowledges
  96. * a client's receipt of a message either when the session has successfully
  97. * returned from a call to <CODE>receive</CODE> or when the message
  98. * listener the session has called to process the message successfully
  99. * returns.
  100. */
  101. static final int AUTO_ACKNOWLEDGE = 1;
  102. /** With this acknowledgment mode, the client acknowledges a consumed
  103. * message by calling the message's <CODE>acknowledge</CODE> method.
  104. * Acknowledging a consumed message acknowledges all messages that the
  105. * session has consumed.
  106. *
  107. * <P>When client acknowledgment mode is used, a client may build up a
  108. * large number of unacknowledged messages while attempting to process
  109. * them. A JMS provider should provide administrators with a way to
  110. * limit client overrun so that clients are not driven to resource
  111. * exhaustion and ensuing failure when some resource they are using
  112. * is temporarily blocked.
  113. *
  114. * @see javax.jms.Message#acknowledge()
  115. */
  116. static final int CLIENT_ACKNOWLEDGE = 2;
  117. /** This acknowledgment mode instructs the session to lazily acknowledge
  118. * the delivery of messages. This is likely to result in the delivery of
  119. * some duplicate messages if the JMS provider fails, so it should only be
  120. * used by consumers that can tolerate duplicate messages. Use of this
  121. * mode can reduce session overhead by minimizing the work the
  122. * session does to prevent duplicates.
  123. */
  124. static final int DUPS_OK_ACKNOWLEDGE = 3;
  125. /** Creates a <CODE>BytesMessage</CODE> object. A <CODE>BytesMessage</CODE>
  126. * object is used to send a message containing a stream of uninterpreted
  127. * bytes.
  128. *
  129. * @exception JMSException if the JMS provider fails to create this message
  130. * due to some internal error.
  131. */
  132. BytesMessage
  133. createBytesMessage() throws JMSException;
  134. /** Creates a <CODE>MapMessage</CODE> object. A <CODE>MapMessage</CODE>
  135. * object is used to send a self-defining set of name-value pairs, where
  136. * names are <CODE>String</CODE> objects and values are primitive values
  137. * in the Java programming language.
  138. *
  139. * @exception JMSException if the JMS provider fails to create this message
  140. * due to some internal error.
  141. */
  142. MapMessage
  143. createMapMessage() throws JMSException;
  144. /** Creates a <CODE>Message</CODE> object. The <CODE>Message</CODE>
  145. * interface is the root interface of all JMS messages. A
  146. * <CODE>Message</CODE> object holds all the
  147. * standard message header information. It can be sent when a message
  148. * containing only header information is sufficient.
  149. *
  150. * @exception JMSException if the JMS provider fails to create this message
  151. * due to some internal error.
  152. */
  153. Message
  154. createMessage() throws JMSException;
  155. /** Creates an <CODE>ObjectMessage</CODE> object. An
  156. * <CODE>ObjectMessage</CODE> object is used to send a message
  157. * that contains a serializable Java object.
  158. *
  159. * @exception JMSException if the JMS provider fails to create this message
  160. * due to some internal error.
  161. */
  162. ObjectMessage
  163. createObjectMessage() throws JMSException;
  164. /** Creates an initialized <CODE>ObjectMessage</CODE> object. An
  165. * <CODE>ObjectMessage</CODE> object is used
  166. * to send a message that contains a serializable Java object.
  167. *
  168. * @param object the object to use to initialize this message
  169. *
  170. * @exception JMSException if the JMS provider fails to create this message
  171. * due to some internal error.
  172. */
  173. ObjectMessage
  174. createObjectMessage(Serializable object) throws JMSException;
  175. /** Creates a <CODE>StreamMessage</CODE> object. A
  176. * <CODE>StreamMessage</CODE> object is used to send a
  177. * self-defining stream of primitive values in the Java programming
  178. * language.
  179. *
  180. * @exception JMSException if the JMS provider fails to create this message
  181. * due to some internal error.
  182. */
  183. StreamMessage
  184. createStreamMessage() throws JMSException;
  185. /** Creates a <CODE>TextMessage</CODE> object. A <CODE>TextMessage</CODE>
  186. * object is used to send a message containing a <CODE>String</CODE>
  187. * object.
  188. *
  189. * @exception JMSException if the JMS provider fails to create this message
  190. * due to some internal error.
  191. */
  192. TextMessage
  193. createTextMessage() throws JMSException;
  194. /** Creates an initialized <CODE>TextMessage</CODE> object. A
  195. * <CODE>TextMessage</CODE> object is used to send
  196. * a message containing a <CODE>String</CODE>.
  197. *
  198. * @param text the string used to initialize this message
  199. *
  200. * @exception JMSException if the JMS provider fails to create this message
  201. * due to some internal error.
  202. */
  203. TextMessage
  204. createTextMessage(String text) throws JMSException;
  205. /** Indicates whether the session is in transacted mode.
  206. *
  207. * @return true if the session is in transacted mode
  208. *
  209. * @exception JMSException if the JMS provider fails to return the
  210. * transaction mode due to some internal error.
  211. */
  212. boolean
  213. getTransacted() throws JMSException;
  214. /** Commits all messages done in this transaction and releases any locks
  215. * currently held.
  216. *
  217. * @exception JMSException if the JMS provider fails to commit the
  218. * transaction due to some internal error.
  219. * @exception TransactionRolledBackException if the transaction
  220. * is rolled back due to some internal error
  221. * during commit.
  222. * @exception IllegalStateException if the method is not called by a
  223. * transacted session.
  224. */
  225. void
  226. commit() throws JMSException;
  227. /** Rolls back any messages done in this transaction and releases any locks
  228. * currently held.
  229. *
  230. * @exception JMSException if the JMS provider fails to roll back the
  231. * transaction due to some internal error.
  232. * @exception IllegalStateException if the method is not called by a
  233. * transacted session.
  234. *
  235. */
  236. void
  237. rollback() throws JMSException;
  238. /** Closes the session.
  239. *
  240. * <P>Since a provider may allocate some resources on behalf of a session
  241. * outside the JVM, clients should close the resources when they are not
  242. * needed.
  243. * Relying on garbage collection to eventually reclaim these resources
  244. * may not be timely enough.
  245. *
  246. * <P>There is no need to close the producers and consumers
  247. * of a closed session.
  248. *
  249. * <P> This call will block until a <CODE>receive</CODE> call or message
  250. * listener in progress has completed. A blocked message consumer
  251. * <CODE>receive</CODE> call returns <CODE>null</CODE> when this session
  252. * is closed.
  253. *
  254. * <P>Closing a transacted session must roll back the transaction
  255. * in progress.
  256. *
  257. * <P>This method is the only <CODE>Session</CODE> method that can
  258. * be called concurrently.
  259. *
  260. * <P>Invoking any other <CODE>Session</CODE> method on a closed session
  261. * must throw a <CODE>JMSException.IllegalStateException</CODE>. Closing a
  262. * closed session must <I>not</I> throw an exception.
  263. *
  264. * @exception JMSException if the JMS provider fails to close the
  265. * session due to some internal error.
  266. */
  267. void
  268. close() throws JMSException;
  269. /** Stops message delivery in this session, and restarts message delivery
  270. * with the oldest unacknowledged message.
  271. *
  272. * <P>All consumers deliver messages in a serial order.
  273. * Acknowledging a received message automatically acknowledges all
  274. * messages that have been delivered to the client.
  275. *
  276. * <P>Restarting a session causes it to take the following actions:
  277. *
  278. * <UL>
  279. * <LI>Stop message delivery
  280. * <LI>Mark all messages that might have been delivered but not
  281. * acknowledged as "redelivered"
  282. * <LI>Restart the delivery sequence including all unacknowledged
  283. * messages that had been previously delivered. Redelivered messages
  284. * do not have to be delivered in
  285. * exactly their original delivery order.
  286. * </UL>
  287. *
  288. * @exception JMSException if the JMS provider fails to stop and restart
  289. * message delivery due to some internal error.
  290. * @exception IllegalStateException if the method is called by a
  291. * transacted session.
  292. */
  293. void
  294. recover() throws JMSException;
  295. /** Returns the session's distinguished message listener (optional).
  296. *
  297. * @return the message listener associated with this session
  298. *
  299. * @exception JMSException if the JMS provider fails to get the message
  300. * listener due to an internal error.
  301. *
  302. * @see javax.jms.Session#setMessageListener
  303. * @see javax.jms.ServerSessionPool
  304. * @see javax.jms.ServerSession
  305. */
  306. MessageListener
  307. getMessageListener() throws JMSException;
  308. /** Sets the session's distinguished message listener (optional).
  309. *
  310. * <P>When the distinguished message listener is set, no other form of
  311. * message receipt in the session can
  312. * be used; however, all forms of sending messages are still supported.
  313. *
  314. * <P>This is an expert facility not used by regular JMS clients.
  315. *
  316. * @param listener the message listener to associate with this session
  317. *
  318. * @exception JMSException if the JMS provider fails to set the message
  319. * listener due to an internal error.
  320. *
  321. * @see javax.jms.Session#getMessageListener
  322. * @see javax.jms.ServerSessionPool
  323. * @see javax.jms.ServerSession
  324. */
  325. void
  326. setMessageListener(MessageListener listener) throws JMSException;
  327. /**
  328. * Optional operation, intended to be used only by Application Servers,
  329. * not by ordinary JMS clients.
  330. *
  331. * @see javax.jms.ServerSession
  332. */
  333. public void run();
  334. }