Top Description Fields Methods
jakarta.jms

public Interface Session

extends Runnable, AutoCloseable
Known Direct Subinterfaces
jakarta.jms.TopicSession, jakarta.jms.XASession, jakarta.jms.QueueSession
Imports
java.io.Serializable

A Session object is a single-threaded context for producing and consuming messages. Although it may allocate provider resources outside the Java virtual machine (JVM), it is considered a lightweight Jakarta Messaging object.

A session serves several purposes:

A session can create and service multiple message producers and consumers.

One typical use is to have a thread block on a synchronous MessageConsumer until a message arrives. The thread may then use one or more of the Session's MessageProducers.

If a client desires to have one thread produce messages while others consume them, the client should use a separate session for its producing thread.

Once a connection has been started, any session with one or more registered message listeners is dedicated to the thread of control that delivers messages to it. It is erroneous for client code to use this session or any of its constituent objects from another thread of control. The only exception to this rule is the use of the session or message consumer close method.

It should be easy for most clients to partition their work naturally into sessions. This model allows clients to start simply and incrementally add message processing complexity as their need for concurrency grows.

The close method is the only session method that can be called while some other session method is being executed in another thread.

A session may be specified as transacted. Each transacted session supports a single series of transactions. Each transaction groups a set of message sends and a set of message receives into an atomic unit of work. In effect, transactions organize a session's input message stream and output message stream into series of atomic units. When a transaction commits, its atomic unit of input is acknowledged and its associated atomic unit of output is sent. If a transaction rollback is done, the transaction's sent messages are destroyed and the session's input is automatically recovered.

The content of a transaction's input and output units is simply those messages that have been produced and consumed within the session's current transaction.

A transaction is completed using either its session's commit method or its session's rollback method. The completion of a session's current transaction automatically begins the next. The result is that a transacted session always has a current transaction within which its work is done.

The Java Transaction Service (JTS) or some other transaction monitor may be used to combine a session's transaction with transactions on other resources (databases, other Jakarta Messaging sessions, etc.). Since Java distributed transactions are controlled via the Java Transaction API (JTA), use of the session's commit and rollback methods in this context is prohibited.

The Jakarta Messaging API does not require support for JTA; however, it does define how a provider supplies this support.

Although it is also possible for a Jakarta Messaging client to handle distributed transactions directly, it is unlikely that many Jakarta Messaging clients will do this. Support for JTA in the Jakarta Messaging API is targeted at systems vendors who will be integrating the Jakarta Messaging API into their application server products.

Since
JMS 1.0
Version
Jakarta Messaging 2.0
See Also
jakarta.jms.QueueSession, jakarta.jms.TopicSession, jakarta.jms.XASession

Field Summary

Modifier and TypeField and Description
public static final int
AUTO_ACKNOWLEDGE

With this acknowledgment mode, the session automatically acknowledges a client's receipt of a message either when the session has successfully returned from a call to receive or when the message listener the session has called to process the message successfully returns.

public static final int
CLIENT_ACKNOWLEDGE

With this acknowledgment mode, the client acknowledges a consumed message by calling the message's acknowledge method.

public static final int
DUPS_OK_ACKNOWLEDGE

This acknowledgment mode instructs the session to lazily acknowledge the delivery of messages.

public static final int
SESSION_TRANSACTED

This value may be passed as the argument to the method createSession(int sessionMode) on the Connection object to specify that the session should use a local transaction.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares java.lang.AutoCloseable.close.

Closes the session.

public void
commit()

Commits all messages done in this transaction and releases any locks currently held.

public QueueBrowser

Returns:

A QueueBrowser object to peek at the messages on the specified queue.
createBrowser
(Queue
the queue to access
queue
)

Creates a QueueBrowser object to peek at the messages on the specified queue.

public QueueBrowser

Returns:

A QueueBrowser object to peek at the messages on the specified queue.
createBrowser
(Queue
the queue to access
queue
,
String
only messages with properties matching the message selector expression are delivered. A value of null or an empty string indicates that there is no message selector for the message consumer.
messageSelector
)

Creates a QueueBrowser object to peek at the messages on the specified queue using a message selector.

public BytesMessage

Returns:

A BytesMessage object.
createBytesMessage
()

Creates a BytesMessage object.

public MessageConsumer

Returns:

A MessageConsumer for the specified destination.
createConsumer
(Destination
the Destination to access.
destination
)

Creates a MessageConsumer for the specified destination.

public MessageConsumer

Returns:

A MessageConsumer for the specified destination.
createConsumer
(Destination
the Destination to access
destination
,
String
only messages with properties matching the message selector expression are delivered. A value of null or an empty string indicates that there is no message selector for the message consumer.
messageSelector
)

Creates a MessageConsumer for the specified destination, using a message selector.

public MessageConsumer

Returns:

A MessageConsumer for the specified destination.
createConsumer
(Destination
the Destination to access
destination
,
String
only messages with properties matching the message selector expression are delivered. A value of null or an empty string indicates that there is no message selector for the message consumer.
messageSelector
,
boolean
- if true, and the destination is a topic, then the MessageConsumer will not receive messages published to the topic by its own connection.
noLocal
)

Creates a MessageConsumer for the specified destination, specifying a message selector and the noLocal parameter.

public MessageConsumer

Returns:

An unshared durable subscription on the specified topic.
createDurableConsumer
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
)

Creates an unshared durable subscription on the specified topic (if one does not already exist) and creates a consumer on that durable subscription.

public MessageConsumer

Returns:

