Tuesday, October 21, 2014

Transaction in Camel

Transaction => it’s all or nothing

Transaction represent sequence of step to be performed and either all the steps will be completed successfully or none.  Transactions conform to ACID  properties


By default, the JMS consumer uses auto-acknowledge mode, which means the client acknowledges the message when it’s received (i.e consumed by consumer), and the message is dequeued from the JMS broker.

But JMS consumer can use transacted acknowledge mode. which means the client acknowledges the message when it’s is able to succefully consume it, and only then the message is dequeued from the JMS broker.


In transacted acknowledge mode when a message has been consumed from the queue and fed into the Camel application, Camel issues a begin signal to the JmsTransactionManager. Depending on whether the Camel route completes consuming the message with success or failure  the JmsTransactionManager will ensure the JMS broker commits or rolls back(i.e dequeue or keep the message).


When you specify <transacted/> in a route, Camel uses transactions for that particular
route. Under the hood, Camel looks up the Spring transaction manager and leverages
it. This is the convention over configuration kicking in.

NOTE : When using transacted() in the Java DSL, you must add it right
after from() to ensure that the route is properly configured to use transactions.
This isn’t enforced in the DSL because the DSL is loosely defined to
make it easy to maintain and develop Camel. There are a few tradeoffs such
as this.


Example of correct transacted route:
   from("jms:test:queue")
         .transacted()
          //other operation
         .end();

Incorrect transacted route:
     from("jms:test:queue")
          .routeId("testRoute") // this will casue issue  as transacted() should be added right after from()
         .transacted()
          //other operation
         .end();





Refer : Chapter 9 "Using Transaction" in "Camel in Action"
Refer : transactional-client.html

No comments:

Post a Comment