The basic requirements for writing a transactional application in Spring are a
transaction manager bean and a resource
bean (or, in some cases, multiple resource beans). You can then
use the transaction manager bean either to create a transactional Fuse Mediation Router
component (see Demarcation by Transactional Endpoints) or to mark a
route as transactional, using the transacted()
Java DSL command
(see Demarcation by Marking the Route).
To configure the JMS transaction manager and the JMS resource, perform the following steps:
Customize the Spring XML configuration. Using your favorite text editor, open the
tx-jms-router/src/main/resources/META-INF/spring/camel-context.xml
file and add the following content to thebeans
element:<beans ... > ... <bean id="jmstx" class="org.apache.camel.component.jms.JmsComponent"> <property name="configuration" ref="jmsConfig" /> </bean> <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> <property name="connectionFactory" ref="jmsConnectionFactory"/> <property name="transactionManager" ref="jmsTransactionManager"/> <property name="transacted" value="true"/> </bean> <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager"> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean> <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://broker1?brokerConfig=xbean:tutorial/activemq.xml"/> </bean> </beans>
This configuration creates a custom JMS component, with bean ID equal to
jmstx
, that you can use to define transactional JMS endpoints in your routes. The underlying JMS system is an embedded Fuse Message Broker (Apache ActiveMQ) broker, which takes its configuration from the file,tutorial/activemq.xml
.Create the ActiveMQ configuration file,
activemq.xml
. First create the new directory,tx-jms-router/src/main/resources/tutorial
. Next, using your favorite text editor, create the file,activemq.xml
, under thetutorial
directory, and add the following text:<?xml version="1.0" encoding="UTF-8"?> <beans> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <broker useJmx="true" xmlns="http://activemq.org/config/1.0" persistent="false" brokerName="broker1"> <transportConnectors> <transportConnector name="openwire" uri="tcp://localhost:61616"/> </transportConnectors> </broker> </beans>