Session
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Session session = conn.createSession(true, 1);//See previous section to understand these parameters boolean operationSuccess = doAnyOperation(); if(operationSuccess){ session.commit(); }else{ session.rollback(); } |
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
1 2 3 4 5 6 7 8 9 10 11 |
session.createTextMessage(“Text Message in Action”); session.createObjectMessage(objectWhichCanbeSerialized); MapMessage mapMessage = session.createMapMessage(); mapMessage.setInt(“myKey”, 1234); BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.writeBytes(myByteArray); |
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
1 2 3 |
producer = session.createProducer(myDestination); consumer = session.createConsumer(myAnotherDestination); |
MessageProducer
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.
MessageConsumer
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
JMS Parent | Point-to-Point | Publish/Subscribe |
Destination | Queue | Topic |
ConnectionFactory | QueueConnectionFactory | TopicConnectionFactory |
Connection | QueueConnection | TopicConnection |
Session | QueueSession | TopicSession |
MessageProducer | QueueSender | TopicPublisher |
MessageConsumer | QueueReceiver/
QueueBrowser |
TopicSubscriber |
Page Visitors: 16493

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
First of all I would like to say fantastic blog! I had a quick question which I’d like to ask if you don’t mind. I was interested to know how you center yourself and clear your head before writing. I have had difficulty clearing my mind in getting my ideas out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any recommendations or tips? Thank you!|