Spring Book – Chapter 17 – Messaging with Spring

CachingConnectionFactory

The CachingConnectionFactory is meant to wrap a JMS provider’s connection to provide caching of sessions, connections and producers as well as automatic connection recovery. By default, it uses a single session to create many connections and this model works very well with most MOM’s. But if you need to scale further, you can also specify the number of sessions to cache using the “sessionCacheSize” property. Listing 18-8 below shows declaring caching connection factory in the Spring configuration file.

Listing 18-8. Declaring CachingConnectionFactory bean

Configuring Destinations

A JMS destination is an object (a JMS queue or a JMS topic) that represents the target of messages that the client produces and the source of messages that the client consumes. In point-to-point messaging, destinations represent queues; in publish/subscribe messaging, destinations represent topics. When you create a destination object, you must specify whether the destination is a JMS queue (in the point-to-point messaging domain) or a JMS topic (in publish/subscribe messaging domain). Listing 18-9 shows declaring destination based on queue and Listing 18-10 shows declaring destination based on topic. Code sample uses JMS provider Apache ActiveMQ to explain things in detail.

Listing 18-9. Declaring queue destination

Defining JmsTemplate

Now that we have discussed the various components of Spring JMS, its now time to define the main helper class in Spring namely JmsTemplate as shown in Listing 18-11 below. As discussed earlier JmsTemplate is mainly used for messaging that is synchronous in nature.

Listing 18-11. Defining JmsTemplate as a Spring bean

To make a JmsTemplate, a connection factory is mandatory. Optionally you have option of providing it with MessageConvereter, DestinationResolver and a default destination object or destination name. Listing 18-12 shows declaring JmsTemplate with these optional parameters configured.

Listing 18-12. Define JmsTemplate with optional parameters in Spring configuration file

Defining Message Listener Container

If JmsTemplate are for synchronous messaging, message listener container are for asynchronous messaging. Asynchronous message receivers in Spring, very much similar to JEE’s MDB, is called MDP. Spring provides two message listener containers namely DefaultMessageListenerContainer and SimpleMessageListenerContainer.

Listing 18-13 below shows declaration of a message container listener in the Spring configuration file.

Listing 18-13. Declaration of message listener container as spring bean

SimpleMessageListenerContainer is the simplest form of a message listener container. It creates a fixed number of JMS Sessions to invoke the listener, not allowing for dynamic adaptation to runtime demands. Its main advantage is its low level of complexity and the minimum requirements on the JMS provider.

DefaultMessageListenerContainer is designed to work in a native JMS environment as well as in a J2EE environment, with only minimal differences in configuration. This is a simple but nevertheless powerful form of message listener container. On startup, it obtains a fixed number of JMS Sessions to invoke the listener, and optionally allows for dynamic adaptation at runtime

Page Visitors: 16774

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 *