Too many global variables can cause a lot of confusion and can also consume a lot of memory. Hence, we define global variables only when we need data to be available all the time. For that reason, we omit the scope variable visibility by defining them locally. By doing this, we also achieve higher clarity of the BPEL process source code. This recipe will explain the difference between global and local variables in the BPEL processes.
For this recipe, we will extend the BPEL process from the previous recipe. Note that the variables are tightly related to the scope, which encloses them. We will add a scope to our BPEL process and reuse the same web service. The BPEL process outlook is as shown in the following screenshot:
To define a local variable in a BPEL process, we perform the following steps:
LocalScope
scope of the BPEL process.The easiest way to check the local variable definition is to examine the source code of the BPEL process. We search for the msg
variable in the following code:
<scope name = "LocalScope" variableAccessSerializable = "no"> <variables> <variable name = "msg" type = "xsd:string"/> </variables> </scope>
The local variable is defined inside the scope definition. So, the variable is accessible as soon as we enter the scope. Consequently, we lose access to the variable as soon as we leave the scope enclosing the local variable definition.
Similar to the global variable definition, we can also define the variable from the toolbar of the BPEL process. This time, we already have one local variable listed as shown in the following screenshot:
To define a global variable, we select the Variables
folder under the Process node. However, to define a local variable, we need to select the Variables
folder under the Scope – LocalScope node.
When creating the <receive>
, <reply>
, <invoke>
, and <catch>
activities, we have the possibility to create a global or local variable. This option is presented through the Create Variable dialog as shown in the following screenshot: