In order to facilitate the sending of domain model objects, the JmsTemplate
has various send methods that take a Java object as an argument for a message's data content. The overloaded methods convertAndSend
and receiveAndConvert
in JmsTemplate
delegate the conversion process to an instance of the MessageConverter
interface. This interface defines a simple contract to convert between Java objects and JMS messages. The default implementation SimpleMessageConverter
supports conversion between String
and TextMessage
, byte[]
and BytesMesssage
, 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 MapMessageConverter
which uses reflection to convert between a JavaBean and a MapMessage
. Other popular implementations 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.
public void sendWithConversion() { Map map = new HashMap(); map.put("Name", "Mark"); map.put("Age", new Integer(47)); jmsTemplate.convertAndSend("testQueue", map, new MessagePostProcessor() { public Message postProcessMessage(Message message) throws JMSException { message.setIntProperty("AccountID", 1234); message.setJMSCorrelationID("123-00001"); return message; } }); }
This results in a message of the form:
MapMessage={ Header={ ... standard headers ... CorrelationID={123-00001} } Properties={ AccountID={Integer:1234} } Fields={ Name={String:Mark} Age={Integer:47} } }
From: http://docs.spring.io/spring/docs/3.0.0.RC2/reference/html/ch21s03.html