Loops

When defining BPEL processes, some activities often need to execute more than once. For example, let us suppose that we had a business process that needed to warehouse an array of books. Other examples include performing calculations or invoking partner web service operations several times, and so on.

In BPEL, we can choose between three types of loops:

  • The <while> loops
  • The <repeatUntil> loops
  • The <forEach> loops

The <while> and <repeatUntil> loops are very similar to other programming languages. The <forEach> loop can also be found in some programming languages. In BPEL, <forEach> also provides the ability to start the loop instances in parallel, which can be very useful.

Let us now look at the <while> loop.

While

The <while> loop repeats the enclosed activities until the Boolean condition no longer holds true. The Boolean condition is expressed through the condition element, using the selected expression language (the default is XPath 1.0). The syntax of the <while> activity is shown in the following code excerpt:

<while>
  <condition>boolean-expression</condition>
  
  <!-- Perform an activity or a set of activities enclosed by 
  <sequence>, <flow>, or other structured activity -->
  
</while>

Repeat until

The <repeatUntil> loop repeats the enclosed activities until the Boolean condition becomes true. The Boolean condition is expressed through the condition element, the same way as in the <while> loop. The syntax of the <repeatUntil> activity is shown in the following code excerpt:

<repeatUntil>

  <!-- Perform an activity or a set of activities enclosed by 
  <sequence>, <flow>, or other structured activity -->

  <condition>boolean-expression</condition>
</repeatUntil>

For each

The <forEach> loop is a for type loop, with an important distinction. In BPEL, the <forEach> loop can execute the loop branches serial or in parallel. The serial <forEach> is very similar to the for loops from various programming languages, such as Java. The parallel <forEach> loop executes the loop branches in parallel, similar to <flow>, but unlike a flow activity, the number of parallel branches is not known at design time. Obviously, in parallel <forEach>, each branch executes the same set of activities, while in the <flow>, each branch can execute different activities. This opens new possibilities in relatively simple parallel execution (for example, invocation of services).

The <forEach> loop requires us to specify the BPEL variable for the counter (counterName), startCounterValue and finalCounterValue . The <forEach> loop will execute (finalCounterValuestartCounterValue + 1) times.

The <forEach> loop requires that we put all activities, which should be executed within the branch, into <scope>. The <scope> loop allows us to group-related activities. We will discuss <scope> in detail later in this book.

The syntax of <forEach> is shown as follows:

<forEachcounterName="BPELVariableName" parallel="yes|no">

  <startCounterValue>unsigned-integer-expression</startCounterValue>
  <finalCounterValue>unsigned-integer-expression</finalCounterValue>

  <scope>
    <!-- The activities that are performed within forEach have 
    to be nested within a scope. -->
  </scope>

</forEach>

Such <forEach> loop will complete when all branches (<scope>) have completed.

Parallel for each

Sometimes, it would be useful if the <forEach> loop would not have to wait for all branches to complete. Rather it would wait for some branches to complete. In <forEach>, we can specify that the loop will complete after at least N branches have completed. We do this using <completionCondition>. We specify the number N of <branches>. The <forEach> loop will complete after at least N branches have completed. We can specify if we would like to count only successful branches or all branches. We do this using the successfulBranchesOnly attribute. If set to yes, only successful branches will count. If set to no (default), successful and failed branches will count. The syntax is shown as follows:

<forEachcounterName="BPELVariableName" parallel="yes|no">

  <startCounterValue>unsigned-integer-expression</startCounterValue>
  <finalCounterValue>unsigned-integer-expression</finalCounterValue>

  <completionCondition><!-- Optional -->
    <branches successfulBranchesOnly="yes|no">
      unsigned-integer-expression
    </branches>
  </completionCondition>
  <scope>
    <!-- The activities that are performed within forEach have 
    to be nested within a scope. -->
  </scope>

</forEach>

Arrays

Loops are also very helpful when dealing with arrays. In BPEL, arrays can be simulated using XML complex types where one or more elements can occur more than once (using the maxOccurs attribute in the XML schema definition). To iterate through multiple occurrences, we will require loops. We can also use XPath expressions to address multiple occurrences of the same element.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset