Conditions and loops are important constructs in every programming language. Conditions allow us to implement branches. Loops provide the possibility for the code to execute more than once. In this chapter, we will look at conditions and loops in BPEL. We will look at the if
/elseif
/else
activity, which is how conditions are expressed in BPEL. For loops, BPEL has more options. We will become familiar with while, repeat until, and for each loops. We will also explain which to use when. We will look into the detailed syntax of each loop and learn when to use parallel loops.
In this chapter, we will take a closer look at the if
/elseif
/else
activity and at the various loop activities provided by BPEL. As we move along the chapter, we will cover the following topics:
<if>
activity<while>
loop<repeatUntil>
loop<forEach>
loop<forEach>
executionSo, let's get started...
To make choices based on conditions in BPEL 2.0, we use the <if>
activity to define the conditional branches, which replaced the <switch>
activity in BPEL 1.1. The <if>
activity can have several <elseif>
branches and one <else>
branch. The following example shows the structure of the <if>
activity:
<if> <condition>boolean-expression</condition> <!--activities --> <elseif> <condition>boolean-expression</condition> <!--activities --> </elseif> <elseif> <condition>boolean-expression</condition> <!--activities --> </elseif> ... <else> <!--activities --> </else> </if>
The Boolean expressions for the <condition>
elements are expressed in the XPath. We can use any valid XPath expression that returns a Boolean value. To use variables in conditions, we can use the $
operator, the same way as in assignments, which we have covered in Chapter 3, Variables, Data Manipulation, and Expressions.