JMS XA Transactions with Atomikos
When considering reliable message delivery in JMS, there are two main categories.
JMS Acknowledgements comes in three types,
JMS Transactions are in two types
- Local Transactions
- Distributed(XA) Transaction
This post I’m trying to give a brief explanation on XA transactions in JMS and give a sample source code I have used to try out JMS Distributed transactions with ActiveMQ and Atomikos.
In JMS Classic API our approach to send/receive messages is by,
- Looking up ConnectionFactory instance
- Create Connection using the ConnectionFactory
- Create Session using the Connection
- Create Producer/Consumer using the Session
- Send/Receive Messages
When comes to JMS XA, we have similar sets of objects
- XAConnection inherited from Connection
- XASession inherited from Session (single XASession will hold one Session and one XAResource)
Note: XAResource is used by the Transaction Manager to control the transaction.
So when we are doing an XA transaction following are the main steps to follow,
- Lookup for the XAConnectionFactory(s) instance
- Create XAConnection(s) object using the XAConnectionFactory(s)
- Create XASession(s) using the XAConnection(s)
- Create/Locate Distributed transaction manager and (get the transaction) and enlist the XAResource(s)
- Begin the transaction
- Get Session from the XASession and create Message Producers/Consumers, and send/receive messages
- Commit/Rollback tranaction
Following is the sample source code I have used to demonstrate this flow. In the sample I have used only single XAResource and performed a single operation.
jms-xa-sample - Sample code to do distributed transaction based XA Operationsgithub.com
You can enlist multiple XAResources to the Transaction Manager and perform transaction, by extending this further.