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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- A connection to ActiveMQ --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL='tcp://localhost:99999" /> <!-- A cached connection to wrap the ActiveMQ connection --> <bean id="cachedConnectionFactory" p:targetConnectionFactory-ref="connectionFactory" p:sessionCacheSize="10" /> |
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
1 2 3 4 5 |
<bean id="sampleQueue"></bean> Listing 18-10. Declaring topic destination <bean id="sampleTopic"></bean> |
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
1 2 3 4 5 |
<bean id="jmsTemplate"> <property name=" connectionFactory" ref="connectionFactory" /> </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
1 2 3 4 5 6 7 8 9 10 11 |
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="destinationResolver" ref="destinationResolver"/> <property name="defaultDestinationName" value="testQueueName"/> <property name="receiveTimeout" value="10000"/> </bean> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<bean id=”messageListener” class=”com.mybook.sample.listners.MyMessageListener”/> <bean id="messageListenerContainer" > <property name="connectionFactory" ref="connectionFactory"/> <property name="destinationResolver" ref="destinationResolver"/> <property name="destinationName" value="testQueueName"/> <property name="messageListener" ref="messageListener"/> </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: 16288

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