Académique Documents
Professionnel Documents
Culture Documents
com
Aim
To demonstrate use of compensation handler and compensate activity
Use case
We shall pass two integers num1 and num2. The result would tell values of num1 +
num2, num1 – num2 and num1/num2.
You can save the following embedded zip file onto your computer. It contains the code
for this project
C:\
CompensateDemo.zip
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Add a assign activity before the Operations scope and set each of the three variables to -1
In Addition, name the assign activity as add. Create a copy operation and add the two
inputs and assign that value to addResult
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num1') +
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num2')
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num1') -
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num2')
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num1') div
bpws:getVariableData('inputVariable','payload','/client:CompensateDemoP
rocessRequest/client:num2')
Now our business requirement is, if the second number is zero, then divisionResult would
be Infinity or –Infinity. We want to test for this condition in the division scope. If it is
found true, we shall throw an exception. That exception shall be caught by Operations
scope and it will ‘undo’ the values in additionResult and subtractionResult, and set them
to -1.
To undo these values, we have two options – either we can write expressions in the catch
handler itself to reset each of these variables, or we can associate ‘undo steps’ or
compensation steps with every scope itself, and then just invoke these undo steps from
the fault handler. This is the concept of compensation handler. The value it brings to
table is this – instead of making the fault handler ‘know’ about every scope and how to
undo it, we localize that information with that scope. That way, we get neat code, and
abstraction.
The first step towards this is therefore, to add compensation handlers to each of the
scopes – addition, subtraction and division. While we do add a compensation handler to
division scope, we know it will never be executed as the division scope is never going to
complete successfully. We shall verify this through the results. Compensation handlers
become active only when the scope they are attached with completes successfully.
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
We also added the compensation handler to the division scope. The division scope looks
like this now
Add a catchAll branch to Operations scope. In that catchAll, drag a compensate activity
and name it Compensate_all
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
After the Operations scope, add a assign activity and name it Prepare_Result
The complete BPEL looks like this: (you may enlarge to 150 – 200% to see clearly)
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Sample Practice Document for Training Evaluation http://soatraining.hpage.com