이 콘텐츠는 선택한 언어로 제공되지 않습니다.

7.3. Sample JMS XA Configuration


Spring XML configuration

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/osgi  
       http://www.springframework.org/schema/osgi/spring-osgi.xsd    
">

    <!--
        OSGi TM Service
    -->
    <!-- access through Spring's PlatformTransactionManager -->
    <osgi:reference id="osgiPlatformTransactionManager"
        interface="org.springframework.transaction.PlatformTransactionManager"/> 1
    <!-- access through PlatformTransactionManager -->
    <osgi: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="maxConnections" value="1" /> 6
        <property name="connectionFactory" ref="jmsXaConnectionFactory" />
        <property name="transactionManager" ref="osgiJtaTransactionManager" />
        <property name="name" value="activemq.default" /> 7
    </bean>
    
    <bean id="jmsXaConnectionFactory"
          class="org.apache.activemq.ActiveMQXAConnectionFactory"> 8
        <property name="brokerURL" value="vm:local?jms.prefetchPolicy.all=1"/> 9
        <property name="redeliveryPolicy"> 10
            <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" /> 11
    </bean>    
    ...
</beans>
Copy to Clipboard Toggle word wrap

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 JCA 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, to pool JMS connections, and support for automatic recovery in combination with the resource manager).
Note
Only the JcaPooledConnectionFactory class has support for auto-recovery of transactions (when combined with the resource manager). The XaPooledConnectionFactory class does not have this support.
6
The maxConnections property 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 name property of the JcaPooledConnectionFactory bean must be set to a value that matches the resourceName property of the resourceManager resource 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 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.
9
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.
In this example, the prefetch policy is set to 1 for 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 the CACHE_CONNECTION option is selected (see the settings on the JmsTxConfig bean). When CACHE_CONNECTION is selected, a new JMS consumer and session are recreated for every message.
10
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.
11
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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat