Friday, February 17, 2012

Parallel execution using FlowN and variable scoping

The BPEL FlowN activity can be used for parallel execution of activities. Often this activity is used to split up a message into parts and process those parts simultaneously. When using FlowN it is advisable to put a scope inside and use local variables. If you neglect to do that, the results can be different from what you might expect.

To demonstrate this I created a database table and a simple BPEL process. The BPEL process inserts the counter variable of the FlowN activity into a table. 

See the below screenshot for an overview of the process.

All variables in this process are global. I let JDeveloper generate a global variable for me when I created the invoke activity. The assign activity consists of the following;
  • Assign an empty XML fragment to the variable used for the invoke
  • Assign values to the fields of this variable. I used the counter in the FlowN (FlowN1_Variable_1) as one of the values.

I set the counter to perform the loop 3 times. The flow which was created is the following;

When I check the assign activities I see the following;

This looks like how I want it; the variable for the invoke is assigned respectively 1,2,3 in the corresponding FlowN branche.When I check the invoke activities however, I see in all 3 the invokes the following contents of the invoke variable;

When I check the database I see the following;

This is of course behavior to be expected since the assign activities are executed in parallel on a global variable, thus which value the global variable has during the invoke, is not solely determined by what happens in its own FlowN branche, but also by what happens in the other branches.

Fixing this is easy; create a new scope inside the FlowN activity and use local variables inside that scope. This makes the different FlowN branches independent of eachother.

See the last three results (id 4,5,6) below for the results after adding scopes and moving the invoke variable to this scope. The below screenshot also illustrates that the order of execution is not fixed when branches are executed in parallel.