background image

JMS Sessions

<< JMS Connections | JMS Message Consumers >>
<< JMS Connections | JMS Message Consumers >>

JMS Sessions

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 941
)
Sessions serialize the execution of message listeners; for details, see
"JMS Message Listeners" on
page 906
.
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 947
.
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 that the session is not transacted; the second means that the session
automatically acknowledges messages when they have been received successfully. (For more
information, see
"Controlling Message Acknowledgment" on page 936
.)
To create a transacted session, use the following code:
Session session = connection.createSession(true, 0);
Here, the first argument means that the session is transacted; the second indicates that message
acknowledgment is not specified for transacted sessions. For more information on transactions,
see
"Using JMS API Local Transactions" on page 947
. For information about the way JMS
transactions work in Java EE applications, see
"Using the JMS API in a Java EE Application" on
page 954
.
JMS Message Producers
A message producer is an object that is created by a session and used for sending messages to a
destination. It implements the MessageProducer interface.
You use a Session to create a MessageProducer for a destination. The following examples show
that you can create a producer for a Destination object, a Queue object, or a Topic object:
The JMS API Programming Model
The Java EE 5 Tutorial · September 2007
904