compensate demo
DESCRIPTION
The sample brings out how to use compensate handler to provide 'undo' in a BPEL process. We shall pass two integers num1 and num2. The result would tell values of num1 + num2, num1 – num2 and num1/num2. Initially the variables for the results – additionResult, subtractionResult and divisionResult are all set to -1. The first operation is addition, followed by subtraction and division. If during division, we find that result is Infinity (which would happen when num2 is zero), we want to undo the assignment of valid values to additionResult and subtractionResult (‘undo’ in the context of this example means setting these back to -1)TRANSCRIPT
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
With every training session, the presentation used and a practice With every training session, the presentation used and a practice With every training session, the presentation used and a practice With every training session, the presentation used and a practice
document that contains stepdocument that contains stepdocument that contains stepdocument that contains step----by step instructions for developing by step instructions for developing by step instructions for developing by step instructions for developing
the sample is provided.the sample is provided.the sample is provided.the sample is provided.
This document is a demo of those practice documents. This This document is a demo of those practice documents. This This document is a demo of those practice documents. This This document is a demo of those practice documents. This
particular one is when we discuss and departicular one is when we discuss and departicular one is when we discuss and departicular one is when we discuss and develop a compensation velop a compensation velop a compensation velop a compensation
handler in a BPEL process. It also brings out how to use throw handler in a BPEL process. It also brings out how to use throw handler in a BPEL process. It also brings out how to use throw handler in a BPEL process. It also brings out how to use throw
and catch activityand catch activityand catch activityand catch activity....
Every document starts with an ‘Aim’ and then goes on to develop Every document starts with an ‘Aim’ and then goes on to develop Every document starts with an ‘Aim’ and then goes on to develop Every document starts with an ‘Aim’ and then goes on to develop
a project around that. It ends after showing the project in action.a project around that. It ends after showing the project in action.a project around that. It ends after showing the project in action.a project around that. It ends after showing the project in action.
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.
Initially the variables for the results – additionResult, subtractionResult and
divisionResult are all set to -1.
The first operation is addition, followed by subtraction and division. If during division,
we find that result is Infinity (which would happen when num2 is zero), we want to undo
the assignment of valid values to additionResult and subtractionResult (‘undo’ in the
context of this example means setting these back to -1)
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
Start by creating a new project
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Modify the input schema to accept two integers
Add a scope named Operations.
Within that, add three scopes named addition, subtraction and division
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Create three variables – additionResult (integer), subtractionResult (integer) and
divisionResult (float)
Steps to create additionResult are shown
Follow similar steps for subtractionResult and divisionResult
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
BPEL looks like this as of now
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
In each scope add an assign activity.
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')
Similarly do for subtraction and division.
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')
The BPEL now looks like this
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
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
Add an assign activity in the compensation handler. Name it reset.
Add a copy operation in that that sets additionResult to -1
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Similarly add a reset assign activity to subtraction scope
Now, we need to check for Infinity in Division Scope.
For that, add a switch in the division scope. .
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Delete the otherwise branch
In case branch, add an expression, as shown bpws:getVariableData('divisionResult') = 'Infinity' or
bpws:getVariableData('divisionResult') = '-Infinity'
In the case branch, add a throw activity.
Name the throw activity as Throw_Err and give some value to the namespace and local
part text boxes.
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
Add the following copy operation to it
concat('Addition result = ',bpws:getVariableData('additionResult'),',
Subtraction result = ',bpws:getVariableData('subtractionResult'),',
Division result = ',bpws:getVariableData('divisionResult'))
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
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
Deploy and test
Give non zero input
Got expected output
Give input such that second value is 0.
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
Sample Practice Document for Training Evaluation http://soatraining.hpage.com
What we have achieved is
1. By using compensate activity without specifying any scope, BPEL PM produced
the default behavior -compensation handlers of all completed scopes – addition
and subtraction, were invoked. This can be verified from the values of
additionResult and subtractionResult which were reset to -1
2. Compensation handler for division scope is not called as this scope did not
complete. It is evident from the value of divisionResult, which is still Infinity,
and not -1