Java Reference
In-Depth Information
JMS Sessions
As mentioned previously, the JMS 2.0 release utilizes a newer simplified API. However, previous versions of the API
may still need to be used for maintenance purposes and other reasons. For that reason, it is useful to understand the
concept of JMS Sessions, which are abstracted away from the developer when using the newer simplified API.
When using the standard API, you must obtain a JMS connection so that you can start a session before you can
begin to send or consume messages. A session can be used to create JMS resources such as Message Consumers,
Message Producers, Messages, Queue Browsers, and Temporary Queues and Topics. A session can be created
using a Connection object. The following line of code demonstrates how to obtain a Connection from an injected
ConnectionFactory .
Connection connection = connectionFactory.createConnection();
To create a session, call a Connection object's createSession method, and pass the appropriate arguments
depending upon your application's needs. The createSession syntax is as follows:
createSession(boolean isTransacted, int acknowledgementType)
The first argument to the createSession method is a boolean value to indicate if transactions should take
place within the session. If a session is created as transacted (set true for the first argument to createSession ),
acknowledgement occurs once the entire transaction is successfully committed. If, for some reason, the transaction
is not committed, the entire transaction is rolled back, and all messages are redelivered. However, if a session is not
transacted, one must indicate which type of acknowledgment must be received to consider a message successfully
sent. The second argument to the createSession method indicates the acknowledgment type. Table 10-1 lists the
different acknowledgment types along with a description of each.
Table 10-1. JMS Session Message Acknowledgment
Acknowledgment Type
The session automatically acknowledges a client's receipt of a message either
when the client has successfully returned from a call to receive or when the
MessageListener it has called to process the message returns successfully.
The client acknowledges the receipt of a message by calling the message's
acknowledge method.
Lazy acknowledgement of messages . . . allowing duplicates to be received.
For example, session creation may look something like the following:
Session session = connection.createSession(false,
The session that is created above is non-transactional, and the receipt type is Session.AUTO_ACKNOWLEDGE .
This is the most common type of JMS session that is created. Once the session has been created, then it can be used
to create JMS resources.
Search WWH ::

Custom Search