Creating sessions

Once a client has established a connection with EAServer, it needs to create one or more sessions. A session serves as a factory for creating message producers, message consumers, and temporary destinations. JMS provides two types of session objects, one for queue connections and one for topic connections. To create a QueueSession object, use a previously created QueueConnection object as follows:

QueueSession queueSession =   queueConn.createQueueSession(true,
                            Session.AUTO_ACKNOWLEDGE);

To create a TopicSession object, use a previously created TopicConnection object as follows:

TopicSession topicSession =   topicConn.createTopicSession(true,
                            Session.AUTO_ACKNOWLEDGE);

When you create a session, set the first parameter to true if you want a transacted session. When you publish or send a message in a transacted session, the transaction begins automatically. Once a transacted session starts, all messages published or sent in the session become part of the transaction until the transaction is committed or rolled back. When a transaction is committed, all published or sent messages are delivered. If a transaction is rolled back, any messages produced in the session are destroyed, and any consumed messages are recovered. When a transacted session is committed or rolled back, the current transaction ends and the next transaction begins. See Chapter 2, “Understanding Transactions and Component Lifecycles” for more information about transactions.

Set the first parameter to false when you do not want a transacted session. If a client has an active transaction context, it can still determine transactional behavior, even if it does not create a transacted session.

The second parameter indicates whether the message producer or the consumer will acknowledge messages. This parameter is valid only for nontransacted sessions. In transacted sessions, acknowledgment is determined by the outcome of the transaction.

Acknowledgment mode

Description

AUTO_ACKNOWLEDGE

The session automatically acknowledges messages.

CLIENT_ACKNOWLEDGE

The client explicitly acknowledges a message, which automatically acknowledges all messages delivered in the session.

DUPS_OK_ACKNOWLEDGE

EAServer implements this the same as AUTO_ACKNOWLEDGE.

Session::recover

To stop message delivery within a session and redeliver all the unacknowledged messages, you can use the Session.recover method. When you call recover for a session, the message service:

The method can be called only by a non-transacted session; it throws an IllegalStateException if it is called by a transacted session.