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).
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 adds the possibility to specify everything, including JTA properties and optional log administrators to manage the transaction logs.
<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> </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>
com.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
PROPAGATION_NESTED
since that one depends on the Spring DataSourceTransactionManager
strategy (incompatible with JTA/XA).
You can receive messages either with the Atomikos receiver sessions, or with the Spring message listener containers.
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>
For sending, again you have the option to use either the Atomikos classes or the Spring facilities…
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>
The database can be accessed in a variety of ways.
datasource
is an Atomikos instance…
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>
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.
<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 "depends-on=" and add the Id of this bean. This will ensure the system property values are set prior to the initialization of Atomikos.
In order to maximize recovery and shutdown/restart ability, it is highly recommended that you configure your Spring configuration with the following init dependencies:
Adding these dependencies to your Spring config can be error-prone (especially if you change things afterwards). Our commercial product takes care of this automatically for you - so there is no room for mistakes. With a free trial you can experiment, PLUS we'll support you if needed. Interested? Click below to start your free trial:
Free Trial