Java Reference
In-Depth Information
When you set a message converter for a JMS template explicitly, it will override the
default SimpleMessageConverter . Now you can call the JMS template's convertAndSend()
and receiveAndConvert() methods to send and receive mail objects.
package com.apress.springenterpriserecipes.post;
...
public class FrontDeskImpl extends JmsGatewaySupport implements FrontDesk {
public void sendMail(Mail mail) {
getJmsTemplate().convertAndSend(mail);
}
}
package com.apress.springenterpriserecipes.post;
...
public class BackOfficeImpl extends JmsGatewaySupport implements BackOffice {
public Mail receiveMail() {
return (Mail) getJmsTemplate().receiveAndConvert();
}
}
7-3. Managing JMS Transactions
Problem
You want to participate in transactions with JMS so that the receipt and sending of messages are
transactional.
Approach
You can use the same strategy as you will everywhere else in Spring: leveraging Spring's many
TransactionManager implementations as needed and wiring the behavior into your beans.
Solution
When producing or consuming multiple JMS messages in a single method, if an error occurs in the
middle, the JMS messages produced or consumed at the destination may be left in an inconsistent state.
You have to surround the method with a transaction to avoid this problem.
In Spring, JMS transaction management is consistent with other data access strategies. For
example, you can annotate the methods that require transaction management with the @Transactional
annotation.
package com.apress.springenterpriserecipes.post;
import org.springframework.jms.core.support.JmsGatewaySupport;
import org.springframework.transaction.annotation.Transactional;
...
 
Search WWH ::




Custom Search