Creating sessions

Once a client has established a connection with EAServer, it must create one or more sessions. A session serves as a factory for creating message producers, message consumers, and temporary destinations.

Beginning in version 6.0, EAServer provides a session object that you can use for both message queues and topics.

To create a Session object, use a previously created Connection object as follows:

Session mySession = myConnect.createSession(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 messages in a transacted session, a 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 that are 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, “CORBA Component Life Cycles and Transaction Semantics,” in the EAServer CORBA Components Guide 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 achieve 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:

  1. Stops message delivery within the session.

  2. Marks all unacknowledged messages as “redelivered,” including those that have been delivered.

  3. Restarts sending all unacknowledged messages, beginning with the oldest message.

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

Session.close

The JMS provider can allocate resources on behalf of a session outside the JVM. Clients should use Session.close to close a session when it is no longer needed, to release its resources and help the application run more efficiently.

Temporary destinations

A temporary destination is generated automatically, and its scope is the connection in which it is created. A typical use for a temporary destination is as the destination to which a reply to a message should be sent. Specify this destination in the message header field JMSReplyTo.