Advanced Activities in BPEL

Define, model, implement, and monitor real-world BPEL 2.0 business processes with SOA-powered BPM for IBM WebSphere 7 with this book and eBook

WS-BPEL 2.0 for SOA Composite Applications with IBM WebSphere 7

WS-BPEL 2.0 for SOA Composite Applications with IBM WebSphere 7

Define, model, implement, and monitor real-world BPEL 2.0 business processes with SOA-powered BPM

  • Develop BPEL and SOA composite solutions with IBM's WebSphere SOA platform
  • Automate business processes with WS-BPEL 2.0 and develop SOA composite applications efficiently
  • Detailed explanation of advanced topics, such as security, transactions, human workflow, dynamic processes, fault handling, and more—enabling you to work smarter

 

Appendix

        Read more about this book      

(For more resources on IBM, see here.)

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:

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

The <while> and <repeatUntil> loops are very similar to other programming languages. The <forEach> 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 <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

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:

<variables>
<variable name="NoOfPassengers" type="xs:int"/>
<variable name="Counter" type="xs:int"/>
</variables>

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:

<assign>
<copy>
<from>number(5)</from>
<to variable="NoOfPassengers"/>
</copy>
<copy>
<from>number(0)</from>
<to variable="Counter"/>
</copy>
</assign>

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

<while>
<condition>$Counter &lt; $NoOfPassengers</condition>
<sequence>
<!-- Construct the FlightDetails variable with passenger data -->
...
<!-- Invoke the web service -->
<invoke partnerLink="AmericanAirlines"
portType="aln:FlightAvailabilityPT"
operation="FlightAvailability"
inputVariable="FlightDetails" />
<receive partnerLink="AmericanAirlines"
portType="trv:FlightCallbackPT"
operation="FlightTicketCallback"
variable="FlightResponseAA" />
...
<!-- Process the results ... -->
...
<!-- Increment the counter -->
<assign>
<copy>
<from>$Counter + 1</from>
<to variable="Counter"/>
</copy>
</assign>
</sequence>
</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>

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

<repeatUntil>
<sequence>
<!-- Construct the FlightDetails variable with passenger data -->
...
<!-- Invoke the web service -->
<invoke partnerLink="AmericanAirlines"
portType="aln:FlightAvailabilityPT"
operation="FlightAvailability"
inputVariable="FlightDetails" />
<receive partnerLink="AmericanAirlines"
portType="trv:FlightCallbackPT"
operation="FlightTicketCallback"
variable="FlightResponseAA" />
...
<!-- Process the results ... -->
...
<!-- Increment the counter -->
<assign>
<copy>
<from>$Counter + 1</from>
<to variable="Counter"/>
</copy>
</assign>
</sequence>
<condition>$Counter &gt;= $NoOfPassengers</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 in parallel or serially. The serial <forEach> is very similar to the for loops from various programming languages, such as Java. The parallel <forEach> executes the loop branches in parallel (similar to <flow>), which 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), the startCounterValue, and the finalCounterValue. The <forEach> loop will execute (finalCounterValue – startCounterValue + 1) times.

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

The syntax of <forEach> is shown in the following:

<forEach counterName="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 a <forEach> loop will complete when all branches (<scope>) have completed.

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

<forEach counterName="Counter" parallel="no">
<startCounterValue>number(1)</startCounterValue>
<finalCounterValue>$NoOfPassengers</finalCounterValue>
<scope>
<sequence>
<!-- Construct the FlightDetails variable with passenger data
-->
...
<!-- Invoke the web service -->
<invoke partnerLink="AmericanAirlines"
portType="aln:FlightAvailabilityPT"
operation="FlightAvailability"
inputVariable="FlightDetails" />
<receive partnerLink="AmericanAirlines"
portType="trv:FlightCallbackPT"
operation="FlightTicketCallback"
variable="FlightResponseAA" />
...
<!-- Process the results ... -->
...
</sequence>
</scope>
</forEach>

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

