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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<beans> ... <!-- A connection to ActiveMQ --> <bean id="amqConnectionFactory" p:brokerURL='tcp://domain:port" /> <!-- A destination in ActiveMQ --> <bean id="activeMQDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="TEST" /> </bean> <!-- Cached connection to wrap the ActiveMQ connections --> <bean id="cachedConnectionFactory" p:targetConnectionFactory-ref="amqConnectionFactory" p:sessionCacheSize="10" /> <!-- A JmsTemplate instance that uses the cached connection and destination --> <bean id="testTemplate" p:connectionFactory-ref="cachedConnectionFactory" p:defaultDestination-ref="activeMQDestination " /> ... </beans> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
public class MySender { private Destination destination; private JmsTemplate jmsTemplate; public MySender () {} public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } public void sendMessage() { MessageCreator creator = new MessageCreator() { public Message createMessage(Session session){ TextMessage message = null; try { message = session.createTextMessage(); message.setStringProperty("text", "Hello Spring"); }catch (JMSException e){ e.printStackTrace(); } return message; } }; jmsTemplate.send(destination, creator); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
public class MyReceiver { private JmsTemplate jmsTemplate; private Destination destination; public MyReceiver() {} public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } public void receiveMessage() { Message message = jmsTemplate.receive(); TextMessage textMessage = null; if (message instanceof TextMessage) { textMessage = (TextMessage)message; try { System.out.println(textMessage.getStringProperty("text")); }catch (JMSException e) { e.printStackTrace(); } } } } |
Page Visitors: 2883
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