3.2 JMS Adapters

Spring Integration provides two adapters for accepting JMS messages: JmsPollingSourceAdapter and JmsMessageDrivenSourceAdapter. The former uses Spring's JmsTemplate to receive based on a polling period. The latter configures and delegates to an instance of Spring's DefaultMessageListenerContainer.

The JmsPollingSourceAdapter requires a reference to either a single JmsTemplate instance or both ConnectionFactory and Destination (a 'destinationName' can be provided in place of the 'destination' reference). The JmsPollingSourceAdapter also requires a 'channel' property that should be a reference to a MessageChannel instance. The adapter accepts additional properties such as: period, initialDelay, maxMessagesPerTask, and sendTimeout. The following example defines a JMS source adapter that polls every 5 seconds and then sends to the "exampleChannel":

<bean class="org.springframework.integration.adapter.jms.JmsPollingSourceAdapter">
    <constructor-arg ref="jmsTemplate"/>
    <property name="channel" ref="exampleChannel"/>
    <property name="period" value="5000"/>
</bean>

In most cases, Spring Integration's message-driven JMS adapter is more appropriate since it delegates to a MessageListener container and supports dynamically adjusting concurrent consumers. The JmsMessageDrivenSourceAdapter requires references to a MessageChannel, a ConnectionFactory, and a Destination (or 'destinationName'). The following example defines a JMS message-driven source adapter that receives from the JMS queue called "exampleQueue" and then sends to the Spring Integration channel named "exampleChannel":

<bean class="org.springframework.integration.adapter.jms.JmsMessageDrivenSourceAdapter">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="destinationName" value="exampleQueue"/>
    <property name="channel" ref="exampleChannel"/>
</bean>

For both source adapter types, Spring's MessageConverter strategy is used to convert the JMS message into a plain Java object, and then Spring Integration's MessageMapper strategy is used to convert from the plain object to a Message.

The JmsTargetAdapter is a MessageHandler implementation that is capable of mapping Spring Integration Messages to JMS messages and then sending to a JMS destination. It requires either a 'jmsTemplate' reference or both 'connectionFactory' and 'destination' references. In the section called “Configuring Channel Adapters”, you will see how to configure a JMS target adapter with Spring Integration's namespace support.