Connectionobject 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:
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
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.
|Method from javax.jms.Connection Summary:|
|close, createConnectionConsumer, createDurableConnectionConsumer, createSession, getClientID, getExceptionListener, getMetaData, setClientID, setExceptionListener, start, stop|
|Method from javax.jms.Connection Detail:|
public void close() throws JMSException
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
listeners that may have been running have returned, and that all pending
receives have returned. 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
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
public ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
public String getClientID() throws JMSException
This value is specific to the JMS provider. It is either preconfigured
by an administrator in a
public ExceptionListener getExceptionListener() throws JMSException
public ConnectionMetaData getMetaData() throws JMSException
public void setClientID(String clientID) throws JMSException
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
public void setExceptionListener(ExceptionListener listener) throws JMSException
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.
public void start() throws JMSException
public void stop() throws JMSException
This call blocks until receives and/or message listeners in progress have completed.
Stopping a connection has no effect on its ability to send messages.
A call to
A call to
If message listeners are running when