Sending Messages
For sending messages using JmsTemplate, it offers two options: “send” methods and “convertAndSend” methods. In simple terms “send” methods sends the supplied message and in case you need some sort of conversion before sending the message use the “convertAndSend” methods accordingly. For sending message using the “send” method, you need to create an instance of “MessageCreator” class. Variations of “send” method also exist in the JmsTemplate mainly if you would like to specify specific destination objects while sending messages. Listing 18-14 below shows a sample class which is used to send messages using Spring JMS.
Listing 18-14. Sample which shows sending of 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class TestMessageSender { private Destination destination; private JmsTemplate jmsTemplate; public TestMessageSender() {} 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", "Test"); }catch (JMSException e){ e.printStackTrace(); } return message; } }; jmsTemplate.send(destination, creator); } } |
The corresponding bean declaration in Spring configuration file is shown in Listing 18-15 below.
Listing 18-15. Bean declaration of test message sender class with objects injected
1 2 3 4 5 6 7 |
<bean id="testMessageSender"> <property name="jmsTemplate" ref="jmsTemplate" /> <property name="destination" ref="sampleQueue" /> </bean> |
The variation of “send” methods in the JmsTemplate class is as shown in Listing 18-16 below.
Listing 18-16. JmsTemplate’s “send” method variations
1 2 3 4 5 |
public void send(MessageCreator messageCreator) throws JmsException; public void send(String destinationName, MessageCreator messageCreator) throws JmsException; public void send(Destination destination, MessageCreator messageCreator) throws JmsException; |
The “convertAndSend” method initially converts the supplied messages and sends it similar to the normal “send” methods in JmsTemplate. There are six variations of this method available in JmsTemplate which is as shown in Listing 18-17 below.
Listing 18-17. JmsTemplate’s “convertAndSend” method variations
1 2 3 4 5 6 7 8 9 10 11 |
public void convertAndSend(Object message) throws JmsException; public void convertAndSend(String destinationName, Object message) throws JmsException; public void convertAndSend(Destination destination, Object message) throws JmsException; public void convertAndSend(Object message, MessagePostProcessor postProcessor) throws JmsException; public void convertAndSend(String destinationName, Object message, MessagePostProcessor postProcessor) throws JmsException; public void convertAndSend(Destination destination, Object message, MessagePostProcessor postProcessor) throws JmsException; |
If you would still like to have more control on the messages, you can use the various callbacks available in Spring. Listing 18-18 shown below shows the methods which can be used to achieve this in your application using appropriate callbacks.
Listing 18-18. Methods in JmsTemplate using callback classes
1 2 3 |
public Object execute(ProducerCallback<T> action); public Object execute(SessionCallback<T> action); |
Page Visitors: 16493

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