Time for action – the <forEach> loop

Let us now implement the same scenario using the <forEach> loop:

  1. We don't want to overwrite the <while> loop example; therefore, we will add another BPEL process component to the composite diagram. We will call it BookOrderForEachBPEL:
    Time for action – the <forEach> loop
  2. We will also wire it with BookWarehousingBPEL:
    Time for action – the <forEach> loop
  3. We will use exactly the same schema as in the previous example. To implement the BPEL process, we will first add the <forEach> activity to the process flow by dragging-and-dropping For Each from the Components palette (you will find it under Structured Activities):
    Time for action – the <forEach> loop
  4. We will double-click the ForEach1 activity and set Name as ManageOrders and name of the counter variable (Counter name) as OrderCounter:
    Time for action – the <forEach> loop
  5. In the Counter Values tab, we will set the start and the end value for the counter. The start value is 1 and the end value is the count($inputVariable.payload/ns1:BookData) expression. With this expression, we will count the number of books in the array. This is exactly the number of times we wish the <forEach> loop to repeat:
    Time for action – the <forEach> loop
  6. You can now click the OK button. As in the previous example, within the <forEach> loop, we will extract the book order item from the array, and prepare the request for the BookWarehousingBPEL process and invoke it. We do not need to manually increase the counter however.
  7. Let us first add the <invoke> activity. We will name the activity InvokeBookWarehousing. We will also create two global variables on the fly, BookWarehousingRequest and BookWarehousingResponse:
    Time for action – the <forEach> loop
  8. Next, we will add the <assign> activity to prepare the request. We will add the <assign> activity before the <invoke> activity and set the following copy expression:
    Time for action – the <forEach> loop

    As you can see, we will copy the $inputVariable.payload/ns1:BookData[$OrderCounter] order item to the $BookWarehousingRequest.payload/ns1:BookData element.

What just happened?

We have successfully developed the BookOrderForEach BPEL process, which iterates through book data items using a <forEach> loop.

The process flow should look like this:

What just happened?

We can see that the <forEach> loop is a little simpler, as we do not have to take care of the counter variable manually.

The source code should read as follows:

What just happened?

Have a go hero – test the process

It's your turn now. You should deploy and test the BookOrderForEachBPEL process.

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

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