ALERT! The community will be read-only on April 19, 8am Pacific as the migration begins. Read more for important details.
ALERT! The community will be read-only on April 19, 8am Pacific as the migration begins.Read more for important details.

Global variables across server scripts

Global variables across server scripts

From time to time, for performance reasons or architectural implementation reasons, you need to use a variable globally across multiple instances of server side scripts. The way to do that within BSM is to use "state" variables. State variables are available on any script that runs in context of the server such as an Automation (serverside) or a right-click operation (serverscript).

One common use case is when you have automations set up to log entries to a database or to open tickets directly to the database. Since it would not be a well performing script to open and close the database connection over and over again, one solution is to open the connection once and hold onto it within a state variable.

The other idea typically used in conjunction with that is to have the automation script do quick checks at the top of the script to determine if the particular alarm needs processed, if so, push it to a global queue, list, collection, etc. Next, you would have another script that is waking up from time to time to check the queue for work to do.

Below is a script fragment example...
if( !state.myList )
{
var myList= java.util.HashSet();
state.myList = myList
}


The idea is, have your script set up to start when the server starts up (IE: script.onStarted). It checks to see if the state variable exists, if not, it creates it. This could be a queue to hold alarm ID's, a sql connection, etc.

Within the serverscript right-click, you can easily add items to the list such as....

state.myList.add( "Junk" )

Another use case I remember from the past was a script that loaded a bunch of data from a database and then used this information for automation scripts, additional algorithm logic, etc.

As always, be careful, while state variables are useful, you need to be careful, the state variable is held in the engines core JVM, as in java heap, try not to load tons-o-data.
Labels (1)

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
5 of 5
Last update:
‎2020-01-22 22:49
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.