Wednesday, November 16, 2016

Distributed Transaction (XATransaction) For JMS Trasnport- WSO2 ESB 5.0.0

Distributed Transaction (XATransaction) For JMS Trasnport- WSO2 ESB 5.0.0

Distributed transactions plays a major role in today's business world. It adds a very important functionality to your system, which you will only see in a system crash. Having good transactional support in your system will definitely save your business in a system crash.

Distributed JMS transaction

An external transaction manager manages the coordination of the transaction. Designing and using JMS distributed transactions is more complex than using local JMS transactions.

The transaction manager is the primary component of the distributed transaction infrastructure and distributed JMS transactions are managed by the XAResource enabled transaction manager in the J2EE application server. Also note that you need to check if your message broker supports XA transactions.

XA two-phase commit process

The two-phase commit consists of the following steps:
  • Immediately after the transaction begins, the transaction manager invokes start() on the JMS XA resource, which indicates that the resource should initialize a new transaction. The JMS XA resource now generates a new transaction ID and sends it over the network to the remote broker.
  • The JMS XA resource now forwards all of the operations that arise during a JMS session (for example, messages, acknowledgments, and so on) to the remote broker.
  • On the broker side, the received operations are not performed immediately. Because the operations are happening in a transaction context and the transaction is not yet committed, the broker buffers all of the operations in a transaction store (held in memory, initially). Messages held in the transaction store are not forwarded to JMS consumers.
  • In a two-phase commit process, the first phase of completing the transaction is where the transaction manager invokes prepare() on all of the participating XA resources. At this stage, the JMS XA resource sends the prepare() operation to the remote broker.
  • On the broker side, when the transaction store receives the prepare() operation, it writes all of the buffered operations to disk. Hence, after the prepare phase, there is no longer any risk of losing data associated with this transaction branch.
  • The second phase of completing the transaction is where the transaction manager invokes commit() on all of the participating XA resources. The JMS XA resource sends the commit() operation to the remote broker.
  • On the broker side, the transaction store marks this transaction as complete. The pending operations are now executed and any pending messages can now be forwarded to JMS consumers.
Please follow documentation to configure WSO2 esb with activemq browser.
XA transaction example.

ESB listen to message queue and send that message to multiple queues.  if something went wrong in sending the message to one of those queues, the original message should be rollbacked to Listening queue and none of the queues should receive the message. The entire transaction should be rollbacked.

The following example code shows the configuration of ESB which listen to a JMS queue and consume messages as well as to send messages to multiple JMS queues in transactional manner.

<proxy xmlns=""
       transports="https http jms"
         <property name="OUT_ONLY" value="true"/>
         <log level="custom">
            <property name="MESSAGE_ID_A" expression="get-property('MessageID')"/>
         <log level="custom">
            <property name="BEFORE" expression="$body"/>
         <property name="MESSAGE_ID_B"
         <property name="failureResultProperty"
            <result xmlns="">failure</result>
            <source clone="true" xpath="$ctx:failureResultProperty"/>
            <target type="body"/>
         <log level="custom">
            <property name="AFTER" expression="$body"/>
         <property name="BEFORE1" value="ABCD" scope="axis2" type="STRING"/>
         <callout serviceURL="jms:/ActiveMQPublisher1?transport.jms.ConnectionFactoryJNDIName=XAConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://localhost:61616&amp;transport.jms.DestinationType=queue;transport.jms.TransactionCommand=begin">
            <source type="envelope"/>
            <target xmlns:s12=""
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
         <callout serviceURL="jms:/ActiveMQPublisher2?transport.jms.ConnectionFactoryJNDIName=XAConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://localhost:61616&amp;transport.jms.DestinationType=queue">
            <source type="envelope"/>
            <target xmlns:s12=""
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
         <callout serviceURL="jms:/ActiveMQPublisher3?transport.jms.ConnectionFactoryJNDIName=XAConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://localhost:61616&amp;transport.jms.DestinationType=queue;transport.jms.TransactionCommand=end">
            <source type="envelope"/>
            <target xmlns:s12=""
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
         <log level="custom">
            <property name="Transaction Action" value="Rollbacked"/>
         <callout serviceURL="jms:/ActiveMQPublisherFault?transport.jms.ConnectionFactoryJNDIName=XAConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://localhost:61616&amp;transport.jms.DestinationType=queue;transport.jms.TransactionCommand=rollback">
            <source type="envelope"/>
            <target xmlns:s12=""
                    xpath="s11:Body/child::*[fn:position()=1] | s12:Body/child::*[fn:position()=1]"/>
   <parameter name="transport.jms.ContentType">
   <parameter name="transport.jms.Destination">MyJMSQueue</parameter>

To place a message into a JMS queue, execute following command from /samples/axis2Client directory.

ant stockquote -Dmode=placeorder -Dtrpurl="jms:/MyJMSQueue?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.ContentTypeProperty=Content-Type&transport.jms.DestinationType=queue"

You should be able to observe ESB consumes messages from MYJMSQueue and sends to multiple queues. To check rollback functionality provide unreachable host name to any destination queue and save the configurations. You should be able to observe ESB fault sequence getting invoked and failed message delivered to configured destination within fault sequence
  • The problem with XA is it can be a bit slow; as the XA protocol requires multiple syncs to disk to ensure it can always recover properly under every possible failure scenario. This adds significant cost (in terms of latency, performance, resources and complexity). Also quite a few EJB servers and databases don't actually properly support XA
  • So a good optimisation is to use regular JMS transactions - with no XA - and just perform some duplicate message detection in your code to check you have not already processed the message. (ref ActiveMQ)


  1. This is a very informative blog post especially for the new businesses. You have shared a precaution as well a cure for the problem. Great job

  2. This comment has been removed by the author.

  3. It is basic for an online dealer record to have a transaction handling arrangement so as to transact business online. From tolerating a request, preparing payments and getting the money tucked neatly away, transaction programming is the thing that will take care of business.소액결제현금화

  4. Bolavita - Situs Taruhan Bola | Live Casino | Sabung Ayam Online | Slot Games |Togel Online Aman Dan Terpercaya

    Minimal Depo & WD Hanya IDR 25.000,- *

    Melayani Deposit Pulsa Telkomsel | XL | Gopay | OVOpay |LinkAja | DANA Serta Semua Bank Indonesia

    Bonus New Member IDR 500.000

    Bonus Cashback 10%

    Bonus Flash Deposit Spesial Hari Jumat( Happy Friday)

    Bonus 8x Win Sabung Ayam S128 SV388

    Bonus 8x Win Sexy Baccarat Casino Online

    Kontak :
    • Telegram :
    • WA Bolavita :
    • Wechat : Bolavita
    • Line : cs_bolavita

  5. Online businesses are on the rise thus necessitating methods of payment and banks that can be used to transact deals between the individual businesses and their consumers.소액결제현금화