An unshared durable subscription on the specified topic.
createDurableConsumer
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
,
String
only messages with properties matching the message selector expression are added to the durable subscription. A value of null or an empty string indicates that there is no message selector for the durable subscription.
messageSelector
,
boolean
if true then any messages published to the topic using this session's connection, or any other connection with the same client identifier, will not be added to the durable subscription.
noLocal
)

Creates an unshared durable subscription on the specified topic (if one does not already exist), specifying a message selector and the noLocal parameter, and creates a consumer on that durable subscription.

public TopicSubscriber

Returns:

An unshared durable subscription on the specified topic.
createDurableSubscriber
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
)

Creates an unshared durable subscription on the specified topic (if one does not already exist) and creates a consumer on that durable subscription.

public TopicSubscriber

Returns:

An unshared durable subscription on the specified topic.
createDurableSubscriber
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
,
String
only messages with properties matching the message selector expression are added to the durable subscription. A value of null or an empty string indicates that there is no message selector for the durable subscription.
messageSelector
,
boolean
if true then any messages published to the topic using this session's connection, or any other connection with the same client identifier, will not be added to the durable subscription.
noLocal
)

Creates an unshared durable subscription on the specified topic (if one does not already exist), specifying a message selector and the noLocal parameter, and creates a consumer on that durable subscription.

public MapMessage

Returns:

A MapMessage object.
createMapMessage
()

Creates a MapMessage object.

public Message

Returns:

A Message object.
createMessage
()

Creates a Message object.

public ObjectMessage

Returns:

A ObjectMessage object.
createObjectMessage
()

Creates an ObjectMessage object.

public ObjectMessage

Returns:

A ObjectMessage object.
createObjectMessage
(Serializable
the object to use to initialize this message
object
)

Creates an initialized ObjectMessage object.

public MessageProducer

Returns:

A MessageProducer to send messages.
createProducer
(Destination
the Destination to send to, or null if this is a producer which does not have a specified destination.
destination
)

Creates a MessageProducer to send messages to the specified destination.

public Queue

Returns:

a Queue object which encapsulates the specified name
createQueue
(String
A provider-specific queue name
queueName
)

Creates a Queue object which encapsulates a specified provider-specific queue name.

public MessageConsumer

Returns:

A shared non-durable subscription with the specified name on the specified topic.
createSharedConsumer
(Topic
the Topic to subscribe to
topic
,
String
the name used to identify the shared non-durable subscription
sharedSubscriptionName
)

Creates a shared non-durable subscription with the specified name on the specified topic (if one does not already exist) and creates a consumer on that subscription.

public MessageConsumer

Returns:

A shared non-durable subscription with the specified name on the specified topic.
createSharedConsumer
(Topic
the Topic to subscribe to
topic
,
String
the name used to identify the shared non-durable subscription
sharedSubscriptionName
,
String
only messages with properties matching the message selector expression are added to the shared non-durable subscription. A value of null or an empty string indicates that there is no message selector for the shared non-durable subscription.
messageSelector
)

Creates a shared non-durable subscription with the specified name on the specified topic (if one does not already exist) specifying a message selector, and creates a consumer on that subscription.

public MessageConsumer

Returns:

A shared durable subscription on the specified topic.
createSharedDurableConsumer
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
)

Creates a shared durable subscription on the specified topic (if one does not already exist), specifying a message selector and the noLocal parameter, and creates a consumer on that durable subscription.

public MessageConsumer

Returns:

A shared durable subscription on the specified topic.
createSharedDurableConsumer
(Topic
the non-temporary Topic to subscribe to
topic
,
String
the name used to identify this subscription
name
,
String
only messages with properties matching the message selector expression are added to the durable subscription. A value of null or an empty string indicates that there is no message selector for the durable subscription.
messageSelector
)

Creates a shared durable subscription on the specified topic (if one does not already exist), specifying a message selector, and creates a consumer on that durable subscription.

public StreamMessage

Returns:

A StreamMessage object.
createStreamMessage
()

Creates a StreamMessage object.

public TemporaryQueue

Returns:

a temporary queue identity
createTemporaryQueue
()

Creates a TemporaryQueue object.

public TemporaryTopic

Returns:

a temporary topic identity
createTemporaryTopic
()

Creates a TemporaryTopic object.

public TextMessage

Returns:

A TextMessage object.
createTextMessage
()

Creates a TextMessage object.

public TextMessage

Returns:

A TextMessage object.
createTextMessage
(String
the string used to initialize this message
text
)

Creates an initialized TextMessage object.

public Topic

Returns:

a Topic object which encapsulates the specified name
createTopic
(String
A provider-specific topic name
topicName
)

Creates a Topic object which encapsulates a specified provider-specific topic name.

public int

Returns:

If the session is not transacted, returns the current acknowledgement mode for the session. If the session is transacted, returns SESSION_TRANSACTED.
getAcknowledgeMode
()

Returns the acknowledgement mode of the session.

public MessageListener

Returns:

the distinguished message listener associated with this session
getMessageListener
()

Returns the session's distinguished message listener (optional).

public boolean

Returns:

true if the session is in transacted mode
getTransacted
()

Indicates whether the session is in transacted mode.

public void
recover()

Stops message delivery in this session, and restarts message delivery with the oldest unacknowledged message.

public void
rollback()

Rolls back any messages done in this transaction and releases any locks currently held.

public void
run()

Redeclares java.lang.Runnable.run.

Optional operation, intended to be used only by Application Servers, not by ordinary Jakarta Messaging clients.

public void
setMessageListener(MessageListener
the message listener to associate with this session
listener
)

Sets the session's distinguished message listener (optional).

public void
unsubscribe(String
the name used to identify this subscription
name
)

Unsubscribes a durable subscription that has been created by a client.