Java Reference
In-Depth Information
Solution
Spring provides an implementation of SimpleMessageConvertor to handle the translation of a JMS
message received to a business object and the translation of a business object to a JMS message. You can
leverage the default or provide your own.
Approach
So far, you have been handling the raw JMS messages by yourself. Spring's JMS template can help
you convert JMS messages to and from Java objects using a message converter. By default, the JMS
template uses SimpleMessageConverter for converting TextMessage to/from a string, BytesMessage
to/from a byte array, MapMessage to/from a map, and ObjectMessage to/from a serializable object. For
your front desk and back office classes, you can send and receive a map using the convertAndSend()
and receiveAndConvert() methods, and the map will be converted to/from MapMessage .
package com.apress.springenterpriserecipes.post;
...
public class FrontDeskImpl extends JmsGatewaySupport implements FrontDesk {
public void sendMail(Mail mail) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("mailId", mail.getMailId());
map.put("country", mail.getCountry());
map.put("weight", mail.getWeight());
getJmsTemplate().convertAndSend(map);
}
}
package com.apress.springenterpriserecipes.post;
...
public class BackOfficeImpl extends JmsGatewaySupport implements BackOffice {
...
public Mail receiveMail() {
Map map = (Map) getJmsTemplate().receiveAndConvert();
Mail mail = new Mail();
mail.setMailId((String) map.get("mailId"));
mail.setCountry((String) map.get("country"));
mail.setWeight((Double) map.get("weight"));
return mail;
}
}
You can also create a custom message converter by implementing the MessageConverter interface
for converting mail objects.
Search WWH ::




Custom Search