Shared Variables
Shared variables are used to save the state, either at the module level or for the duration of a job.
Using shared variables, you can share data across process instances associated with a module or a job. A process instance can read or update the data stored in a shared variable. The shared variable data updated by one process instance is accessible to other process instances of a Module or Job.
There are two types of shared variables: module shared variables and job shared variables. Both module and job shared variables are defined at the module level and can be accessed in a process using the activities Set Shared Variable and Get Shared Variable.
For more information on how to define and use shared variables, see Using Shared Variables in the TIBCO ActiveMatrix BusinessWorks™ Application Development guide.
Module Shared Variables
Module shared variables are used to share the state at a module level and are visible to all process instances created from the processes that are within a module. Module shared variables can be read and updated by the process instances during execution. Once the value is updated, the new value is available to all the process instances created from the processes that are within the module. Consider an example where the exchange rates are updated daily and the updated exchange rates should be accessible to all processes in a module. You can create a module shared variable to hold the exchange rate and use one process in the module for updating the exchange rate. All other processes in the module that require the exchange rate can retrieve the current value through the module shared variable.
Persistent Module Shared Variable
The current state of a module shared variable is stored in memory by default and the state of the module shared variable is lost in case the ActiveMatrix BusinessWorks™ engine (or the AppNode) crashes. However, to preserve the current state in case of an engine failure, the module shared variable can be configured to be persistent. For more information on configuring module shared variables with persistent option, see Using Shared Variables in the TIBCO ActiveMatrix BusinessWorks™ Application Development guide. When a module shared variable is configured as persistent, its current state is written to the engine database and on engine restart the module shared variable state is restored.
Sharing Module Shared Variable Across Multiple Engines (AppNodes)
A module shared variable state can be accessible across multiple engines if the engine persistent mode (bw.engine.peristentMode) is set to "group" and the module shared variable is configured to be persistent. For more information on configuring engine persistence, see Engine Persistence Modes in the TIBCO ActiveMatrix BusinessWorks™ Administration guide. Using the persistence option, the same ActiveMatrix BusinessWorks™ applications running in multiple engines (AppNodes) that are part of the same engine group can read or update the module shared variable state. Once the value is updated, the new value is available to all engines (AppNodes) which are running the same application.
Job Shared Variables
Job shared variables are used to share the state at the job level for the duration of a job. A copy of the job shared variable is created for each new job and it is accessible to all process instances associated with the job. Job shared variables can be used to share data between all process instances of a job without creating an input or output schema for the called process. They can also be set in shared modules, and accessed by application modules after setting up dependencies to the shared module.
Sharing Job Shared Variables Between Inline and Non-Inline Processes
Inline subprocesses are executed as part of the caller (parent) process jobs and therefore the current value of the job shared variable is passed from the caller process to the inline subprocess. Non-inline service subprocesses, and direct subprocesses that have been spawned, spawn a new thread and are not executed on the same job as the caller process. Hence non-inline subprocess and direct subprocesses that have been spawned, obtain a copy of the job shared variable and do not obtain the current value of the job shared variable from the caller process.
At runtime, the engine allocates a new job shared variable for each new job and the value of that variable is visible only to that job. Persistence option is not available for the Job Shared Variables.
Shared Variable Synchronization
Multiple process instances can potentially access or update a shared variable at the same time. For example, a module shared variable can be accessed by multiple jobs concurrently. Without a synchronization mechanism, a process instance could update the value of a shared variable while another process instance is trying to read the value. This could result in an unpredictable value for the shared variable.
Critical Section groups can be used to synchronize access to shared variables. A Critical Section group allows only one process instance to execute the Critical Section group and its contents at any given time. To synchronize shared variables, use a Critical Section group to contain the activities that access the shared variables (Set Shared Variable and Get Shared Variable). Once a process instance begins executing a Critical Section group, other concurrently running process instances that are associated with that Critical Section group wait at the start of the group until the currently running process instance exits the critical section group. This ensures that the value of the shared variable is not modified while another process instance is accessing it. To synchronize multiple critical section groups, use a shared lock. The shared lock can be defined using a module or a job shared variable.