Spring Book – Chapter 6 – Enterprise Integration

CachingConnectionFactory

The org.springframework.jms.connection.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. If you need to scale further, you can also specify the number of sessions to cache using the sessionCacheSize property.

Listing 6-2 below demonstrates the configuration for the CachingConnectionFactory using ActiveMQ.

Listing 6-2. Configuration of CachingConnectionFactory

Sending messages

For making the example simple we will send the traditional “Hello Spring” message. Listing 6-3 show usage of JmsTemplate to send messages.

Listing 6-3. Using JmsTemplate to send message using Spring JMS

The class MySender class needs to be aware of two things; one is the destination object where the message has to be sent and the other being the JmsTemplate object that simplifies the process of sending the message.

The sending of the message is defined by the method JmsTemplate.send() that takes two arguments. The first argument is the message destination and the second argument is the message creator object that knows how to create a message that can be sent later.

Receiving messages

Receiving the message from the Message Broker is as simple as sending the message which we saw in the previous topic. Listing 6-4 show usage of JmsTemplate which is used to receive message.

Listing 6-4. Using JmsTemplate to receive message using Message broker

Page Visitors: 2305

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)

Leave a Reply

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