A Session object is a single-threaded context for producing and consuming messages. A Session object is created from the Connection object as shown in the Listing 18-3 below.
Listing 18-3. Creating Session objects
Session session = conn.createSession(true, 1);//See previous section to understand these parameters
boolean operationSuccess = doAnyOperation();
Several purposes which session serves can be summarized as below:
- It is a factory for its message producers and consumers.
- It supplies provider-optimized message factories.
- It provides a way to create Queue or Topic objects for those clients that need to dynamically manipulate provider-specific destination names.
- It supports a single series of transactions that combine work spanning its producers and consumers into atomic units.
- It defines a serial order for the messages it consumes and the messages it produces.
- It retains messages it consumes until they have been acknowledged.
- It serializes execution of message listeners registered with its message consumers.
The session object is responsible for creating various message types discussed earlier. Listing 18-4 shows creation of four message types which we have already seen using the session object as shown below.
Listing 18-4. Message creation from session object
session.createTextMessage(“Text Message in Action”);
MapMessage mapMessage = session.createMapMessage();
BytesMessage bytesMessage = session.createBytesMessage();
The session objevct is also reponsible for creating both the producer and the consumer as shown in Listing 18-5 below.
Listing 18-5. Message creation from session object
producer = session.createProducer(myDestination);
consumer = session.createConsumer(myAnotherDestination);
A client uses a javax.jms.MessageProducer object to send messages to a specified destination. MessageProducer is the parent interface for all message producers. A MessageProducer object is created by passing a Destination object to a message-producer creation method supplied by a Session.
A client can specify a default delivery mode, priority, and time to live for messages sent by a message producer. It can specify the delivery mode, priority, and time to live for an individual message as well. A client can specify a time-to-live value in milliseconds for each message it sends. This value defines a message expiration time that is the sum of the message’s time-to-live and the GMT when it was sent.
A client uses a javax.jms.MessageConsumer object to receive messages from a destination. MessageConsumer is the parent interface for all message consumers. A MessageConsumer object is created by passing a Destination object to a message-consumer creation method supplied by a Session.
A message consumer can be created with a message selector. A message selector allows the client to restrict the messages delivered to the message consumer to those that match the selector. Clients either synchronously receive a message or have them asynchronously delivered as they arrive.
Table 18-1 shows the various JMS specification interfaces and its corresponding point-to-point and publish/subscribe interfaces.
Table 18-1. Relationship between point-to-point and publish/subscribe interfaces
Page Visitors: 15166