Java Reference
In-Depth Information
Connection connection = connectionFactory.createConnection();
Before an application completes, you must close any connections you have created. Fail-
ure to close a connection can cause resources not to be released by the JMS provider.
Closing a connection also closes its sessions and their message producers and message
consumers.
connection.close();
Before your application can consume messages, you must call the connection's
start
method; for details, see “
JMS Message Consumers
” on page
353
. If you want to stop mes-
sage delivery temporarily without closing the connection, you call the
stop
method.
JMS Sessions
A
session
is a single-threaded context for producing and consuming messages. You use
sessions to create the following:
• Message producers
• Message consumers
• Messages
• Queue browsers
• Temporary queues and topics (see “
Creating Temporary Destinations
” on page
Sessions serialize the execution of message listeners; for details, see “
JMS Message
A session provides a transactional context with which to group a set of sends and receives
into an atomic unit of work. For details, see “
Using JMS API Local Transactions
” on page
Sessions implement the
Session
interface. After you create a
Connection
object, you
use it to create a
Session
:
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
The first argument means the session is not transacted; the second means the session auto-
matically acknowledges messages when they have been received successfully. (For more
information, see “
Controlling Message Acknowledgment
”
on page
360
.
)
To create a transacted session, use the following code: