Atomikos-Spring集成

阅读更多

Spring Integration

  • Configuring Atomikos as the Spring JTA Transaction Manager
    • The Basic Case (Pre-3.3)
    • The Advanced Case (As of 3.3)
  • Spring-Demarcated Transactions for POJOs
  • Receiving JMS Messages
    • With the Atomikos MessageDrivenContainer
    • With Spring's MessageListenerContainer
  • Sending JMS Messages
    • With the Atomikos JmsSenderTemplate
    • With Spring's JmsTemplate
  • Accessing the Database
    • Configuring Atomikos DataSource
    • Spring's JdbcTemplate
    • Spring's HibernateTemplate
  • Spring JMS Pitfalls
  • JMX Administration of Atomikos Transactions
  • Setting Atomikos System Properties in Spring
  • Optimizing the Init and Close Order for Recovery

 

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:

 

 
"AtomikosTransactionManager"  
      class="com.atomikos.icatch.jta.UserTransactionManager"  
    init-method="init" destroy-method="close"> 
 
    
   "forceShutdown" value="false" /> 
 
 
  
"AtomikosUserTransaction"  
   class="com.atomikos.icatch.jta.UserTransactionImp"> 
 
   "transactionTimeout" value="300" /> 
 
 
 
"JtaTransactionManager"  
      class="org.springframework.transaction.jta.JtaTransactionManager"> 
   "transactionManager" ref="AtomikosTransactionManager" /> 
   "userTransaction" ref="AtomikosUserTransaction" /> 
 

 

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.

 

 
"localLogAdministrator"  
 class="com.atomikos.icatch.admin.imp.LocalLogAdministrator"/> 
 
"userTransactionService"  
  class="com.atomikos.icatch.config.UserTransactionServiceImp"  
  init-method="init" destroy-method="shutdownForce"> 
     
         
         
            "com.atomikos.icatch.service"> 
              com.atomikos.icatch.standalone.UserTransactionServiceFactory 
             
         
     
    "initialLogAdministrators"> 
         
            "localLogAdministrator"/> 
         
     
 
 
 
"AtomikosTransactionManager"  
      class="com.atomikos.icatch.jta.UserTransactionManager"  
      init-method="init" destroy-method="close"  
      depends-on="userTransactionService"> 
 
    
   "startupTransactionService" value="false"/> 
 
    
   "forceShutdown" value="false" /> 
 
 
  
"AtomikosUserTransaction"  
      class="com.atomikos.icatch.jta.UserTransactionImp"   
      depends-on="userTransactionService"> 
   "transactionTimeout" value="300" /> 
 
 
 
"JtaTransactionManager"  
      class="org.springframework.transaction.jta.JtaTransactionManager"  
      depends-on="userTransactionService"> 
   "transactionManager" ref="AtomikosTransactionManager" /> 
   "userTransaction" ref="AtomikosUserTransaction" /> 
 

 

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:

 

   "xaFactory"  
              class="org.apache.activemq.ActiveMQXAConnectionFactory"> 
      "brokerURL" value="tcp://localhost:61616" /> 
    
 
    
   "ConnectionFactory"  
              class="com.atomikos.jms.AtomikosConnectionFactoryBean"  
              init-method="init" destroy-method="close"> 
      "uniqueResourceName" value="amq1" /> 
      "xaConnectionFactory" ref="xaFactory" /> 
    
 
    
   "AtomikosTransactionManager"  
              class="com.atomikos.icatch.jta.UserTransactionManager"  
              init-method="init" destroy-method="close"> 
       
      "forceShutdown" value="false" /> 
    
 
     
   "AtomikosUserTransaction"  
              class="com.atomikos.icatch.jta.UserTransactionImp"> 
       "transactionTimeout" value="300" /> 
    
 
    
   "JtaTransactionManager"  
              class="org.springframework.transaction.jta.JtaTransactionManager"> 
      "transactionManager" ref="AtomikosTransactionManager" /> 
      "userTransaction" ref="AtomikosUserTransaction" /> 
    
 
     
   "MessageListener" class="jtatest.TextOutputMessageListener" /> 
 
     
    "MessageListenerContainer" 
          class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
        "transactionManager" ref="JtaTransactionManager" /> 
        "connectionFactory" ref="ConnectionFactory" /> 
        "messageListener" ref="MessageListener" /> 
        "destinationName" value="requestQueue" /> 
        "concurrentConsumers" value="1" /> 
        "receiveTimeout" value="3000" /> 
        "sessionTransacted" value="true"/> 
     

 

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.

 

     
    "xaFactory" class="org.activemq.ActiveMQXAConnectionFactory"> 
        "brokerURL"> 
            tcp://localhost:61616 
         
     
 
     
    "topicConnectionFactoryBean"  
          class="com.atomikos.jms.AtomikosConnectionFactoryBean"  
          init-method="init" destroy-method="close"> 
         
        "uniqueResourceName"> 
            TOPIC_BROKER 
         
        "xaConnectionFactory"> 
            "xaFactory"/> 
         
     
 
     
    "topic" class="org.activemq.message.ActiveMQTopic"> 
        "physicalName"> 
            TIMETABLE_TOPIC 
         
     
 
     
    "jmsTemplate"  
          class="org.springframework.jms.core.JmsTemplate"> 
        "connectionFactory"> 
            "topicConnectionFactoryBean"/> 
         
        "defaultDestination"> 
            "topic"/> 
         
        "receiveTimeout" value="1000"/> 
   "sessionTransacted" value="true"/> 
     

 

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.

 

"sessionFactory"  
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 
   "mappingResources"> 
    
    
   ... 
   ... 
    
    
 
    
   "dataSource">"datasource"/> 
 
    
   "hibernateProperties"> 
    
   "hibernate.dialect">... 
   "hibernate.connection.isolation">3 
   "hibernate.current_session_context_class">jta 
   "hibernate.transaction.factory_class"> 
       org.hibernate.transaction.JTATransactionFactory 
    
   "hibernate.transaction.manager_lookup_class"> 
      com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup 
    
    
    
 
 
 
 
"hibernateTemplate"  
   class="org.springframework.orm.hibernate3.HibernateTemplate"> 
   "sessionFactory">"sessionFactory"/> 
 

 

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.

 

 
"jmxTransactionService"  
      class="com.atomikos.icatch.admin.jmx.JmxTransactionService"> 
     
    "heuristicsOnly" value="true"/> 
 
 
 
"mbeanServer"  
      class="org.springframework.jmx.support.MBeanServerFactoryBean"/> 
 
 
"exporter" class="org.springframework.jmx.export.MBeanExporter"> 
        "beans"> 
             
                "atomikos:name=tx-service"> 
                    "jmxTransactionService"/> 
                 
             
         
        "server"> 
            "mbeanServer"/> 
         
 

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:


        
            
            
                
                
            
        
        
        
            
            
                /etc/myapp/jta.properties
                true
            
        
    

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,Spring)