Receiving Messages
Similar to “send” methods, JmsTemplate offers four variations of “receive” methods namely “receive”, “receiveSelected”, “receiveAndConvert” and “receiveSelectedAndConvert”. Listing 18-19 below shows a sample class which is used to receive messages using Spring JMS.
Listing 18-19. Sample which shows receiving 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 |
import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; public class TestMessageReceiver { private JmsTemplate jmsTemplate; private Destination destination; public TestMessageReceiver() {} 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(); } } } } |
The corresponding bean declaration in Spring configuration file is shown in Listing 18-20 below.
Listing 18-20. Bean declaration of test message receiver class with objects injected
1 2 3 4 5 6 7 |
<bean id="testMessageReceiver"> <property name="jmsTemplate" ref="jmsTemplate" /> <property name="destination" ref="sampleTopic" /> </bean> |
The variations of “receive” methods can be summarized as shown in Listing 18-21 below.
Listing 18-21. Variations of “receive” methods in JmsTemplate class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public Message receive() throws JmsException; public Message receive(String destinationName) throws JmsException; public Message receive(Destination destination) throws JmsException; public Message receiveSelected(String messageSelector) throws JmsException; public Message receiveSelected(String destinationName, String messageSelector) throws JmsException; public Message receiveSelected(Destination destination, String messageSelector) throws JmsException; public Object receiveAndConvert() throws JmsException; public Object receiveAndConvert(String destinationName) throws JmsException; public Object receiveAndConvert(Destination destination) throws JmsException; public Object receiveSelectedAndConvert(String messageSelector) throws JmsException; public Object receiveSelectedAndConvert(String destinationName, String messageSelector) throws JmsException; public Object receiveSelectedAndConvert(Destination destination, String messageSelector) throws JmsException; |
Page Visitors: 16288
The following two tabs change content below.

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.

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