Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.7.3. Sample JMS XA Configuration
Spring XML configuration Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Example 7.2, “Camel JMS Component with XA Enabled” shows the complete Spring XML configuration required to initialize the
jmstx Camel JMS component with XA transactions. After setting up the Camel JMS component with this code, you can create a transactional JMS endpoint in a route using a URL like jmstx:queue:QueueName.
Example 7.2. Camel JMS Component with XA Enabled
Listing notes Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The preceding Spring XML configuration can be explained as follows:
- 1
- Define a reference to the OSGi service that exposes the
PlatformTransactionManagerinterface of the OSGi container's built-in XA transaction manager. This service can then be accessed through the bean ID,osgiPlatformTransactionManager. - 2
- Define a reference to the OSGi service that exposes the JTA
TransactionManagerinterface of the OSGi container's built-in XA transaction manager. This service can then be accessed through the bean ID,osgiJtaTransactionManager. - 3
- The bean identified by the ID,
jmstx, is the ActiveMQ implementation of the Camel JMS component. You can use this component to define transactional JMS endpoints in your routes. The only property that you need to set on this bean is a reference to theJmsConfigurationbean with the ID,jmsTxConfig. - 4
- The
JmsConfigurationbean with the ID,jmsTxConfig, is configured as described in Section 7.1, “Enabling XA on the Camel JMS Component”. In particular, the configuration bean gets a reference to the JCA pooled connection factory and a reference to theosgiPlatformTransactionManagerbean. The transacted property must be set tofalse. - 5
- The
JcaPooledConnectionFactoryis a wrapper class that adds extra capabilities to the basic connection factory bean (that is, it adds the capabilities to auto-enlist XA resources, to pool JMS connections, and support for automatic recovery in combination with the resource manager).NoteOnly theJcaPooledConnectionFactoryclass has support for auto-recovery of transactions (when combined with the resource manager). TheXaPooledConnectionFactoryclass does not have this support. - 6
- The
maxConnectionsproperty should be set to 1. - 7
- In order for the Aries transaction manager to write its transaction log properly (which is needed if you want to recover transactions after a crash), the
nameproperty of theJcaPooledConnectionFactorybean must be set to a value that matches theresourceNameproperty of theresourceManagerresource manager bean. - 8
- The bean with the ID,
jmsXaConnectionFactory, is the basic connection factory, which encapsulates the code for connecting to the JMS broker. In this case, the bean is an instance ofActiveMQXAConnectionFactorytype, which is a special connection factory class that you must use when you want to connect to the ActiveMQ broker with support for XA transactions. - 9
- The
brokerURLproperty defines the protocol for connecting to the broker. In this case, thevm:localURL connects to the broker that is embedded in the current JVM and is identified by the namelocal(the configuration of the embedded broker is not shown in this example).There are many different protocols supported by Apache ActiveMQ that you could use here. For example, to connect to a remote broker through the OpenWire TCP protocol listening on IP port61616on hostMyHost, you would use the broker URL,tcp://MyHost:61616.In this example, the prefetch policy is set to1for all queues and topics. This ensures that a connection pulls only one message at a time from the broker, which is the recommended setting when theCACHE_CONNECTIONoption is selected (see the settings on theJmsTxConfigbean). WhenCACHE_CONNECTIONis selected, a new JMS consumer and session are recreated for every message. - 10
- In this example, the redelivery policy is disabled by setting
maximumRedeliveriesto0. Typically, you would not use a redelivery policy together with transactions. An alternative approach would be to define an exception handler that routes failed exchanges to a dead letter queue. - 11
- The
resourceNameproperty is the key entry that maps from the transaction manager log to a real-worldXAResourceobject. It must be unique for eachXAResourceobject.