6 min read

Loops

When defining business processes, we will sometimes want to perform a certain activity or a set of activities in a loop; for example, perform a calculation or invoke a partner web service operation several times, and so on.

BPEL supports three types of loops:

  • loops
  • loops
  • loops

The and loops are very similar to other programming languages. The loop, on the other hand, provides the ability to start the loop instances in parallel.

Loops are 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 of the same element, we can use XPath expressions.

Let us now look at the loop.

While

The 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 activity is shown in the following code excerpt:


boolean-expression
, , or other structured activity –>


Let us consider a scenario where we need to check flight availability for more than one person. Let us also assume that we need to invoke a web service operation for each person. In addition to the variables already present in the example, we would need two more NoOfPassengers to hold the number of passengers and Counter to use in the loop. The code excerpt with variable declarations is as follows:





We also need to assign values to the variables. The NoOfPassengers can be obtained from the Employee Travel web service. In the following code, we initialize both variables with static values:



number(5)



number(0)



The loop to perform the web service invocation is shown in the following code excerpt. Please remember that this excerpt is not complete:


$Counter




portType=”aln:FlightAvailabilityPT”
operation=”FlightAvailability”
inputVariable=”FlightDetails” />
portType=”trv:FlightCallbackPT”
operation=”FlightTicketCallback”
variable=”FlightResponseAA” />






$Counter + 1





Repeat Until

The 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 loop. The syntax of the activity is shown in the following code excerpt:


, , or other structured activity –>
boolean-expression


A similar example of a loop as mentioned in the previous section, but using , is as follows:






portType=”aln:FlightAvailabilityPT”
operation=”FlightAvailability”
inputVariable=”FlightDetails” />
portType=”trv:FlightCallbackPT”
operation=”FlightTicketCallback”
variable=”FlightResponseAA” />






$Counter + 1




$Counter >= $NoOfPassengers


For Each

The loop is a for type loop, with an important distinction. In BPEL the loop can execute the loop branches in parallel or serially. The serial is very similar to the for loops from various programming languages, such as Java. The parallel executes the loop branches in parallel (similar to ), which opens new possibilities in relatively simple parallel execution (for example, invocation of services).

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

The requires that we put all activities, which should be executed within the branch, into a . The allows us to perform group-related activities.

The syntax of is shown in the following:


unsigned-integer-expression
unsigned-integer-expression

nested within a scope. –>


Such a loop will complete when all branches () have completed.

A similar example of a loop as in the previous section is as follows:


number(1)
$NoOfPassengers


–>


portType=”aln:FlightAvailabilityPT”
operation=”FlightAvailability”
inputVariable=”FlightDetails” />
portType=”trv:FlightCallbackPT”
operation=”FlightTicketCallback”
variable=”FlightResponseAA” />






Sometimes it would be useful if the loop would not have to wait for all branches to complete. Rather, it would wait until at least some branches complete. In we can specify that the loop will complete after at least N branches have completed. We do this using the . We specify the number N of . The 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 as shown in the following:


unsigned-integer-expression
unsigned-integer-expression


unsigned-integer-expression



nested within a scope. –>



Subscribe to the weekly Packt Hub newsletter

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here