Chapter 3. Variables, Data Manipulation, and Expressions

A very important part of each BPEL process is the orchestration of services (partner links) through the operation invocations. With each operation invocation, messages are exchanged. When a BPEL process invokes an operation on the external service and when it receives the result, we often want to store the messages temporarily, while the BPEL process is executing. The same holds true for other data that is related to the process state. This is where we need to use variables.

BPEL supports variables like any other programming language. It allows data manipulation in different ways, as we will see in this chapter. In contrast to many other programming languages, variables in BPEL are stored in XML. This means that manipulating variables in BPEL requires us to be familiar with XML and XML schemas.

In this chapter, we will learn how to declare and use variables, how to store, assign, copy, and manipulate data within variables, which variable types are supported, and other interesting aspects about variables. We will also learn that variable declaration and manipulation in BPEL is closely related to XML schema and WSDL.

In this chapter, we will take a closer look at the variable declarations, Web Services Description Language (WSDL), assign activity, and other means for variable data manipulation. In this chapter, we shall:

  • Understand BPEL variables
  • Know how to declare variables and be familiar with variable types
  • Be familiar with data manipulation and the <assign> activity
  • Understand different possibilities with <assign>
  • Understand the XPath expressions and know how to access variables from expressions
  • Know how to validate variables
  • Understand how and when to use XSLT transformations

So, let's get started...

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 http://www.w3.org/XML/ and http://www.w3schools.com/xml/ for 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 type
  • element: This is a variable that stores an XML Schema element
  • messageType: 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.44.52