`

Atomikos-Spring集成

    博客分类:
  • JAVA
阅读更多

Spring Integration

 

Configuring Atomikos as the Spring JTA Transaction Manager

You basically have two big options: the basic case (with JTA property file lookup) or the advanced case (where everything is specified in the Spring configuration).

 

The Basic Case (Pre-3.3)

Atomikos can easily be configured as the Spring JTA transaction manager. The following code snippet shows how to specify this in your Spring configuration file:

 

<!--  
     Construct Atomikos UserTransactionManager,  
     needed to configure Spring  
--> 
<bean id="AtomikosTransactionManager"  
      class="com.atomikos.icatch.jta.UserTransactionManager"  
    init-method="init" destroy-method="close"> 
 
   <!--   
         when close is called, should we force  
         transactions to terminate or not?  
   --> 
   <property name="forceShutdown" value="false" /> 
</bean> 
 
<!--  
     Also use Atomikos UserTransactionImp,  
     needed to configure Spring   
-->  
<bean id="AtomikosUserTransaction"  
   class="com.atomikos.icatch.jta.UserTransactionImp"> 
 
   <property name="transactionTimeout" value="300" /> 
</bean> 
 
<!--  
     Configure the Spring framework to use  
     JTA transactions from Atomikos  
--> 
<bean id="JtaTransactionManager"  
      class="org.springframework.transaction.jta.JtaTransactionManager"> 
   <property name="transactionManager" ref="AtomikosTransactionManager" /> 
   <property name="userTransaction" ref="AtomikosUserTransaction" /> 
</bean> 

 

The Advanced Case (As of 3.3)

The advanced case adds the possibility to specify everything, including JTA properties and optional log administrators to manage the transaction logs.

 

<!-- Optional: add a log administrator --> 
<bean id="localLogAdministrator"  
 class="com.atomikos.icatch.admin.imp.LocalLogAdministrator"/> 
 
<bean id="userTransactionService"  
  class="com.atomikos.icatch.config.UserTransactionServiceImp"  
  init-method="init" destroy-method="shutdownForce"> 
    <constructor-arg> 
        <!-- IMPORTANT: specify all Atomikos properties here --> 
        <props> 
            <prop key="com.atomikos.icatch.service"> 
              com.atomikos.icatch.standalone.UserTransactionServiceFactory 
            </prop> 
        </props> 
    </constructor-arg> 
    <property name="initialLogAdministrators"> 
        <list> 
            <ref bean="localLogAdministrator"/> 
        </list> 
    </property> 
</bean> 
 
<!--  
     Construct Atomikos UserTransactionManager,  
     needed to configure Spring  
--> 
<bean id="AtomikosTransactionManager"  
      class="com.atomikos.icatch.jta.UserTransactionManager"  
      init-method="init" destroy-method="close"  
      depends-on="userTransactionService"> 
 
   <!-- IMPORTANT: disable startup because the userTransactionService above does this --> 
   <property name="startupTransactionService" value="false"/> 
 
   <!--   
         when close is called,  
         should we force transactions to terminate or not?  
   --> 
   <property name="forceShutdown" value="false" /> 
</bean> 
 
<!--  
     Also use Atomikos UserTransactionImp,  
     needed to configure Spring   
-->  
<bean id="AtomikosUserTransaction"  
      class="com.atomikos.icatch.jta.UserTransactionImp"   
      depends-on="userTransactionService"> 
   <property name="transactionTimeout" value="300" /> 
</bean> 
 
<!--  
   Configure the Spring framework to use JTA transactions from Atomikos   
--> 
<bean id="JtaTransactionManager"  
      class="org.springframework.transaction.jta.JtaTransactionManager"  
      depends-on="userTransactionService"> 
   <property name="transactionManager" ref="AtomikosTransactionManager" /> 
   <property name="userTransaction" ref="AtomikosUserTransaction" /> 
</bean> 

 

At the time of writing, avoiding property file lookup also requires you to set the system propertycom.atomikos.icatch.service - since this property will NOT be read from the regular init properties that you supply. This will be fixed in a later release. Until then, you can find a Spring config workaround here:http://fogbugz.atomikos.com/default.asp?community.6.1921

 

Spring-Demarcated Transactions for POJOs

We support most of the Spring demarcation attributes except PROPAGATION_NESTED since that one depends on the Spring DataSourceTransactionManager strategy (incompatible with JTA/XA).

 

Receiving JMS Messages

You can receive messages either with the Atomikos receiver sessions, or with the Spring message listener containers.

 

With the Atomikos MessageDrivenContainer

See the examples in the Atomikos download for how to configure...

 

With Spring's MessageListenerContainer

You can also use Spring's message listener; the following XML fragment shows a complete example on how to configure this:

 

   <bean id="xaFactory"  
              class="org.apache.activemq.ActiveMQXAConnectionFactory"> 
      <property name="brokerURL" value="tcp://localhost:61616" /> 
   </bean> 
 
   <!--  
             Configure the JMS connector;  
             call init to register for recovery!  
        --> 
   <bean id="ConnectionFactory"  
              class="com.atomikos.jms.AtomikosConnectionFactoryBean"  
              init-method="init" destroy-method="close"> 
      <property name="uniqueResourceName" value="amq1" /> 
      <property name="xaConnectionFactory" ref="xaFactory" /> 
   </bean> 
 
   <!--  
             Construct Atomikos UserTransactionManager,  
             needed to configure Spring  
        --> 
   <bean id="AtomikosTransactionManager"  
              class="com.atomikos.icatch.jta.UserTransactionManager"  
              init-method="init" destroy-method="close"> 
      <!--  when close is called, should we force  
                      transactions to terminate or not? --> 
      <property name="forceShutdown" value="false" /> 
   </bean> 
 
   <!--  
             Also use Atomikos UserTransactionImp,  
             needed to configure Spring   
        -->  
   <bean id="AtomikosUserTransaction"  
              class="com.atomikos.icatch.jta.UserTransactionImp"> 
       <property name="transactionTimeout" value="300" /> 
   </bean> 
 
   <!--  
           Configure the Spring framework to use JTA transactions from Atomikos  
        --> 
   <bean id="JtaTransactionManager"  
              class="org.springframework.transaction.jta.JtaTransactionManager"> 
      <property name="transactionManager" ref="AtomikosTransactionManager" /> 
      <property name="userTransaction" ref="AtomikosUserTransaction" /> 
   </bean> 
 
    <!-- a class that implements javax.jms.MessageListener --> 
   <bean id="MessageListener" class="jtatest.TextOutputMessageListener" /> 
 
    <!--  
         a kind of message listener pool that will listen to  
         messages posted to 'requestQueue' with 3 threads,  
         each one consuming a connection from bean 'ConnectionFactory'  
    --> 
    <bean id="MessageListenerContainer" 
          class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
        <property name="transactionManager" ref="JtaTransactionManager" /> 
        <property name="connectionFactory" ref="ConnectionFactory" /> 
        <property name="messageListener" ref="MessageListener" /> 
        <property name="destinationName" value="requestQueue" /> 
        <property name="concurrentConsumers" value="1" /> 
        <property name="receiveTimeout" value="3000" /> 
        <property name="sessionTransacted" value="true"/> 
    </bean> 

 

Note that the sessionTransacted property of the listener container must be set to true or it won't work!

 

Sending JMS Messages

For sending, again you have the option to use either the Atomikos classes or the Spring facilities...

 

With the Atomikos JmsSenderTemplate

See the examples in the Atomikos download for how to configure...

 

With Spring's JmsTemplate

The following XML snippet shows how to configure the Spring JmsTemplate to work with Atomikos.

 

    <!--  
         The underying JMS vendor's XA connection factory.  
         XA is required for transactional correctness.  
    --> 
    <bean id="xaFactory" class="org.activemq.ActiveMQXAConnectionFactory"> 
        <property name="brokerURL"> 
            <value>tcp://localhost:61616</value> 
        </property> 
    </bean> 
 
    <!--  
         The Atomikos JTA-enabled TopicConnectionFactory,  
         configured with the vendor's XA factory.  
    --> 
    <bean id="topicConnectionFactoryBean"  
          class="com.atomikos.jms.AtomikosConnectionFactoryBean"  
          init-method="init" destroy-method="close"> 
        <!--  
             The unique resource name needed for recovery  
             by the Atomikos core.  
        --> 
        <property name="uniqueResourceName"> 
            <value>TOPIC_BROKER</value> 
        </property> 
        <property name="xaConnectionFactory"> 
            <ref bean="xaFactory"/> 
        </property> 
    </bean> 
 
    <!--  
         Where do we send updated time table info to?  
         Note: this is a topic to allow multiple subscribers.  
    --> 
    <bean id="topic" class="org.activemq.message.ActiveMQTopic"> 
        <property name="physicalName"> 
            <value>TIMETABLE_TOPIC</value> 
        </property> 
    </bean> 
 
    <!--  
      JMS template for easy sending of timetable update messages with Spring  
    --> 
    <bean id="jmsTemplate"  
          class="org.springframework.jms.core.JmsTemplate"> 
        <property name="connectionFactory"> 
            <ref bean="topicConnectionFactoryBean"/> 
        </property> 
        <property name="defaultDestination"> 
            <ref bean="topic"/> 
        </property> 
        <property name="receiveTimeout" value="1000"/> 
   <property name="sessionTransacted" value="true"/> 
    </bean> 

 

Note that the sessionTransacted attribute must be set to true! For performance, you can also wrap the connection factory in Spring's SingleConnectionFactory instead. This will reuse the same connection for sending in the JmsTemplate.

 

Accessing the Database

The database can be accessed in a variety of ways.

 

Configuring Atomikos DataSource

See the examples/demos included in the download for how to configure a data source in Spring.

 

Spring's JdbcTemplate

It suffices to make sure that the template's datasource is an Atomikos instance...

 

Spring's HibernateTemplate

The following XML fragment shows how to configure the HibernateTemplate for JTA transactions with Atomikos.

 

<bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 
   <property name="mappingResources"> 
   <list> 
   <!-- list all your Hibernate mapping file locations here --> 
   <value>...</value> 
   ... 
   </list> 
   </property> 
 
   <!--  
        IMPORTANT: make sure to refer to an ATOMIKOS  
        JTA/XA datasource for the sessionFactory!  
   --> 
   <property name="dataSource"><ref bean="datasource"/></property> 
 
   <!-- IMPORTANT: make sure to tell Hibernate to use JTA --> 
   <property name="hibernateProperties"> 
   <props> 
   <prop key="hibernate.dialect">...</prop> 
   <prop key="hibernate.connection.isolation">3</prop> 
   <prop key="hibernate.current_session_context_class">jta</prop> 
   <prop key="hibernate.transaction.factory_class"> 
       org.hibernate.transaction.JTATransactionFactory 
   </prop> 
   <prop key="hibernate.transaction.manager_lookup_class"> 
      com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup 
   </prop> 
   </props> 
   </property> 
 
</bean> 
 
<!--  
     Configure the Hibernate template  
     with the resulting sessionFactory  
--> 
<bean id="hibernateTemplate"  
   class="org.springframework.orm.hibernate3.HibernateTemplate"> 
   <property name="sessionFactory"><ref bean="sessionFactory"/></property> 
</bean> 

 

Spring JMS Pitfalls

You should take care to set sessionTransacted to true for the JmsTemplate and the listener containers.

 

JMX Administration of Atomikos Transactions

From release 3.3 on, Atomikos can be configured for administration in JMX. The following shows how this works with Spring and JDK 1.5 or higher.

 

<!--  
     Configure the Atomikos JMX transaction  
     service to administer pending transactions  
--> 
<bean id="jmxTransactionService"  
      class="com.atomikos.icatch.admin.jmx.JmxTransactionService"> 
    <!-- Optional: show only heuristic problem cases --> 
    <property name="heuristicsOnly" value="true"/> 
</bean> 
 
<!-- Spring JMX config --> 
<bean id="mbeanServer"  
      class="org.springframework.jmx.support.MBeanServerFactoryBean"/> 
 
<!--  
     Export the Atomikos JMX transaction service  
     to the local JMX service in the running VM (1.5 or higher)  
--> 
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> 
        <property name="beans"> 
            <map> 
                <entry key="atomikos:name=tx-service"> 
                    <ref bean="jmxTransactionService"/> 
                </entry> 
            </map> 
        </property> 
        <property name="server"> 
            <ref bean="mbeanServer"/> 
        </property> 
</bean> 

Also see JMX Instrumentation of Active Transactions for additional VM settings.

 

Setting Atomikos System Properties in Spring

The Spring Bean configuration of the class UserTransactionServiceImp described above can be used to set the Atomikos JTA properties. However there are three JVM (or System) properties that cannot be set through the constructor of the UserTransactionServiceImp class or via any programmatic means. These properties are:

 

  • com.atomikos.icatch.file
  • com.atomikos.icatch.no_file
  • com.atomikos.icatch.hide_init_file_path

Fortunately, it is possible to set the value of system properties within a Spring configuration file through the use of Spring's MethodInvokingFactoryBean class. For example:

<bean id="setMyAtomikosSystemProps"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <!-- System.getProperties() -->
            <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetClass" value="java.lang.System" />
                <property name="targetMethod" value="getProperties" />
            </bean>
        </property>
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
            <!-- The new Properties -->
            <util:properties>
                <prop key="com.atomikos.icatch.file">/etc/myapp/jta.properties</prop>
                <prop key="com.atomikos.icatch.hide_init_file_path">true</prop>
            </util:properties>
        </property>
    </bean>

In order for this to work, the Atomikos beans must add the Spring attribute "depend-on=" and add the Id of this bean. This will ensure the system property values are set prior to the initialization of Atomikos.

 

Optimizing the Init and Close Order for Recovery

In order to maximize recovery and shutdown/restart ability, it is highly recommended that you configure your Spring configuration with the following init dependencies:

 

  • Make the transaction manager depends on all your JDBC and JMS connection factories: this ensures that connections are kept until AFTER the transaction manager has terminated all transactions during shutdown.
  • Make any MessageDrivenContainer depend on the transaction manager, to ensure that all JMS listener sessions have terminated before the transaction manager starts shutting down.
分享到:
评论

相关推荐

    atomikos-util-3.7.0.jar.zip

    8. **Spring框架集成**:对于使用Spring框架的开发者,Atomikos 提供了易于使用的适配器,使得在Spring应用中配置和使用事务管理变得简单。 9. **测试支持**:在开发和测试环境中,Atomikos 提供了一些工具和模拟...

    atomikos-Spring-Mybatis(Oracle,Mysql)

    Atomikos 是一款开源的事务处理管理器,它支持分布式事务处理,特别是在Java应用程序中,如Spring和Mybatis框架的整合。本项目“atomikos-Spring-Mybatis(Oracle,Mysql)”着重于在Spring和Mybatis框架下,结合Oracle...

    java+spring+mybatis+mysql+RuoYi-atomikos-实现分布式事务.zip

    8. **集成与配置**: 集成Atomikos到Spring Boot项目中,需要在配置文件中设置相关的事务管理属性,比如数据源、事务超时时间等,并且需要在代码中适当地声明和管理事务边界。 9. **测试与监控**: 在分布式事务环境...

    atomikos_3.7_jta项目整合jar包

    此外,还需要在应用服务器或者Spring等框架的配置文件中配置Atomikos事务管理器,例如在Spring中,你可以使用`AtomikosTransactionManager`作为`PlatformTransactionManager`的实现。 总的来说,Atomikos 3.7 JTA...

    atomikos-transactions-jdbc.jar.zip

    在实际项目中,结合Spring框架或其他依赖注入容器,Atomikos可以无缝集成,实现声明式事务管理,进一步降低开发难度。总的来说,Atomikos Transactions JDBC是Java分布式系统中的一个强大武器,值得每个Java开发者...

    atomikos-transactions-jta.jar.zip

    5. **易于集成**:Atomikos 提供了简单的API和配置选项,可以轻松地集成到Spring、Hibernate等框架中,简化开发工作。 `atomikos-transactions-jta.jar` 文件是Atomikos Transactions JTA库的二进制发行版,包含...

    atomikos-transactions-hibernate3.jar.zip

    Atomikos Transactions for Hibernate3.jar是专门为Hibernate3集成的版本,它允许开发者在使用Hibernate进行数据库操作时,享受到事务管理的高级功能。Hibernate3是一个流行的对象关系映射(ORM)框架,它简化了Java...

    atomikos-jta-jdbc-jms-example:带有 Spring、JDBC 和 JMS 的 Atomikos JTA 示例

    在这个"atomikos-jta-jdbc-jms-example"示例中,我们将会深入理解如何在 Java 应用程序中集成 Atomikos 来实现 JTA(Java Transaction API)、JDBC(Java Database Connectivity)和 JMS(Java Message Service)的...

    atomikos-3.7.0

    7. **易于集成**:Atomikos 提供了简单易用的API和配置选项,方便开发者将其集成到现有的Java应用程序中。它兼容Spring、EJB、Hibernate等常见的Java框架。 8. **故障恢复与高可用性**:Atomikos 提供了故障检测和...

    atomikos-jta-camel-jpa-jms-example:带有 Camel、Spring、JPA 和 JMS 的 Atomikos JTA 示例

    这是一个关于集成 Atomikos JTA(Java Transaction API)与 Apache Camel、Spring、JPA 和 JMS 的示例项目。在IT行业中,这样的集成是构建分布式、事务性应用程序的关键技术,尤其是在处理多数据源和消息传递时。让...

    atomikos_3.7_jta JAR包下载

    描述中提到的"SpringMVC+Atomikos实现分布式事务管理"意味着SpringMVC,一个流行的用于构建MVC(模型-视图-控制器)结构Web应用程序的框架,可以通过与Atomikos的集成来支持复杂的分布式事务。SpringMVC框架提供了对...

    atomikos-jta-jpa-jms-example:带有 Spring、JPA 和 JMS 的 Atomikos JTA 示例

    Atomikos 是一个开源的事务处理管理器,它支持 Java Transaction API (JTA) 和 Java Messaging Service (JMS),并且可以与 Spring 框架无缝集成。本示例项目 "atomikos-jta-jpa-jms-example" 旨在演示如何在分布式...

    Spring多数据源atomikos所有jar包

    在Spring中集成Atomikos,可以让应用程序在多个数据源之间进行协调一致的事务操作,保证数据的一致性和完整性。 首先,我们需要了解Spring如何配置多数据源。在Spring中,可以通过定义不同的DataSource bean来配置...

    dynamic-datasource-spring-boot-starter-master.zip

    "dynamic-datasource-spring-boot-starter-master"项目的核心在于提供了一个快速启动的SpringBoot扩展,帮助开发者快速集成多数据源环境。以下是一些关键知识点: 1. **配置文件**:在项目的配置文件(如...

    Spring3.1+Hibernate4.1+Atomikos3.9+MySql5.1+Tomcat5/6/7实现JTA事务管理

    标题中的“Spring3.1+Hibernate4.1+Atomikos3.9+MySql5.1+Tomcat5/6/7实现JTA事务管理”揭示了一个集成开发环境,用于构建分布式、事务一致性的Java应用程序。这个组合利用了Spring框架的依赖注入和AOP(面向切面...

    spring+mybatis+atomikos配置所需包跟配置文件

    4. 配置MyBatis:MyBatis需要与Spring集成,使用Spring管理SqlSessionFactory,同时需要配置使用Atomikos的数据源。 ```xml &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt; ...

    分布式事务管理SpringBoot集成Atomikos使用Oracle数据库mybatis、jta框架.rar

    SpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatisSpringBoot集成Atomikos使用Oracle数据库mybatis

    AtomikosTransactions jar 包

    9. **集成友好**:Atomikos Transactions jar 可以无缝集成到Spring、EJB等企业级框架中,提供事务管理服务。此外,它还提供了与多种中间件和数据库的适配器,如Oracle、MySQL、IBM DB2等,简化了开发过程。 10. **...

    spring-boot-atomikos.rar

    综上所述,"spring-boot-atomikos.rar"是一个关于Spring Boot集成Atomikos实现分布式事务的综合资源,包含理论知识、代码实践和源码解析,对于理解和应用分布式事务具有很高的价值。通过学习这个资源,开发者将能更...

Global site tag (gtag.js) - Google Analytics