Apache Camel 和 Apache ActiveMQ 中的 XA 事务

XA transaction in Apache Camel and Apache ActiveMQ

回滚事务不起作用。

我尝试在bean中设置参数ActiveMQComponent:

如何解决这个错误?

日志:

[ad #8 - JmsConsumer[Caosqueue]] TransactionErrorHandler        WARN  Transaction rollback (0x67756120) redelivered(false) for (MessageId: ID:stws2327-61437-1452674698638-1:3:1:1:1 on ExchangeId: ID-stws2327-61436-1452674698403-0-3) due exchange was marked for rollbackOnly
[ad #8 - JmsConsumer[Caosqueue]] EndpointMessageListener        WARN  Execution of JMS message listener failed. Caused by:      [org.apache.camel.RuntimeCamelException - org.apache.camel.RollbackExchangeException: Intended rollback. Exchange[Message: two]]

路线:

<!-- Next route-->
<route autoStartup="true" errorHandlerRef="myErrorHandler" id="OperDayRoute">
  <from uri="jms:Caosqueue?transacted=true" />
  <transacted ref="PROPAGATION_REQUIRED" />
  <setHeader headerName="body">
     <simple>${body}</simple>
  </setHeader>
  <to uri="sql:{{sql.insertBody}}"/>
  <convertBodyTo type="java.lang.String" />
  <choice>
    <when>
      <simple>${headers.CamelSqlUpdateCount} != 1</simple>
      <log message="PROCESSING RESULT IS ${body} - OK" />
    </when>
    <otherwise>
      <log message="PROCESSING RESULT IS ${body} - NO OK!" />
      <camel:rollback markRollbackOnly="true" />
      <throwException ref="IllegalRez" />
    </otherwise>
  </choice>
</route>

这不是问题。默认情况下,Camel 在 WARN 日志级别下的日志中报告回滚。如果您愿意,可以将其更改为 DEBUG

在你的 Spring XML:

<bean id="transactionErrorHandler" class="org.apache.camel.spring.spi.TransactionErrorHandlerBuilder">
    <property name="rollbackLoggingLevel" value="DEBUG"/>
    <property name="transactionTemplate" ref="PROPAGATION_REQUIRED"/>
</bean>

<!-- the standard spring transaction template for required -->
<bean id="PROPAGATION_REQUIRED" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="jmsTransactionManager"/>
</bean>

Camel 应该会自动拾取它。如果没有,您可以通过 route 元素的 errorHandlerRef 属性在您的路由中明确设置它。