07-8 CompensateDemo

Embed Size (px)

Citation preview

  • 8/11/2019 07-8 CompensateDemo

    1/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    With every training session, the presentation used and apractice document that contains step-by step instructions fordeveloping the sample is provided.

    This document is a demo of those practice documents. Everydocument starts with an Aim and then goe s on to develop aproject around that. It ends after showing the project in action. This document comes along with the code that was developedso that the student can see the final code as well.

    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 anddivisionResult 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 toundo the assignment of valid values to additionResult and subtractionResult (undo inthe context of this example means setting these back to -1)

    Start by creating a new composite CompensateDemo

  • 8/11/2019 07-8 CompensateDemo

    2/15

  • 8/11/2019 07-8 CompensateDemo

    3/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Create three variables additionResult (integer), subtractionResult (integer) anddivisionResult (float)Steps to create additionResult are shown

  • 8/11/2019 07-8 CompensateDemo

    4/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Follow similar steps for subtractionResult and divisionResult

    Add a assign activity before the Operations scope and set each of the three variables to -1

    In each scope add an assign activity.

    In Addition, name the assign activity as Add . Create a copy operation and add the twoinputs and assign that value to additionResult

  • 8/11/2019 07-8 CompensateDemo

    5/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Similarly do for subtraction and division.

    bpws:getVariableData('inputVariable','payload','/client:process/client:num1')-bpws:getVariableData('inputVariable','payload','/client:process/client:num2')

    bpws:getVariableData('inputVariable','payload','/client:process/client:num1') divbpws:getVariableData('inputVariable','payload','/client:process/client:num2')

    The BPEL now looks like this

  • 8/11/2019 07-8 CompensateDemo

    6/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

  • 8/11/2019 07-8 CompensateDemo

    7/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Now our business requirement is, if the second number is zero, then divisionResultwould be Infinity or Infinity. We want to test for this condition in the division scope. Ifit is found true, we shall throw an exception. That exception shall be caught byOperations 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 thecatch handler itself to reset each of these variables, or we can associate undo steps orcompensation steps with every scope itself, and then just invoke these undo steps fromthe fault handler. This is the concept of compensation handler. The value it brings totable is this instead of making the fault handler know about every scope and how toundo it, we localize that information with that scope. That way, we get neat code andloose coupling.

    The first step towards this is therefore, to add compensation handlers to each of thescopes Scope_Addition, Scope_Subtraction and Scope_Division. While we do add acompensation handler to division scope, we know it will never be executed as theprocess would never error out after the division scope is finished. We shall verify thisthrough the results. Compensation handlers become active only when the scope theyare attached with completes successfully.

    Add an assign activity in the compensation handler. Name it reset.

  • 8/11/2019 07-8 CompensateDemo

    8/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Add a copy operation in that that sets additionResult to -1

    Similarly add a reset assign activity to subtraction scopeNow, we need to check for Infinity in Division Scope.For that, add a switch in the division scope.Delete the otherwise branch

    In case branch, add an expression, as shownbpws:getVariableData('divisionResult') = 'Infinity' or bpws:getVariableData('divisionResult') ='-Infinity'

  • 8/11/2019 07-8 CompensateDemo

    9/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    In the case branch, add a throw activity.Name the throw activity as ThrowErr_DivByZero and give some value to the namespaceand local part text boxes.

    Add a catch handler to the division scope which handles the DivByZero fault

  • 8/11/2019 07-8 CompensateDemo

    10/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    We reset the divisionResult to -1.0 and also throw another error.

    Where the Throw is defined as below

  • 8/11/2019 07-8 CompensateDemo

    11/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Add a catchAll branch to Operations scope. In that catchAll, drag a compensate activityand name it Compensate_all

    Compensate activity allows us to choose the scope if desired (which means call thecompensate handler of that particular scope), or not select any scope (which means call the compensate handler of ALL completed scopes). We choose the defaultbehaviour, by not selecting any scope

  • 8/11/2019 07-8 CompensateDemo

    12/15

    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'))

    The complete BPEL looks like this: (you may enlarge to 150% to see clearly)

  • 8/11/2019 07-8 CompensateDemo

    13/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

  • 8/11/2019 07-8 CompensateDemo

    14/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Deploy and testGive non zero input

    Got expected output

    Give input such that second value is 0.

  • 8/11/2019 07-8 CompensateDemo

    15/15

    Sample Practice Document for Training Evaluation http://soatraining.hpage.com

    Notes1. By using compensate activity without specifying any scope, BPEL PM produced

    the default behavior -compensation handlers of all completed scopes Scope_Addition and Scope_Subtraction, were invoked. This can be verified fromthe values of additionResult and subtractionResult which were reset to -1

    2. The compensate activity is to be added in a scope higher than the scopes whosecompensateHandlers we want to invoke. In the example, we added thecompensate activity in Scope_Operation, so that it could call the compensationhandlers in Scope_Addition and Scope_Subtraction.