Connection object is a client's active connection to its JMS
provider. It typically allocates provider resources outside the Java virtual
machine (JVM).
Connections support concurrent use.
A connection serves several purposes:
ConnectionMetaData object.
ExceptionListener object.
Because the creation of a connection involves setting up authentication and communication, a connection is a relatively heavyweight object. Most clients will do all their messaging with a single connection. Other more advanced applications may use several connections. The JMS API does not architect a reason for using multiple connections; however, there may be operational reasons for doing so.
A JMS client typically creates a connection, one or more sessions, and a number of message producers and consumers. When a connection is created, it is in stopped mode. That means that no messages are being delivered.
It is typical to leave the connection in stopped mode until setup
is complete (that is, until all message consumers have been
created). At that point, the client calls
the connection's start method, and messages begin arriving at
the connection's consumers. This setup
convention minimizes any client confusion that may result from
asynchronous message delivery while the client is still in the process
of setting itself up.
A connection can be started immediately, and the setup can be done afterwards. Clients that do this must be prepared to handle asynchronous message delivery while they are still in the process of setting up.
A message producer can send messages while a connection is stopped.
javax.jms.ConnectionFactory, javax.jms.QueueConnection, javax.jms.TopicConnection
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public ConnectionConsumer | Returns: the connection consumerthe destination to access destination, String only messages with properties matching the message selector
expression are delivered. A value of null or an empty string
indicates that there is no message selector for the message
consumer. messageSelector, ServerSessionPool the server session pool to associate with this connection
consumer sessionPool, int the maximum number of messages that can be assigned to a
server session at one time maxMessages)Creates a connection consumer for this connection (optional operation) on the specific destination. |
| public ConnectionConsumer | Returns: the durable connection consumertopic to access topic, String the name used to identify the unshared durable subscription subscriptionName, String only messages with properties matching the message selector
expression are delivered. A value of null or an empty string
indicates that there is no message selector for the message
consumer. messageSelector, ServerSessionPool the server session pool to associate with this durable
connection consumer sessionPool, int the maximum number of messages that can be assigned to a
server session at one time maxMessages)Creates a connection consumer for this connection (optional operation) on the specific topic using an unshared durable subscription with the specified name. |
| public Session | Returns: a newly created sessionindicates whether the session will use a local transaction,
except in the cases described above when this value is ignored.. transacted, int when transacted is false, indicates how messages received
by the session will be acknowledged, except in the cases described above
when this value is ignored. acknowledgeMode)Creates a |
| public Session | Returns: a newly created sessionspecifies the session mode that will be used, except in the
cases described above when this value is ignored. Legal values are
sessionMode)JMSContext.SESSION_TRANSACTED, JMSContext.CLIENT_ACKNOWLEDGE,
JMSContext.AUTO_ACKNOWLEDGE and JMSContext.DUPS_OK_ACKNOWLEDGE.Creates a |
| public Session | |
| public ConnectionConsumer | Returns: the connection consumerthe topic to access topic, String the name used to identify the shared non-durable subscription subscriptionName, String only messages with properties matching the message selector
expression are delivered. A value of null or an empty string
indicates that there is no message selector for the message
consumer. messageSelector, ServerSessionPool the server session pool to associate with this connection
consumer sessionPool, int the maximum number of messages that can be assigned to a
server session at one time maxMessages)Creates a connection consumer for this connection (optional operation) on the specific topic using a shared non-durable subscription with the specified name. |
| public ConnectionConsumer | Returns: the durable connection consumertopic to access topic, String the name used to identify the shared durable subscription subscriptionName, String only messages with properties matching the message selector
expression are delivered. A value of null or an empty string
indicates that there is no message selector for the message
consumer. messageSelector, ServerSessionPool the server session pool to associate with this durable
connection consumer sessionPool, int the maximum number of messages that can be assigned to a
server session at one time maxMessages)Creates a connection consumer for this connection (optional operation) on the specific topic using a shared durable subscription with the specified name. |
| public String | |
| public ExceptionListener | Returns: theExceptionListener for this connection, or null.
if no ExceptionListener is associated
with this connection.Gets the |
| public ConnectionMetaData | |
| public void | setClientID(String
the unique client identifier clientID)Sets the client identifier for this connection. |
| public void | setExceptionListener(ExceptionListener
the exception listener listener)Sets an exception listener for this connection. |
| public void | |
| public void |
| close | back to summary |
|---|---|
| public void close() throws JMSException Redeclares java. Closes the connection. Since a provider typically allocates significant resources outside the JVM on behalf of a connection, clients should close these resources when they are not needed. Relying on garbage collection to eventually reclaim these resources may not be timely enough. There is no need to close the sessions, producers, and consumers of a closed connection. Closing a connection causes all temporary destinations to be deleted. When this method is invoked, it should not return until message processing has been shut down in an orderly fashion. This means that all message listeners that may have been running have returned, and that all pending receives have returned. However if the close method is called from a message listener on its own
connection, then it will either fail and throw a
A close terminates all pending message receives
on the connection's sessions' consumers. The receives may return with a
message or with null, depending on whether there was a message available
at the time of the close. If one or more of the connection's sessions'
message listeners is processing a message at the time when connection
This method must not return until any incomplete asynchronous send operations for this Connection have been completed and any CompletionListener callbacks have returned. Incomplete sends should be allowed to complete normally unless an error occurs.
For the avoidance of doubt, if an exception listener for this connection
is running when Closing a connection causes any of its sessions' transactions
in progress to be rolled back. In the case where a session's
work is coordinated by an external transaction manager, a session's
A CompletionListener callback method must not call close on its own Connection. Doing so will cause an IllegalStateException to be thrown.
Invoking the
| |
| createConnectionConsumer | back to summary |
|---|---|
| public ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException Creates a connection consumer for this connection (optional operation) on the specific destination. This is an expert facility not used by ordinary JMS clients.
This method must not be used in a Java EE web or EJB application. Doing
so may cause a
| |
| createDurableConnectionConsumer | back to summary |
|---|---|
| public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException Creates a connection consumer for this connection (optional operation) on the specific topic using an unshared durable subscription with the specified name. This is an expert facility not used by ordinary JMS clients.
This method must not be used in a Java EE web or EJB application. Doing
so may cause a
| |
| createSession | back to summary |
|---|---|
| public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException Creates a
This method has been superseded by the method
The effect of setting the In a Java SE environment or in the Java EE application client container:
In a Java EE web or EJB container, when there is an active JTA transaction in progress:
In the Java EE web or EJB container, when there is no active JTA transaction in progress:
Applications running in the Java EE web and EJB containers must not attempt
to create more than one active (not closed)
| |
| createSession | back to summary |
|---|---|
| public Session createSession(int sessionMode) throws JMSException Creates a
The effect of setting the In a Java SE environment or in the Java EE application client container:
In a Java EE web or EJB container, when there is an active JTA transaction in progress:
In the Java EE web or EJB container, when there is no active JTA transaction in progress:
Applications running in the Java EE web and EJB containers must not attempt
to create more than one active (not closed)
| |
| createSession | back to summary |
|---|---|
| public Session createSession() throws JMSException Creates a The behaviour of the session that is created depends on whether this method is called in a Java SE environment, in the Java EE application client container, or in the Java EE web or EJB container. If this method is called in the Java EE web or EJB container then the behaviour of the session also depends on whether or not there is an active JTA transaction in progress. In a Java SE environment or in the Java EE application client container:
In a Java EE web or EJB container, when there is an active JTA transaction in progress:
In the Java EE web or EJB container, when there is no active JTA transaction in progress:
Applications running in the Java EE web and EJB containers must not attempt
to create more than one active (not closed)
| |
| createSharedConnectionConsumer | back to summary |
|---|---|
| public ConnectionConsumer createSharedConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException Creates a connection consumer for this connection (optional operation) on the specific topic using a shared non-durable subscription with the specified name. This is an expert facility not used by ordinary JMS clients.
This method must not be used in a Java EE web or EJB application. Doing
so may cause a
| |
| createSharedDurableConnectionConsumer | back to summary |
|---|---|
| public ConnectionConsumer createSharedDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException Creates a connection consumer for this connection (optional operation) on the specific topic using a shared durable subscription with the specified name. This is an expert facility not used by ordinary JMS clients.
This method must not be used in a Java EE web or EJB application. Doing
so may cause a
| |
| getClientID | back to summary |
|---|---|
| public String getClientID() throws JMSException Gets the client identifier for this connection. This value is specific to the JMS provider. It is either preconfigured
by an administrator in a
| |
| getExceptionListener | back to summary |
|---|---|
| public ExceptionListener getExceptionListener() throws JMSException Gets the
| |
| getMetaData | back to summary |
|---|---|
| public ConnectionMetaData getMetaData() throws JMSException Gets the metadata for this connection.
| |
| setClientID | back to summary |
|---|---|
| public void setClientID(String clientID) throws JMSException Sets the client identifier for this connection. The preferred way to assign a JMS client's client identifier is for
it to be configured in a client-specific Alternatively, a client can set a connection's client identifier
using a provider-specific value. The facility to set a connection's
client identifier explicitly is not a mechanism for overriding the
identifier that has been administratively configured. It is provided
for the case where no administratively specified identifier exists.
If one does exist, an attempt to change it by setting it must throw an
The purpose of the client identifier is to associate a connection and its objects with a state maintained on behalf of the client by a provider. The only such state identified by the JMS API is that required to support durable subscriptions. If another connection with the same
This method must not be used in a Java EE web or EJB application.
Doing so may cause a
| |
| setExceptionListener | back to summary |
|---|---|
| public void setExceptionListener(ExceptionListener listener) throws JMSException Sets an exception listener for this connection. If a JMS provider detects a serious problem with a connection, it
informs the connection's An exception listener allows a client to be notified of a problem asynchronously. Some connections only consume messages, so they would have no other way to learn their connection has failed. A connection serializes execution of its
A JMS provider should attempt to resolve connection problems itself before it notifies the client of them.
This method must not be used in a Java EE web or EJB application.
Doing so may cause a
| |
| start | back to summary |
|---|---|
| public void start() throws JMSException Starts (or restarts) a connection's delivery of incoming messages.
A call to
| |
| stop | back to summary |
|---|---|
| public void stop() throws JMSException Temporarily stops a connection's delivery of incoming messages. Delivery
can be restarted using the connection's
Stopping a connection has no effect on its ability to send messages. A
call to
A call to
If message listeners are running when
However if the
Since two alternative behaviors are permitted in this case, applications
should avoid calling
For the avoidance of doubt, if an exception listener for this connection
is running when
This method must not be used in a Java EE web or EJB application. Doing
so may cause a
| |