O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looping

Problem

You need to repeat the execution of some activity until a condition fails, as in a loop.

Solution

Select from one of the available BPEL constructs for this purpose: <while>, <repeatUntil>, or <forEach>.

Discussion

These constructs are rather straightforward, so we’ll just take a quick tour.

while loop

As in Java, the <while> activity allows you to repeat a block of code (or, more specifically, the BPEL activity you’re enclosing) until a certain <condition> no longer evaluates to true.

Here is the basic structure:

<while>
  <condition>$lineItems < 10</condition>
  <scope>...</scope>
</while>

A slightly more ambitious example illustrates how you can increment a variable counter within a loop:

<while>
   <condition>$counter < $lineItems</condition>

   <sequence>
      <invoke partnerLink="CheckInventory" ... />
      <!–- Here we increment the condition's counter-->
      <assign>
         <copy>
            <from expression="$counter + 1" />
            <to variable="counter" />
         </copy>
      </assign>
   </sequence>
</while>

This all behaves just as you would expect. If the condition is not true the first time it is evaluated, the activity it contains will never be executed.

repeatUntil loop

The repeatUntil loop acts like a do/while loop in Java. That is, its primary distinguishing feature is that it always executes at least once, as the condition is evaluated after the enclosed activity has been run. Its structure looks like this:

<repeatUntil>
   <some activity />
   <condition>
       boolean expression
   </condition>
</repeatUntil>

Here is the previous example ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required