Variable Initialization
Introduction
One of the most common problems that newcomers to BPEL have is the concept of variable initialization.
Variables in a BPEL process come in three forms: variables defined using a simple schema type, variables defined using a schema element or variables defined using a WSDL message type definition. Any variable based on a schema element or WSDL message that contains more than just a root element is considered a complex variable.
All complex variables in a bpel process need to be initalized prior to use. This means that before you can copy a value to an element in a complex variable the variable structure must be present. Two types of standard BPEL Faults can be generated when you attempt to use a variable that is not properly initialized. This sample will describe these faults as well as provide two alternative approaches for resolving this issue.
Objectives
- Describe the types of standard BPEL Faults that can be raised when variables are not properly initialized
- Demonstrate these faults by simulating the attached bpel process
- Describe alternative ways to resolve these problems
Getting Set Up
The files provided with this article are contained in VariableInitialization.zip. You may extract this archive into a directory of your choosing. The archive contains numerous directories, and includes an ActiveBPEL® Designer project and various support, test and deployment files.
This sample is designed to use ActiveBPEL Designer to simulate and modify your process. Start ActiveBPEL Designer and select File / Import. In the Import dialog, select General / Existing Projects into Workspace and navigate to and import the following project directory
[path to extracted archive]/VariableInitialization
This opens a project called VariableInitialization,
which appears in the
Navigator View.
With the project successfully imported, you can now add the Orchestration Nature to the project. Right-click on the project name in the Project Explorer and select Add Orchestration Nature.
Open the Interfaces view if it is not visible (Window / Show View / Interfaces from the main menu) and note that the CreditPLT and RetailerPLT partnerLinkType definitions have been added. These definitions are contained in the CreditManager.wsdl and Retailer.wsdl files, and are imported automatically when the Orchestration Nature is added to the project. These can now be used to define interaction activities, sample data, etc.
Standard BPEL Initialization Faults
The two standard BPEL faults pertaining to improperly initialized variables are the uninitializedVariable and the selectionFailure faults. Each is described with some common situations were they can occur.
UninitializedVariable
This fault is generated when you attempt to use a variable that has not been initialized.
For instance, when the input variable for an <invoke> activity has not been initialized when it executes.
You can resolve this by creating an <assign> activity with the necessary copy
operations that populate the <invoke> activity's input variable.
SelectionFailure
This fault is generated when a selection operation, performed either in a function or in an assignment, encounters an error. For instance, in an <assign> activity you are attempting to copy something to a location path for a non-existent node in a complex variable. Since the node needs to exist you have two options, described below: use the process-level createXPath attribute or use an <assign> activity to create the target variable's structure using a literal copy operation.
Create XPath
The createXPath attribute allows an executing process to automatically create a location path for a non-existent node in a complex variable. To enable this function at any point in a process do the following:
- In the Properties view for the process, select the All tab.
- In the Create XPath row, select Yes from the drop-down list.
Literal Copy
Use a literal copy assignment to generate the literal XML contents and copy this value to a complex variable. This is described in the ActiveBPEL Designer User's Guide in the section labeled Copy Operation Literal Contents Examples.
Simulating the VariableInitialization Process
Simulating the uninitializedVariable fault
- Start by opening the VariableInitialization.bpel process.
- Open the SubmitOrderRequest process variable and Load Data from the SubmitOrderRequest.xml file found in the support\sampleData folder.
- Open the CheckCreditResponse process variable and add the CheckCreditResponse.xml file from the sampleData folder.
- Start simulation. Click Resume to allow the process to execute.
The process faults because the input variable for the PerformCreditCheck <invoke> activity was never initialized. You need to create an <assign> activity with some copy operations that will copy the data from the SubmitOrderRequest process variable to the CheckCreditRequest process variable.
The VariableInitialization2.bpel process has this <assign> activity already created. This will resolve the uninitializedVariable fault but if you simulate you will encounter the selectionFailure fault because you are attempting to copy data to a location in the the CheckCreditRequest variable that does not exist. The two ways to resolve this are to enable the ActiveBPEL extension Create XPath function or add a literal copy operation to an <assign> activity before copying any data to the CheckCreditRequest process variable. Both these techniques will be described in the next section.
Simulating the selectionFailure fault.
- Start by opening the VariableInitialization2.bpel process
- Open the SubmitOrderRequest process variable and add the SubmitOrderRequest.xml file found in the support\sampleData folder.
- Open the CheckCreditResponse process variable and add the CheckCreditResponse.xml file from the sampleData folder.
- Enable the Create XPath option as described above.
- Save the process.
- Start simulation. Click Resume to allow the process to execute.
The process will complete successfully since the paths for the target of the copy activity are created by the engine because we've enabled the Create XPath option. The second technique is to disable the Create XPath option and create a literal copy operation for each complex variable so that the path exists before you try to copy something to it. This technique is described here:
- Disable the Create XPath option.
- Now you need to create a literal copy operation in an <assign> activity somewhere in the process before you try to copy data to any complex variable. These complex variables include the CheckCreditRequest and the SubmitOrderResponse process variable. We will do this starting in the next step.
- Add a copy operation to the PrepareCreditCheckMessage <assign> activity.
- Complete the section labeled To by selecting the Variable option from the drop-down list for the Type field, CheckCreditRequest from the drop-down list for the Variable field and Document from the drop-down list for the Part field.
- Complete the section labeled From by selecting the Literal option from the drop-down list for the Type field. Press the Dialog Button under Literal Contents to open the XML Wizard and generate a sample.
- Add another copy operation to the PrepareCreditCheckMessage <assign> activity.
- Complete the section labeled To by selecting the Variable option from the drop-down list for the Type field, SubmitOrderResponse from the drop-down list for the Variable field and Document from the drop-down list for the Part field.
- Complete the section labeled From by selecting the Literal option from the drop-down list for the Type field. Press the Dialog Button under Literal Contents to open the XML Wizard and generate a sample.
- Move both Literal copy operations to the top of the list of copy operations using the button labeled Up.
- Save the process and start the simulation again.
- The completed process using the Literal copy technique is saved in the support\bpel folder with a filename of VariableInitialization3.bpel.
Copyright © 2004–2008 Active Endpoints, Inc.
