7.3. Sample JMS XA Configuration


Spring XML configuration

Example 7.1, “Camel JMS Component with XA Enabled” shows the complete Blueprint 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.1. Camel JMS Component with XA Enabled

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!--
        OSGi TM Service
    -->
    <!-- access through Spring's PlatformTransactionManager -->
    <reference id="osgiPlatformTransactionManager"
        interface="org.springframework.transaction.PlatformTransactionManager"/> 1
    <!-- access through PlatformTransactionManager -->
    <reference id="osgiJtaTransactionManager"
        interface="javax.transaction.TransactionManager"/> 2
    ...
    <!--
        JMS TX endpoint configuration
    -->
    <bean id="jmstx"
          class="org.apache.activemq.camel.component.ActiveMQComponent"> 3
        <property name="configuration" ref="jmsTxConfig" /> 
    </bean> 
    
    <bean id="jmsTxConfig"
          class="org.apache.camel.component.jms.JmsConfiguration"> 4
        <property name="connectionFactory" ref="jmsXaPoolConnectionFactory"/> 
        <property name="transactionManager" ref="osgiPlatformTransactionManager"/> 
        <property name="transacted" value="false"/>
        <property name="cacheLevelName" value="CACHE_CONNECTION"/>
    </bean> 
    
    <!-- connection factory wrapper to support auto-enlisting of XA resource -->
    <bean id="jmsXaPoolConnectionFactory"
          class="org.apache.activemq.pool.JcaPooledConnectionFactory"> 5
        <property name="name" value="MyXaResourceName" />
        <property name="maxConnections" value="1" /> 6
        <property name="connectionFactory" ref="jmsXaConnectionFactory" />
        <property name="transactionManager" ref="osgiJtaTransactionManager" />
    </bean>
    
    <bean id="jmsXaConnectionFactory"
          class="org.apache.activemq.ActiveMQXAConnectionFactory"> 7
        <property name="brokerURL" value="vm:local"/> 8
        <property name="userName" value="UserName"/>
        <property name="password" value="Password"/>
        <property name="redeliveryPolicy"> 9
            <bean class="org.apache.activemq.RedeliveryPolicy">
                <property name="maximumRedeliveries" value="0"/>
            </bean>
        </property>
    </bean>
    
    <!--
        ActiveMQ XA Resource Manager
    -->
    <bean id="resourceManager"
          class="org.apache.activemq.pool.ActiveMQResourceManager"
          init-method="recoverResource"> 
        <property name="transactionManager" ref="osgiJtaTransactionManager" />
        <property name="connectionFactory" ref="jmsXaPoolConnectionFactory" />
        <property name="resourceName" value="activemq.default" /> 10
    </bean>    
    ...
</blueprint>

Listing notes

The preceding Spring XML configuration can be explained as follows:
1
Define a reference to the OSGi service that exposes the PlatformTransactionManager interface 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 TransactionManager interface 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 the JmsConfiguration bean with the ID, jmsTxConfig.
4
The JmsConfiguration bean 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 XA pooled connection factory and a reference to the osgiPlatformTransactionManager bean. The transacted property must be set to false.
5
The JcaPooledConnectionFactory is a wrapper class that adds extra capabilities to the basic connection factory bean (that is, it adds the capabilities to auto-enlist XA resources and to pool JMS connections).
6
The maxConnections property should be set to 1.
7
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 of ActiveMQXAConnectionFactory type, 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.
8
The brokerURL property defines the protocol for connecting to the broker. In this case, the vm:local URL connects to the broker that is embedded in the current JVM and is identified by the name local (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 port 61616 on host MyHost, you would use the broker URL, tcp://MyHost:61616.
9
In this example, the redelivery policy is disabled by setting maximumRedeliveries to 0. 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.
10
The resourceName property is the key entry that maps from the transaction manager log to a real-world XAResource object. It must be unique for each XAResource object.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.