Sending a Message - Java-Springs

The JmsTemplate contains many convenience methods to send a message. There are send methods that specify the destination using a javax.jms.Destination object and those that specify the destination using a string for use in a JNDI lookup. The send method that takes no destination argument uses the default destination. Here is an example that sends a message to a queue using the 1.0.2 implementation.

This example uses the Message Creator callback to create a text message from the supplied Session object. The JmsTemplate is constructed by passing a reference to a Connection Factory. As an alternative, a zero argument constructor and connection Factory is provided and can be used for constructing the instance in JavaBean style (using a BeanFactory or plain Java code). Alternatively, consider deriving from Spring's Jms Gate way Support convenience base class, which provides pre-built bean properties for JMS configuration.

The method send(String destination Name, Message Creator creator) lets you send a message using the string name of the destination. If these names are registered in JNDI, you should set the destinationResolver property of the template to an instance of Jndi Destination Resolver.

If you created the JmsTemplate and specified a default destination, the send(Message Creatorc) sends a message to that destination.

Using Message Converters
In order to facilitate the sending of domain model objects, the Jms Template has various send methods that take a Java object as an argument for a message's data content. The overloaded methods convert And Send() and receive And Convert() in Jms Template delegate the conversion process to an instance of the Message Converter interface. This interface defines a simple contract to convert between Java objects and JMS messages. The default implementation Simple Message Converter supports conversion between String and Text Message, byte[] and Bytes Messsage, and java.util.Map and MapMessage. By using the converter, you and your application code can focus on the business object that is being sent or received via JMS and not be concerned with the details of how it is represented as a JMS message.

The sandbox currently includes a MapMessage Converter which uses reflection to convert between a JavaBean and a MapMessage. Other popular implementation choices you might implement yourself are Converters that use an existing XML marshalling package, such as JAXB, Castor, XMLBeans, or XStream, to create a TextMessage representing the object.

To accommodate the setting of a message's properties, headers, and body that can not be generically encapsulated inside a converter class, the MessagePostProcessor interface gives you access to the message after it has been converted, but before it is sent. The example below demonstrates how to modify a message header and a property after a java.util.Map is converted to a message.

SessionCallback and ProducerCallback
While the send operations cover many common usage scenarios, there are cases when you want to perform multiple operations on a JMS Session or Message Producer. The SessionCallback and ProducerCallback expose the JMS Session and Session / MessageProducer pair respectively. The execute() methods on Jms Template execute these callback methods.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics