As an AVIO consultant, I work with many different clients and with many different technologies. During my current engagement, it dawned on me that something as simple as initializing a variable can be very different across the many technologies and programming languages that my coworkers and I encounter. In this particular case, I wanted to perform a simple POC (Proof of Concept) in Oracle BPEL 11g using a BPEL 2.0 service. In doing so, I came across the need to initialize a BPEL variable with an XML literal.
Initializing a variable in this way provides the following benefits:
- Ability to avoid SelectionFailure faults by initializing all or most important elements of the variable
- Ability to quickly initialize a variable, once all sticking points are understood
- Reduces the overall time taken to build and test a POC
Now as my gift to those that are Oracle BPEL 11g wise, n00bish, or in-between, I present as small example with some quick steps that walk through initializing a BPEL variable with an XML literal. The example is performed with Oracle SOA Suite 22.214.171.124.0 (PS3) using a BPEL 2.0 service.
- First we must view and understand the variable that needs to be initialized, in this case outputVariable. The variable, outputVariable, will be returned upon the invocation of this synchronous BPEL service. In order to properly populate outputVariable correctly the variable will need to be initialized first. After it has been initialized various operations like appending array elements to outputVariable elements and such can be performed without issue. Here we have the Variables editor open for the BPEL service and can see a list of variables.
IMPORTANT - We now must take note that outputVariable has the namespace prefix "client", and that the type is "CreateBpmOrderBpelResponseMessage". We need to know the namespace prefix of a BPEL variable in order to properly initialize the variable with an XML Literal.
- Now we see an open Assign activity editor for an Assign activity that is in the BPEL service. In the Assign activity editor the Literal assignment icon is being dragged and dropped onto the payload element of outputVariable. As we can see in the image below, payload contains the element type "client:readyOrdersResponsePayload". We will need to view the WSDL and/or XSD for this BPEL service to see how to put together the XML literal in order to populate payload.
- The WSDL for our BPEL service reveals the following:
In green - The output message type for the response of the BPEL service which is the message type "client:CreateBpmOrderBpelResponseMessage".
In yellow - The message type "CreateBpmOrderBpelResponseMessage" has a part named "payload" and is of type "client:readyOrdersResponsePayload"
In red - The XSD location and file (xsd/CreateBpmOrderBpel.xsd) that defines the "payload" type of "readyOrdersResponsePayload"
In blue - The definition of the namespace prefix "client", which is xmlns:client="http://xmlns.oracle.com/OPA_BPMServices/OPA_BPMServices/CreateBpmOrderB…"
- After looking at the XSD file, highlighted in red (xsd/CreateBpmOrderBpel.xsd), I have viewed the XSD definition for the "readyOrdersResponsePayload" complex type and with the definition I can now create an empty XML literal of "readyOrdersResponsePayload" type which is the following:
- We cannot simply paste an anonymous XML literal into the editor and expect it to populate payload properly. As we saw earlier in step 2, payload is expecting the client prefix to be used. In order to correctly create a valid XML Literal the correct namespacing and prefixing must be included. The correct XML literal is look like the following:
Correct XML Literal
Note that the namespace prefix "client" definition is supplied in the root element of the XML literal, along with a default definition (xmlns). Earlier in the BPEL service WSDL we noted that the "client" prefix information was highlighted in blue.
Note that the "client" prefix is used for each element of the XML literal.
The XML literal is valid and will now populate all elements of payload and in turn populate outputVariable.
- Now that outputVariable is initialized numerous XPath functions and activities can be performed using the variable without issues being caused by operating on an uninitialized variable.
There are other ways to go about initializing variables in BPEL and, depending on the situation, some ways will be better than others. We now have an example and the steps to initialize a BPEL variable with an XML Literal. Just remember, it is our job as professionals to apply this approach when the correct situation calls for it. I hope this information is found to be helpful, and best of luck from all of us here at Avio Consulting :)