Friday, February 5, 2016

Apache Camel - How to trigger Compensation Handler? (7/250-2016)





Compensation handler in camel is based on the concept of Unit of Work — group together multiple tasks as a single unit — mimicking transnational boundaries.
  • org.apache.camel.spi.UnitOfWork Interface represents UnitOfWork in Camel.
  • Exchange has exactly one UnitOfWork (private to Exchange — not shared with others) — accessed from exchange using method — getUnitOfWork
  • Camel will automatically inject a new UnitOfWork into Exchange — using internal processor UnitOfWorkProcessor — involved in start of every route
  • At the end of the route, the processor invokes registered Synchronization callback

Synchronization (a callback)
  • org.apache.camel.spi.Synchronization Interface is used to represent Synchronization.
  • used to execute custom logic — after processing — using the methods below.
onComplete
onFailure
  • An exchange can add multiple callBacks.
  • If there is an error while processing callBack — Camel will log in WARN level and continue to next callback.
Synchronization is added / removed to the UnitOfWork using the methods below — using a processor.
addSynchronization
removeSynchronization
done — invoked when UnitOfWork is complete
OR
addOnCompletetion
This is a method on the exchange — it creates a new thread — and is the preferred method for compensation handling.
addOnCompletion can be written in Java DSL as well as Spring DSL.
It can be route / context scoped.

References:

No comments: