5.5. Messaging (JMS) Integration
Spring applications have two distinct mechanisms of integrating with JMS. One is by using the JmsTemplate which simplifies the usage of the JMS API for sending and receiving messages, and the other is by implementing message-driven POJOs.
The recommended practice in Red Hat JBoss Enterprise Application Platform is to use the JmsTemplate for sending messages, but not for receiving them.
Spring applications can implement message-driven POJOs by wiring MessageListener beans (or POJOs through a MessageListenerAdapter) into a MessageListenerContainer.
A typical message-driven POJO is shown in the following example.
Example 5.21. A message-driven POJO as a simple Java class with a single-argument method
public class MessageDrivenPojo { @Autowire PaymentProcessor paymentProcessor; public void pojoHandlerMethod(PaymentNotification paymentNotification) { paymentProcessor.processPayment(paymentNotification.getAccountNumber(), paymentNotification.getAmount()); } }
Spring provides two different types of MessageListenerContainers; native JMS and JCA-based. JBoss Enterprise Application Platform uses the JCA MessageListenerContainer for better integration with the application server for server message and delivery session, connection, and message consumer management. In order to minimize the amount of proprietary code, you can use Snowdrop's namespace support for JMS/JCA integration.
Example 5.22. JCA Message Listener Containers and Namespace Support in Red Hat JBoss Enterprise Application Platform
<jms:jca-listener-container resource-adapter="resourceAdapter" acknowledge="auto" activation-spec-factory="activationSpecFactory"> <jms:listener destination="/someDestination" ref="messageDrivenPojo" method="pojoHandlerMethod"/> </jms:jca-listener-container> <jboss:activation-spec-factory id="activationSpecFactory"/> <jboss:resource-adapter id="resourceAdapter"/> <bean id="messageDrivenPojo" class="example.MessageDrivenPojo"/>
When a message arrives, the container invokes the messageDrivenPojo bean. The message is converted to the argument type of the pojoHandlerMethod inside the bean. You can use any regular bean as a message-driven POJO; the only restriction is that the handling method must have a single argument. Spring handles the conversion of message content to the expected argument type. For a JCA-based container, the invocation is automatically enrolled in a JTA transaction.