Spring Book – Chapter 17 – Messaging with Spring

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

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

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

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

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

Page Visitors: 16832

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)

1 thought on “Spring Book – Chapter 17 – Messaging with Spring

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

Leave a Reply

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