Sunday 17 July 2016

Compensaction Handler

Compensation Handler  


Some times in soa we have to deal with a complex orchestration of services, to complete a transaction. Like a travel request processing or a bulk order consisting of multiple item processing.

Now if at some part that transaction fails and we want to to rollback entire transaction if it fails, hence if at some point if customer requested for a desired hotel and that service is returning nothing available, then we have to cancel air tickets and other accommodation related bookings also. 


But these booking already been made till we come to hotel booking, hence we need to do two things, 

Happy Path  

first we need to make a happy path, which will be followed by the orchestration, if there is no issue. It will finish the job.


Rollback path 

Now we have rollback path, think at any point happy path break, like we are doing a booking for tour and we don't get return tickets or no accommodation available.  Hence we need to now rollback entire transaction, but not all the services provide transaction capabilities. so we need to declare a compensation scope which will compensate activity done in the happy path. Like if there is an mean booking in compensate we can simple reverse it. 


Example

In the example given below we have used a dummy order processing service, this service will do nothing but it will process the order and give you and status and Id. we have not done much in these processes, we have just implemented dummies. Hence its a combination of two processes. Please download it from the link.

Its upto you, you can implement more logic inside these processes. Now coming to process 





Process Orchestration
  
  In the process orchastration, we have called the order service to create the order, and post that we have called a custom throw error message. Also we have attached the compensation which is nothing but the calling of  cancellation service.
  
Catch block will catch the error and then run the compensation handler. This compensation handler is nothing but executing all the compensation handlers, till the point transaction got failed.  



Processing Running:-

  You can provide any input to the process and run it, once its failed then catch all block will run the compensation handlers and will revert the transactions. 
     

  

Play more with the services, any question is welcome.