以 IBM MQ 和 Oracle 作为资源的独立 spring 应用程序 XA 事务

Standalone spring app XA transactions with IBM MQ and Oracle as resources

我正在开发一个独立的 Apache camel 应用程序(不是 运行 在 J2EE 容器上)。 此应用程序需要能够在分布式事务中将消息从 IBM MQ 队列管理器路由到 Oracle 数据库。 我的 google 搜索几乎把我带到了几个地方,但其中 none 能够为我提供一些关于如何将所有内容放在一起的好线索。 下面的 link 最接近我的需要,但不幸的是,它不够清晰,无法让我走上正确的道路。

IBM MQManager as XA Transaction Manager with Spring-jms and Spring-tx

提前感谢您的意见。

您将需要使用 JTA TransactionManager,但由于不在 j2ee 容器中,我建议使用 Atomikos。

https://github.com/camelinaction/camelinaction/tree/master/chapter9/xa

我的路线是使用 IBM MQ -> Oracle 数据库,在 J2EE 中是,但仍然应该使用 Atomikos 设置。我会说这不是正确的方法,但这是我设法让它工作的唯一方法 - 对于我的用例来说工作得很好。

from(inQueue)
    .transacted()
    .setHeader("storeData", constant(false))
    .to("direct:a")
    .choice().when(header("storeData").isEqualTo(false)) // if this is true the database calls are 'successful'
    .log("Sending message to errorQueue")
    .to(errorQueue)
;

StoreDataBean storeDataBean = new StoreDataBean();
from("direct:a")
    .onException(Exception.class).handled(false).log("Rollbacking database changes").markRollbackOnlyLast().end()
    .transacted("PROPAGATION_REQUIRES_NEW")
    .bean(storeDataBean) //storeData sets the header storeData to true if no SQLException or other exceptions are thrown
    .end()
;

提交由事务管理器处理,所以如果我在提交数据库时确实遇到错误,它应该回滚消息。 我在回滚消息方面遇到的下一个问题是我无法设置 deadLetterQueue,而您可以使用 ActiveMQ。所以它回滚到传入队列。所以我实际上是按照我的方式处理数据库事务,它是在一个新的事务中,在调用数据库时出现正常SQLException的情况下回滚。

我希望这能奏效:

from(inQueue)
    .onException(Exception.class).to(errorQueue).markRollbackOnly().end()
    .bean(storeDataBean)
    .end()

我已经在社区论坛上发布了关于此的帖子,但根本没有答案。