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

2022-01-19 00:00:00 spring oracle apache-camel ibm-mq

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

I am in the process of developing a stand alone Apache camel application (not running on a J2EE container). This apps needs to be capable of routing messages from an IBM MQ queue manager to an Oracle database in a distributed transaction. My google searches pretty much took me to a few places but none of those were able to give me some good clues about how to put everything together. This link below was the closest to what I need but unfortunately it is not cler enough to put me on the right path.

IBM MQManager 作为 XA 事务带有 Spring-jms 和 Spring-tx 的管理器

提前感谢您的意见.

推荐答案

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

You will need to use a JTA TransactionManager, but since not being in a j2ee container i would sugest to use Atomikos.

https://github.com/camelinaction/camelinaction/tree/master/第9章/xa

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

My route which is working with IBM MQ -> Oracle Database, in an J2EE yes but still should be working with Atomikos setup. I would say this isn't the proper way to to it, but it's the only way I managed to get it working - working well enough for my use case.

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 的情况下会回滚.

The commit are handled by the transaction manager, so if I actually get an error with the database commit, it should rollback the message. Next problem that I have had with rollbacking messages is that I can't set the deadLetterQueue, as you can with ActiveMQ. So it rolls back to the incoming queue. Therefore I actually handle the database transaction as I do, it is in a new transaction, which is rollbacked in case of a normal SQLException when calling the database.

我希望这能奏效:

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

我已经在社区论坛上发布了有关此问题的信息,但根本没有答案.

I have posted about this in the community forums but no answers at all.

相关文章