2,Camel里的全局事务,一个route里有多个事务参与者,示例图如下:

route的定义如下:
public class XaTransaction extends RouteBuilder {
public void configure() throws Exception {
TProcessor1 p1 = new TProcessor1();
from("jms:queue:TOOL.DEFAULT")
.transacted()
.log("+++ before database +++")
.bean(SQLBean.class, "toSql")
.to("jdbc:myDataSource")
.process(p1)
.log("+++ after database +++");
}
}
public class SQLBean {
public String toSql(String str) {
//create table CamelTEST(msg varchar2(2000));
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO CamelTEST VALUES ('camel test')");
return sb.toString();
}
}
route的逻辑是从queue里取消息,然后操作数据库,然后做后续其他操作(process(p1)),这里的process(p1)如果抛出异常的话,取消息和数据库操作都回滚,
如果整个route都成功完成的话,取消息和数据库操作提交.
这里用到JTA transaction manager是atomikos,相应的jar包可以从这里下载:
下载在Linux公社的1号FTP服务器里,下载地址:
FTP地址:ftp://www.linuxidc.com
用户名:www.linuxidc.com
密码:www.muu.cc
在 2012年LinuxIDC.com\2月\Apache Camel框架之事务控制
下载方法见 http://www.linuxidc.net/thread-1187-1-1.html
atomikos的主页 http://www.atomikos.com/Main/ProductsOverview
Spring的配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:broker="http://activemq.apache.org/schema/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>
com.test.camel.transaction.xa
</package>
</camelContext>
<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>
<!-- this is some atomikos setup you must do -->
<bean id="atomikoSUSErTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" >
<property name="transactionTimeout" value="300"/>
</bean>
<!-- this is some atomikos setup you must do -->
<bean id="connectionFactory"
class="com.atomikos.jms.AtomikosConnectionFactoryBean" >
<property name="uniqueResourceName" value="amq1"/>
<property name="xaConnectionFactory" ref="jmsXaConnectionFactory"/>
</bean>
<!-- this is the Spring JtaTransactionManager which under the hood uses Atomikos -->
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction"/>
</bean>
<!-- Is the ConnectionFactory to connect to the JMS broker -->
<!-- notice how we must use the XA connection factory -->
<bean id="jmsXaConnectionFactory" class="org.apache.activemq.ActiveMQXAConnectionFactory" >
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<!-- define the activemq Camel component so we can integrate with the AMQ broker below -->
<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent" >
<property name="transacted" value="true"/>
<property name="transactionManager" ref="jtaTransactionManager"/>
</bean>
<bean id="myDataSource"
class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<!-- set an arbitrary but unique name for the datasource -->
<property name="uniqueResourceName"><value>XADBMS</value></property>
<property name="xaDataSourceClassName">
<value>Oracle.jdbc.xa.client.OracleXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="user">xxx</prop>
<prop key="password">xxx</prop>
<prop key="URL">jdbc:oracle:thin:@147.151.240.xxx:1521:orcl</prop>
</props>
</property>
<property name="poolSize" value="1"/>
</bean>
</beans>
