Using the For Each action to process a collection

This recipe will show we can loop over a collection of information by using the For Each action. We will implement a proxy service accepting a Customer element through a one-way interface. The proxy service will use the For Each action in its message flow to loop over the single addresses inside the addresses collection.

Each address will then be sent to an Address Checking Service which would check the address for correctness. In our case, this service is a mock service implemented in soapUI. To better see the sequential nature of the processing, the Address Checking Service is written so that it takes four seconds to respond.

Using the For Each action to process a collection

Getting ready

You can import the OSB project containing the base setup for this recipe into Eclipse OEPE from chapter-9getting-readyusing-foreach-to-process-collection.

Start the soapUI mock service simulating the Address Checking Service by double-clicking on start-AddressCheckingService.cmd in the chapter-9getting-readymisc folder.

How to do it...

Let's create the proxy service with a one-way interface accepting a Customer XML Schema type. In Eclipse OEPE, perform the following steps:

  1. In the using-foreach-to-process-collections project, open the ForEach proxy service and navigate to the Message Flow tab.
  2. Insert a Pipeline Pair node into the StoreCustomer branch and name it ProcessAddressesPipelinePair.
  3. Insert a Stage into the Request Pipeline and name it ProcessAddressesStage.
  4. Insert a For Each action into the stage.
  5. On the Properties tab of the For Each action, enter address into the For Each Variable field.
  6. Click on the <XPath> link and enter ./cus1:StoreCustomer/Customer/cus:Addresses/cus:Address into the Expression field.
  7. Enter body into the In Variable field, index into the Index Variable field and count into the Count Variable fied.
    How to do it...
  8. By doing that, we have implemented the loop over the addresses collection using a For Each action. But we are not yet doing anything inside the loop. So let's add a Log action to show a log output for each item (address) of the collection and pass the address to the AddressChecking business service. In Eclipse OEPE, perform the following steps:
  9. Insert a Log action into the For Each action.
  10. On the Properties tab of the Log action click <Expression>.
  11. Enter concat('Address ', $index, ' of ', $count, ' = ', $address) into the Expression field and click OK.
  12. Select Warning from the Severity drop-down listbox.
  13. Insert a Service Callout action after the Log action.
  14. On the Properties tab of the Service Callout action click Browse and then select the AddressChecking business service from business folder.
  15. Select CheckAddress from the Invoking drop-down listbox.
  16. Select the Configure Payload Document option.
  17. Enter request into the Request Variable and response into the Response Variable field.
  18. Insert an Assign action into the Request Action of the Service Callout action.
  19. On the Properties tab of the Assign action click <Expression> and enter the following XML fragment into the Expression field:
    <add:CheckAddress 
      xmlns:add="http://www.osbcookbook.org/AddressCheckingService/">
      {$address}
    </add:CheckAddress>
  20. Click OK.
  21. Enter request into the Variable field.
    How to do it...
  22. Deploy the project to the OSB server.

    Now we can test the proxy service. In the Service Bus console, perform the following steps:

  23. In the Project Explorer navigate to proxy folder inside the using-foreach-to-process-collections project.
  24. Click on the Launch Test Console icon of the ForEach proxy service.
  25. Enter a message with two addresses into the Payload field as shown in the following screenshot and click Execute.
    How to do it...
  26. Two log statements should be shown in the Service Bus console log window, with a four seconds delay in between the first and the second.
    How to do it...
  27. The service will take around eight seconds to respond with the result window.

How it works...

The For Each action allows us to implement a loop to process each single item of a collection. The loop body can include all the different actions the Oracle Service Bus provides. However, it's not possible to include a node such as a Pipeline Pair or Route inside a loop. Therefore, it's not possible to execute a Routing action inside a loop. To call a service for each item of a collection, a Service Callout or Publish action has to be used in the loop body.

See also

  • Check the Using dynamic Split-Join to perform work in parallel recipe to see how the processing can also be done in parallel by using a Split-Join instead of the For Each action.
  • Check the Using the Service Callout action to invoke a service recipe to see the Service Callout action.
..................Content has been hidden....................

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