Variables in BPEL
Variables are an essential part of each and every programming language. In BPEL, variables are used to store the data that the process wishes to retain during the execution of each process instance. For example, when a BPEL process invokes an operation on an external service (partner link), this operation may require input parameters. An operation might also return a result as an output parameter. We will use variables to store the result and to prepare the input parameters for the operation. We can also use variables to store other data, required for the process instance execution, even if it is never sent to a service.
Often, a service will require a different schema (structure) of the data to the one used in our BPEL process. Therefore, we will need to map the data to a schema expected by the service. The same holds true for the result. We will use XSLT transformations to achieve this.
As BPEL and web services are based on XML, variables in BPEL are also designed to store XML directly. This means dealing with XML, therefore a good understanding of XML and XML Schema is a prerequisite for successful BPEL development. We will not go into the details of XML and XML Schema in this book; to find out more about these, please refer to the related literature, such as XML, and http://www.w3.org/XML/Schema and http://www.w3schools.com/schema/ for XML Schema.
Declaring variables
In BPEL, we have to declare each variable before we use it. We can declare variables globally at the beginning of a BPEL process declaration document. We can also declare local (scoped) variables within scopes. We will discuss scopes in Chapter 7, Working with Scopes. In this chapter, we will focus only on globally declared variables.
We declare variables within the <variables>
activity, which is placed after the <partnerLinks>
declarations. Within <variables>
, we declare each variable using the <variable>
declaration. The following example shows the structure of a BPEL process that uses variables:
<process ...> <partnerLinks> ... </partnerLinks> <variables> <variable ... /> <variable ... /> ... </variables> <sequence> ... </sequence> </process>
Variable types
We already know that variables store XML data. To achieve type safety, in other words, to be sure that we store the correct structure in each variable, variables in BPEL are typed. When we declare a variable, we have to provide a type.
Variables can store XML Schema simple types, XML Schema elements, or WSDL messages. To declare a variable, we must specify the variable name and type. To specify the type, we have the following three options:
type
: This is a variable that stores an XML Schema simple typeelement
: This is a variable that stores an XML Schema elementmessageType
: This is a variable that stores a WSDL message
To declare variables of simple types, we use XML Schema simple types. We can use the built-in simple types or declare our own. XML Schema provides a wide selection of well-known simple types, such as boolean
, string
, byte
, decimal
, double
, float
, int
, integer
, long
, short
, date
, dateTime
, duration
, time
; and some less common ones, such as negativeInteger
, nonNegativeInteger
, nonPositiveInteger
, positiveInteger
, unsignedShort
, unsignedLong
, unsignedInt
, normalizedString
, anyUR
, token
, and so on. For a complete list of XML Schema data types, please refer to the specification: http://www.w3.org/TR/xmlschema11-2/.
To declare variables that store complex XML structures, we can use any XML element from the XML Schema. This is useful to store XML documents or their fragments.
To store the request for service invocations and responses from services, we will use variables that can store the whole messages accepted or returned from services. For this, we will declare the messageType
variables. The type of these variables is specified by the WSDL messages.