問題描述
我正在開發一個獨立的 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.
這篇關于以 IBM MQ 和 Oracle 為資源的獨立 Spring 應用程序 XA 事務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!