Below is our IntegrationFlow
, kindly suggest how 2-phase commit or best-efforts 1-phase commit can be implemented for the 2 databases involved. Thanks
MQ
Adapter --> Jpa.updatingGateway
(db1) & Jpa.updatingGateway
(db2)
db1 & db2 operations should commit or rollback together
For two different databases (and I assume for MQ as well) you need to use a org.springframework.transaction.jta.JtaTransactionManager
. It is up to you and your environment to determine how you are going to obtain XA transaction: JNDI from some application server like IBM WebSphere or using Atomikos.
Then you configure your JMS Inbound Endpoint to use that transaction manager and all the downstream flow will participate in it, if it is connect with direct channels.
Your two Jpa.updatingGateway()
can be subscribed to the same PublishSubscribeChannel
or you can use a routeToRecipients()
.
For example:
@Bean
public IntegrationFlow jmsMessageDrivenRedeliveryFlow() {
return IntegrationFlow
.from(Jms.messageDrivenChannelAdapter(mqFactory)
.destination("myQueue")
.configureListenerContainer(c -> c.transactionManager(jtaTransactionManager)))
.publishSubscribeChannel(publishSubscribeSpec -> publishSubscribeSpec
.subscribe(subFlow1 -> subFlow1.handle(Jpa.updatingGateway(db1)))
.subscribe(subFlow2 -> subFlow2.handle(Jpa.updatingGateway(db2))))
.get();
}