Spring Book – Chapter 17 – Messaging with Spring


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

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

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


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

JMS Parent Point-to-Point Publish/Subscribe
Destination Queue Topic
ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Session QueueSession TopicSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver/ 



Page Visitors: 16493

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 17 – Messaging with Spring

  1. 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!|

Leave a Reply

Your email address will not be published. Required fields are marked *