Skip Headers
Oracle® Fusion Middleware Developer's Guide for Oracle SOA Suite
11g Release 1 (11.1.1)

Part Number E10224-03
Go to Documentation Home
Go to Book List
Book List
Go to Table of Contents
Go to Index
Go to Feedback page
Contact Us

Go to previous page
Go to next page
View PDF

10 Using Conditional Branching in a BPEL Process

This chapter describes how to use conditional branching in a BPEL process service component. Conditional branching introduces decision points to control the flow of execution of a BPEL process service component.

This chapter includes the following sections:

For additional information on creating conditional branching in a SOA composite application, see the WebLogic Fusion Order Demo application.

10.1 Introduction to Conditional Branching

BPEL applies logic to make choices through conditional branching. You can use either of the following activities to design your code to select different actions based on conditional branching:

Many branches are set up, and each branch has a condition in the form of an XPath expression.

You can program a conditional branch to have a timeout. That is, if a response cannot be generated in a specified period, the BPEL flow can stop waiting and resume its activities. Chapter 14, "Using Events and Timeouts in BPEL Processes" explains this feature in detail.


You can also define conditional branching logic with business rules. See Oracle Fusion Middleware User's Guide for Oracle Business Rules and the WebLogic Fusion Order Demo application for details.

10.2 Creating a Switch Activity to Define Conditional Branching

Assume you designed a flow activity in the BPEL process service component that gathered loan offers from two companies at the same time, but did not compare either of the offers. Each offer was stored in its own global variable. To compare the two bids and make decisions based on that comparison, you can use a switch activity.

Figure 10-1 provides an overview of a BPEL conditional branching process that has been defined in a switch activity.

Figure 10-1 Conditional Branching

Description of Figure 10-1 follows
Description of "Figure 10-1 Conditional Branching"

10.2.1 How to Create a Switch Activity

To create a switch activity:

  1. From the Component Palette, drag a Switch activity into the designer.

  2. Click the + sign to expand the switch activity, as shown in Figure 10-2.

    The Switch activity has two switch case branches by default, each with a box for functional elements. If you want to add more branches, select the entire switch activity, right-click, and select Add Switch Case from the menu.

    Figure 10-2 Switch Activity

    Description of Figure 10-2 follows
    Description of "Figure 10-2 Switch Activity"

  3. In the first branch, right-click and select Edit from the menu.

    The Switch Case dialog appears.

  4. In the Expression field, enter an XPath boolean expression by pressing Ctrl+Space to start the XPath Building Assistant. Example 10-1 provides details.

    Example 10-1 XPath Expression

    bpws:getVariableDate('loanOffer1','payload','/loanOffer/APR') >
  5. Enter this expression on one line. To use the XPath Expression Builder, click the XPath Expression Builder icon above the Expression field.

    In this example, two loan offers from completing loan companies are stored in the global variables loanOffer1 and loanOffer2. Each loan offer variable contains the loan offer's APR. The BPEL flow must choose the loan with the lower APR. One of the following switch activities takes place:

    • If loanOffer1 has the higher APR, then the first branch selects loanOffer2 by assigning the loanOffer2 payload to the selectedLoanOffer payload.

    • If loanOffer1 does not have the lower APR than loanOffer2, the otherwise case assigns the loanOffer1 payload to the selectedLoanOffer payload.

10.2.2 What Happens When You Create a Switch Activity

A switch activity, like a flow activity, has multiple branches. In Example 10-2, there are only two branches shown in the .bpel file after design completion. The first branch, which selects a loan offer from a company named United Loan, is executed if a case condition containing an XPath boolean expression is met. Otherwise, the second branch, which selects the offer from a company named Star Loan, is executed. By default, the switch activity provides two switch cases, but you can add more if you want.

Example 10-2 Switch Activity

<switch name="switch-1">
     <case condition="bpws:getVariableData('loanOffer1','payload',
     '/autoloan:loanOffer/autoloan:APR') <
          <assign name="selectUnitedLoan">
               <from variable="loanOffer1" part="payload">
               <to variable="selectedLoanOffer" part="payload"/>
         <assign name="selectStarLoan">
            <from variable="loanOffer2" part="payload">
            <to variable="selectedLoanOffer" part="payload"/>

10.3 Creating a While Activity to Define Conditional Branching

Another way to design your BPEL code to select between multiple actions is to use a while activity to create a while loop. The while loop repeats an activity until a specified success criteria is met. For example, if a critical web service is returning a service busy message in response to requests, you can use the while activity to keep polling the service until it becomes available. The condition for the while activity is that the latest message received from the service is busy, and the operation within the while activity is to check the service again. Once the web service returns a message other than service busy, the while activity terminates and the BPEL process service component continues, ideally with a valid response from the web service.

10.3.1 How To Create a While Activity

To create a while activity:

  1. From the Component Palette, drag a While activity into the designer.

  2. Click the + sign to expand the while activity.

    The while activity has icons to allow you to build condition expressions and to validate the while definition. It also provides an area for you to drag an activity to define the while loop. Figure 10-3 provides an example.

    Figure 10-3 While Activity

    Description of Figure 10-3 follows
    Description of "Figure 10-3 While Activity"

  3. Drag and define additional activities for using the while condition into the Drop Activity Here area of the While activity (for example, a Scope activity).

    The activities can be existing or new activities.

  4. Press Ctrl+Space to invoke the XPath Building Assistant or click the XPath Expression Builder icon to open the Expression Builder dialog.

  5. Enter an expression to perform repeatedly, as shown in Figure 10-4. This action is performed until the given boolean while condition is no longer true. In this example, this activity is set to loop while less than 5.

    Figure 10-4 While Activity with an Expression

    Description of Figure 10-4 follows
    Description of "Figure 10-4 While Activity with an Expression"

  6. Click OK when complete.

10.3.2 What Happens When You Create a While Activity

Example 10-3 provides an example of the .bpel file after design completion. The while activity includes a scope activity. The scope activity includes invoke, assign, and wait activities. Database exception handling tasks are performed by creating a local variable and placing the invoke activity inside the scope activity. The local variable is set to false (represented by 0). You attempt to call the external partner service in the while loop activity until the local variable is satisfied (set to 1). The while activity is set to loop a maximum of five times. In the case of an exception, you reset the flag to false (0).

Example 10-3 While Activity

<while name="While_1" condition="bpws:getVariableData('dbStatus') > 5">
      <scope name="Scope_1">
            <sequence name="Sequence_2">
              <assign name="assign_DB_retry">
                  <from expression="bpws:getVariableData('dbStatus') + 1"/>
                  <to variable="dbStatus"/>
              <wait name="Wait_30_sec" for="'PT31S'"/>
        <sequence name="Sequence_1">
          <invoke name="Write_DBWrite" partnerLink="WriteDBRecord"
                  portType="ns2:WriteDBRecord_ptt" operation="insert"
          <assign name="Assign_dbComplete">
              <from expression="'10'"/>
              <to variable="dbStatus"/>