WSO2 BPS - Sample Guide - Compensation Handler

Sample Guide - Compensation Handler

Table of Content


Reversing the effects of a service invocation in a case of a later failure is meant as compensation. The act of undoing any actions that were performed by a successfully completed compensatable activity because of an exception that occurred elsewhere in a workflow is meant as compansation handling. It is represented using <compensationHandler/> construct.


Overall Idea

The following application does two things. First, it books an airline ticket and then it books a hotel. If the hotel booking fails, then the airline booking should be cancelled. The business process can be shown as below.

<process><faultHandlers><catch name="BookHotelFault"><!--Since the hotel booking failed, cancel the airline booking by calling the compensation handler of the scope that did the airline booking--><compensateScope scope="BookAirlineTktScope"/></!--Notify that the booking failed---><reply name="SendBookingFailedReply"></catch></faultHandlers><receive name="DoBooking"/><scope name="A"><compensationHandler><!--Cancel the airline booking--><invoke name="CancelAirlineTkt"/></compensationHandler><!--Book airline ticket--><invoke name="BookAirlineTkt"/></scope><scope name="B"><!--Book hotel--><invoke name="BookHotel"/></scope><!--Both bookings are successful. Notify--><reply name="SendBookingSuccessReply"/></process>

At A's scope, does the airline booking. At B's scope, does the hotel booking. The hotel booking may fail which happens when the invoke "BookAirlineTkt" fails and throws a fault "BookHotelFault". This fault is caught by the catch in the process's fault handler. Since the hotel booking failed, we need to cancel the airline booking. Therefore, at the catch block we call the compensation handler of scope A, which has the logic to cancel the airline booking. <compensateScope> activity is used to specify this. So, when the catch executes <compensateScope>, it calls the scope A's compensation handler which in turn invokes the webservice to cancel the airline booking.