Oracle® BPEL Process Manager Developer's Guide 10g (10.1.3.1.0) Part Number B28981-03 |
|
|
View PDF |
This chapter describes conditional branching. Conditional branching introduces decision points to control the flow of execution of a BPEL process.
This chapter contains the following topics:
The BPEL process you created in Chapter 6, "Parallel Flow" collected two loan offers, one from United Loan and another from Star Loan. This chapter describes how to design the BPEL process to select the loan with the lowest annual percentage rate (APR) automatically.
See Also:
The following sample:SOA_Oracle_Home
\bpel\samples\demos\LoanDemo\LoanFlow
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:
Switch activity: In this method, you set up two or more branches, with each branch in the form of an XPath expression. If the expression is true, then the branch is executed. If the expression is false, then the BPEL process moves to the next branch condition, until it either finds a valid branch condition, encounters an otherwise branch, or runs out of branches. If more than one branch condition is true, then BPEL executes the first true branch. "Using a Switch Activity to Define Conditional Branching" explains how to create switch activities.
While activity: You can use a while activity to create a while loop to select between two actions. "Using a While Activity to Define Conditional Branching" describes while activities.
A number of 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 of time, the BPEL flow can stop waiting and resume its activities. Chapter 10, "Events and Timeouts" explains this feature in detail.
In Chapter 6, the flow activity of the BPEL process gathered two loan offers 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 offers and make decisions based on that comparison, the BPEL flow requires a switch activity.
Figure 7-1 provides an overview of a BPEL conditional branching process that has been defined in a switch activity.
A switch activity, like a flow activity, has multiple branches. In this example, there are only two branches. The first branch, which selects a loan offer from United Loan, is executed if a case condition containing an XPath Boolean expression is met. Otherwise, the second branch, which selects the Star Loan loan offer, is executed. By default, the switch activity provides two switch cases, but you can add more if you want.
<switch name="switch-1"> <case condition="bpws:getVariableData('loanOffer1','payload', '/autoloan:loanOffer/autoloan:APR') <; bpws:getVariableData('loanOffer2','payload','/autoloan:loanOffer/autoloan:APR ')"> <assign name="selectUnitedLoan"> <copy> <from variable="loanOffer1" part="payload"> </from> <to variable="selectedLoanOffer" part="payload"/> </copy> </assign> </case> <otherwise> <assign name="selectStarLoan"> <copy> <from variable="loanOffer2" part="payload"> </from> <to variable="selectedLoanOffer" part="payload"/> </copy> </assign> </otherwise> </switch>
To add a switch activity to your BPEL flow in Oracle JDeveloper:
Drag a switch activity from the Process Activities list of the Component Palette into your BPEL flow.
Click the + sign to expand the switch activity.
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.
Right-click the first branch and select Edit from the menu.
The Switch Case window appears.
Enter an XPath Boolean expression in the Expression field by pressing the Ctrl key and then the space bar to start the XPath Building Assistant. For example:
bpws:getVariableDate('loanOffer1','payload','/loanOffer/APR') > bpws:getVariableData('loanOffer2','payload','/loanOffer/APR')
Enter this expression on one line. To use the XPath Expression Builder, click the XPath Expression Builder icon above the Expression field.
The two loan offers that the LoanFlow tutorial uses 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:
See Also:
The following documentation for examples of creating switch activities in Oracle JDeveloper:SOA_Oracle_Home
\bpel\samples\references\Switch
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 continues, ideally with a valid response from the Web service.
To create a while activity in Oracle JDeveloper:
Drag and drop a while activity from the Process Activities list of the Component Palette into your BPEL flow.
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 drop an activity to define the while loop.
Drag the activity that you want to use to define the while condition onto the Drop Activity Here area of the while activity.
The activity can be an existing activity or a new activity, such as an invoke activity to launch a task.
See Also:
The following documentation for examples of defining a while activity in Oracle JDeveloper:SOA_Oracle_Home
\bpel\samples\references\While
This chapter discusses the concepts and procedures for creating a switch activity conditional flow that selects different behavior based on comparing two pieces of information. The BPEL process in this example considers two loan offers, and selects the offer with the lower APR. This chapter also discusses the while looping conditional activity.