You can easily understand the utilization of Compensate and compensate scope from its meaning itself. Compensation occurs when BPEL process cana��t complete all process after completing some processes or transactions. BPEL process can use compensation activity by using compensation handler.
For example, if BPEL process is designed as calculator operation (addition, subtraction, division) and for division operation the second input number is zero then the whole BPEL process will perform compensation activity by showing all the output of operations as -2.
- Create a separate application for exception example. Go to File -> New -> Application. Select SOA Application under General category. Enter application name CompensateScopeApplication
- Click on Next button and enter project name CompensateScopeDemo. Select Composite With BPEL Process in the next wizard.
- Click on Finish button and BPEL creation wizard will open. Enter BPEL process name CalculatorBPELProcess and select synchronous BPEL process in template field. Select the checkbox Expose as a SOAP service and input & output value will be remain default.
- Add one more element under process element
- Create three variables in BPEL process
- Place a scope between receiveInput and replyOutput. Add three more scopes within this scope and add three assign activity within each scope like below screenshot.
- Initialize the three variables to -1 in Assign activity before all the three scopes.
- Edit each assign activity within the scopes. Add the below three expression in three assign activities. A� A� A� A� A� $inputVariable.payload/client:number1 + $inputVariable.payload/client:number2 A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� $inputVariable.payload/client:number1 – $inputVariable.payload/client:number2 A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A� A�$inputVariable.payload/client:number1 div $inputVariable.payload/client:number2
- Add Compensation Handler in two scope (addition scope and subtraction scope)
- Drag and drop Assign activity in each Compensation Handler and assign -2 to addResult and subResult
- Add an IF condition under division scope. Drag and drop IF condition after Assign_divOutput like the below screenshot
- Add condition in IF condition like when second input variable is zero
- Now throw the exception when second input will be zero. Drag a Throw activity under IF container
- Double click on Throw activity and editor window will open. Enter any name of throw activity and enter DivisionByZeroURI as Namespace URI and DivisionByZeroLP as Local Part
- Add a Catch block in the division scope
- Edit the Catch activity like below screen
- Add an Assign activity in the Catch Block. Assign a value -2 to the variable divResult. Again throw this exception to the Catch All block in the outer scope
- Select Compensate from BPEL construct and drag and drop it in the Catch All block
- Add an Assign activity before replyOutput and add an expression like below screenshot
- Deploy the project and test it in EM console. Enter second number as 0 and you will get the below output as all the scope will be compensated by the compensation handler and compensate activity. A� Now we will discuss about the Compensate Scope activity.
- Delete compensate activity from catch all block and drag and drop a Compensate Scope in place of compensate.
- Double click on compensate scope and editor window will open. Select scope2 as target scope
- Again deploy the project and test it by giving same input. Now this time output will be different. Only scope2 (subtraction operation) will be compensated with -2 and scope1 (addition operation) will give correct result. As target scope is scope2 in the compensate scope, only scope2 will be compensated with the value -2.