<forEach counterName="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>

        Read more about this book      

(For more resources on IBM, see here.)

Delays

Sometimes a business process may need to specify a certain delay. In BPEL, we can specify delays either for a specified period of time or until a certain deadline is reached, by using the <wait> activity. Typically, we could specify delays to invoke an operation at a specific time, or wait for some time and then invoke an operation; for example, we could choose to wait before we pool the results of a previously initiated operation or wait between iterations of a loop.

The i><wait> activity can be:

  • for: We can specify duration, that is, we specify a period of time.

    <wait>
    <for> duration-expression </for>
    </wait>

  • until: We can specify a deadline; we specify a certain date and time.

    <wait>
    <until> deadline-expression </until>
    </wait>

Deadline and duration expressions

To specify deadline and duration expressions, BPEL uses lexical representations of corresponding XML Schema data types. For deadlines, these data types are either dateTime or date. For duration, we use the duration data type. The lexical representation of expressions should conform to the XPath 1.0 (or the selected query language) expressions. The evaluation of such expressions should result in values that are of corresponding XML Schema types: dateTime and date for deadline and duration for duration expressions.

All three data types use lexical representation inspired by the ISO 8601 standard, which can be obtained from the ISO web page http://www.iso.ch. ISO 8601 lexical format uses characters within the date and time information. Characters are appended to the numbers and have the following meaning:

  • C represents centuries.
  • Y represents years.
  • M represents months.
  • D represents days.
  • h represents hours.
  • m represents minutes.
  • s represents seconds. Seconds can be represented in the format ss.sss to increase precision.
  • Z is used to designate Coordinated Universal Time (UTC). It should immediately follow the time of day element.

For the dateTime expressions there is another designator:

T is used as time designator to indicate the start of the representation of the time.

Examples of deadline expressions are shown in the following code excerpts:

<wait>
<until>'2004-03-18T21:00:00+01:00'</until>
</wait>
<wait>
<until>'18:05:30Z'</until>
</wait>

For duration expressions, the following characters can also be used:

  • P is used as the time duration designator. Duration expressions always start with P.
  • Y follows the number of years.
  • M follows the number of months or minutes.
  • D follows the number of days.
  • H follows the number of hours.
  • S follows the number of seconds.

To specify a duration of 4 hours and 10 minutes, we use the following expression:

<wait>
<for>'PT4H10M'</for>
</wait>

To specify the duration of 1 month, 3 days, 4 hours, and 10 minutes, we need to use the following expression:

<wait>
<for>'P1M3DT4H10M'</for>
</wait>

The following expression specifies the duration of 1 year, 11 months, 14 days, 4 hours, 10 minutes, and 30 seconds:

<wait>
<for>'P1Y11M14DT4H10M30S'</for>
</wait>

Empty activities

When developing BPEL processes, you may come across instances where you need to specify an activity as per rules, but you do not really want to perform the activity. For example, in <if> activities, we need to specify an activity for each branch. However, if we do not want to perform any activity for a particular case, we can specify an <empty> activity. Not specifying any activity in this case would result in an error, because the BPEL process would not correspond to the BPEL schema. Empty activities are also useful in fault handling, when we need to suppress a fault.

The syntax for the <empty> element is rather straightforward:

<empty/>

Ending a process

BPEL provides the <exit> activity to immediately end a business process, before it has finished. We can use it to immediately terminate processes that are in execution. Often we use <exit> in conditional branches, where we need to exit a process when certain conditions are not met.

The <exit> activity ends the current business process instance and no fault and compensation handling is performed.

The syntax is very simple and is as follows:

<exit/>

Summary

This article familiarized us with the advanced concepts of BPEL, such as loops, process termination, delays, and deadline and duration expressions.


Further resources on this subject:


Books to Consider

Business Process Execution Language for Web Services 2nd Edition
$ 21.00
WS-BPEL 2.0 Beginner's Guide
$ 32.99
BPEL pour les services web : Deuxième Edition [French]
$ 55.99
comments powered by Disqus