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

2021-11-11 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()
;

提交是由事务管理器处理的,所以如果我真的收到数据库提交错误,它应该回滚消息.我在回滚消息方面遇到的下一个问题是我无法像使用 ActiveMQ 一样设置 deadLetterQueue.所以它回滚到传入队列.因此,我实际上像我一样处理数据库事务,它在一个新事务中,如果调用数据库时出现正常的 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.

相关文章