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 <CODE>Connection</CODE> object is a client's active connection to its JMS
  7. * provider. It typically allocates provider resources outside the Java virtual
  8. * machine (JVM).
  9. *
  10. * <P>Connections support concurrent use.
  11. *
  12. * <P>A connection serves several purposes:
  13. *
  14. * <UL>
  15. * <LI>It encapsulates an open connection with a JMS provider. It
  16. * typically represents an open TCP/IP socket between a client and
  17. * a provider service daemon.
  18. * <LI>Its creation is where client authentication takes place.
  19. * <LI>It can specify a unique client identifier.
  20. * <LI>It provides a <CODE>ConnectionMetaData</CODE> object.
  21. * <LI>It supports an optional <CODE>ExceptionListener</CODE> object.
  22. * </UL>
  23. *
  24. * <P>Because the creation of a connection involves setting up authentication
  25. * and communication, a connection is a relatively heavyweight
  26. * object. Most clients will do all their messaging with a single connection.
  27. * Other more advanced applications may use several connections. The JMS API
  28. * does
  29. * not architect a reason for using multiple connections; however, there may
  30. * be operational reasons for doing so.
  31. *
  32. * <P>A JMS client typically creates a connection, one or more sessions,
  33. * and a number of message producers and consumers. When a connection is
  34. * created, it is in stopped mode. That means that no messages are being
  35. * delivered.
  36. *
  37. * <P>It is typical to leave the connection in stopped mode until setup
  38. * is complete (that is, until all message consumers have been
  39. * created). At that point, the client calls
  40. * the connection's <CODE>start</CODE> method, and messages begin arriving at
  41. * the connection's consumers. This setup
  42. * convention minimizes any client confusion that may result from
  43. * asynchronous message delivery while the client is still in the process
  44. * of setting itself up.
  45. *
  46. * <P>A connection can be started immediately, and the setup can be done
  47. * afterwards. Clients that do this must be prepared to handle asynchronous
  48. * message delivery while they are still in the process of setting up.
  49. *
  50. * <P>A message producer can send messages while a connection is stopped.
  51. *
  52. * @version 1.0 - 3 August 1998
  53. * @author Mark Hapner
  54. * @author Rich Burridge
  55. *
  56. * @see javax.jms.ConnectionFactory
  57. * @see javax.jms.QueueConnection
  58. * @see javax.jms.TopicConnection
  59. */
  60. public interface Connection {
  61. /** Gets the client identifier for this connection.
  62. *
  63. * <P>This value is specific to the JMS provider. It is either preconfigured
  64. * by an administrator in a <CODE>ConnectionFactory</CODE> object
  65. * or assigned dynamically by the application by calling the
  66. * <code>setClientID</code> method.
  67. *
  68. *
  69. * @return the unique client identifier
  70. *
  71. * @exception JMSException if the JMS provider fails to return
  72. * the client ID for this connection due
  73. * to some internal error.
  74. *
  75. **/
  76. String
  77. getClientID() throws JMSException;
  78. /** Sets the client identifier for this connection.
  79. *
  80. * <P>The preferred way to assign a JMS client's client identifier is for
  81. * it to be configured in a client-specific <CODE>ConnectionFactory</CODE>
  82. * object and transparently assigned to the <CODE>Connection</CODE> object
  83. * it creates.
  84. *
  85. * <P>Alternatively, a client can set a connection's client identifier
  86. * using a provider-specific value. The facility to set a connection's
  87. * client identifier explicitly is not a mechanism for overriding the
  88. * identifier that has been administratively configured. It is provided
  89. * for the case where no administratively specified identifier exists.
  90. * If one does exist, an attempt to change it by setting it must throw an
  91. * <CODE>IllegalStateException</CODE>. If a client sets the client identifier
  92. * explicitly, it must do so immediately after it creates the connection
  93. * and before any other
  94. * action on the connection is taken. After this point, setting the
  95. * client identifier is a programming error that should throw an
  96. * <CODE>IllegalStateException</CODE>.
  97. *
  98. * <P>The purpose of the client identifier is to associate a connection and
  99. * its objects with a state maintained on behalf of the client by a
  100. * provider. The only such state identified by the JMS API is that required
  101. * to support durable subscriptions.
  102. *
  103. * <P>If another connection with the same <code>clientID</code> is already running when
  104. * this method is called, the JMS provider should detect the duplicate ID and throw
  105. * an <CODE>InvalidClientIDException</CODE>.
  106. *
  107. * @param clientID the unique client identifier
  108. *
  109. * @exception JMSException if the JMS provider fails to
  110. * set the client ID for this connection due
  111. * to some internal error.
  112. *
  113. * @exception InvalidClientIDException if the JMS client specifies an
  114. * invalid or duplicate client ID.
  115. * @exception IllegalStateException if the JMS client attempts to set
  116. * a connection's client ID at the wrong time or
  117. * when it has been administratively configured.
  118. */
  119. void
  120. setClientID(String clientID) throws JMSException;
  121. /** Gets the metadata for this connection.
  122. *
  123. * @return the connection metadata
  124. *
  125. * @exception JMSException if the JMS provider fails to
  126. * get the connection metadata for this connection.
  127. *
  128. * @see javax.jms.ConnectionMetaData
  129. */
  130. ConnectionMetaData
  131. getMetaData() throws JMSException;
  132. /**
  133. * Gets the <CODE>ExceptionListener</CODE> object for this connection.
  134. *
  135. * @return the <CODE>ExceptionListener</CODE> for this connection
  136. *
  137. * @exception JMSException if the JMS provider fails to
  138. * get the <CODE>ExceptionListener</CODE> for this
  139. * connection.
  140. */
  141. ExceptionListener
  142. getExceptionListener() throws JMSException;
  143. /** Sets an exception listener for this connection.
  144. *
  145. * <P>If a JMS provider detects a serious problem with a connection, it
  146. * informs the connection's <CODE>ExceptionListener</CODE>, if one has been
  147. * registered. It does this by calling the listener's
  148. * <CODE>onException</CODE> method, passing it a <CODE>JMSException</CODE>
  149. * object describing the problem.
  150. *
  151. * <P>An exception listener allows a client to be notified of a problem
  152. * asynchronously.
  153. * Some connections only consume messages, so they would have no other
  154. * way to learn their connection has failed.
  155. *
  156. * <P>A connection serializes execution of its
  157. * <CODE>ExceptionListener</CODE>.
  158. *
  159. * <P>A JMS provider should attempt to resolve connection problems
  160. * itself before it notifies the client of them.
  161. *
  162. * @param listener the exception listener
  163. *
  164. * @exception JMSException if the JMS provider fails to
  165. * set the exception listener for this connection.
  166. */
  167. void
  168. setExceptionListener(ExceptionListener listener) throws JMSException;
  169. /** Starts (or restarts) a connection's delivery of incoming messages.
  170. * A call to <CODE>start</CODE> on a connection that has already been
  171. * started is ignored.
  172. *
  173. * @exception JMSException if the JMS provider fails to start
  174. * message delivery due to some internal error.
  175. *
  176. * @see javax.jms.Connection#stop
  177. */
  178. void
  179. start() throws JMSException;
  180. /** Temporarily stops a connection's delivery of incoming messages.
  181. * Delivery can be restarted using the connection's <CODE>start</CODE>
  182. * method. When the connection is stopped,
  183. * delivery to all the connection's message consumers is inhibited:
  184. * synchronous receives block, and messages are not delivered to message
  185. * listeners.
  186. *
  187. * <P>This call blocks until receives and/or message listeners in progress
  188. * have completed.
  189. *
  190. * <P>Stopping a connection has no effect on its ability to send messages.
  191. * A call to <CODE>stop</CODE> on a connection that has already been
  192. * stopped is ignored.
  193. *
  194. * <P>A call to <CODE>stop</CODE> must not return until delivery of messages
  195. * has paused. This means that a client can rely on the fact that none of
  196. * its message listeners will be called and that all threads of control
  197. * waiting for <CODE>receive</CODE> calls to return will not return with a
  198. * message until the
  199. * connection is restarted. The receive timers for a stopped connection
  200. * continue to advance, so receives may time out while the connection is
  201. * stopped.
  202. *
  203. * <P>If message listeners are running when <CODE>stop</CODE> is invoked,
  204. * the <CODE>stop</CODE> call must
  205. * wait until all of them have returned before it may return. While these
  206. * message listeners are completing, they must have the full services of the
  207. * connection available to them.
  208. *
  209. * @exception JMSException if the JMS provider fails to stop
  210. * message delivery due to some internal error.
  211. *
  212. * @see javax.jms.Connection#start
  213. */
  214. void
  215. stop() throws JMSException;
  216. /** Closes the connection.
  217. *
  218. * <P>Since a provider typically allocates significant resources outside
  219. * the JVM on behalf of a connection, clients should close these resources
  220. * when they are not needed. Relying on garbage collection to eventually
  221. * reclaim these resources may not be timely enough.
  222. *
  223. * <P>There is no need to close the sessions, producers, and consumers
  224. * of a closed connection.
  225. *
  226. * <P>Closing a connection causes all temporary destinations to be
  227. * deleted.
  228. *
  229. * <P>When this method is invoked, it should not return until message
  230. * processing has been shut down in an orderly fashion. This means that all
  231. * message
  232. * listeners that may have been running have returned, and that all pending
  233. * receives have returned. A close terminates all pending message receives
  234. * on the connection's sessions' consumers. The receives may return with a
  235. * message or with null, depending on whether there was a message available
  236. * at the time of the close. If one or more of the connection's sessions'
  237. * message listeners is processing a message at the time when connection
  238. * <CODE>close</CODE> is invoked, all the facilities of the connection and
  239. * its sessions must remain available to those listeners until they return
  240. * control to the JMS provider.
  241. *
  242. * <P>Closing a connection causes any of its sessions' transactions
  243. * in progress to be rolled back. In the case where a session's
  244. * work is coordinated by an external transaction manager, a session's
  245. * <CODE>commit</CODE> and <CODE>rollback</CODE> methods are
  246. * not used and the result of a closed session's work is determined
  247. * later by the transaction manager.
  248. *
  249. * Closing a connection does NOT force an
  250. * acknowledgment of client-acknowledged sessions.
  251. *
  252. * <P>Invoking the <CODE>acknowledge</CODE> method of a received message
  253. * from a closed connection's session must throw an
  254. * <CODE>IllegalStateException</CODE>. Closing a closed connection must
  255. * NOT throw an exception.
  256. *
  257. * @exception JMSException if the JMS provider fails to close the
  258. * connection due to some internal error. For
  259. * example, a failure to release resources
  260. * or to close a socket connection can cause
  261. * this exception to be thrown.
  262. *
  263. */
  264. void
  265. close() throws JMSException;
  266. }