14.2. Adding scripts to Data Grid Server deployments
Use the command line interface to add scripts to Data Grid Server.
Prerequisites
Data Grid Server stores scripts in the ___script_cache cache. If you enable cache authorization, users must have CREATE permissions to add to ___script_cache.
Assign users the deployer role at minimum if you use default authorization settings.
Procedure
Define scripts as required.
For example, create a file named
multiplication.jsthat runs on a single Data Grid server, has two parameters, and uses JavaScript to multiply a given value:// mode=local,language=javascript multiplicand * multiplier- Create a CLI connection to Data Grid.
Use the
taskcommand to upload scripts, as in the following example:task upload --file=multiplication.js multiplicationVerify that your scripts are available.
ls tasks multiplication
14.2.1. Data Grid Server scripts リンクのコピーリンクがクリップボードにコピーされました!
Data Grid Server scripting is based on the javax.script API and is compatible with any JVM-based ScriptEngine implementation.
Hello world
The following is a simple example that runs on a single Data Grid server, has one parameter, and uses JavaScript:
// mode=local,language=javascript,parameters=[greetee]
"Hello " + greetee
When you run the preceding script, you pass a value for the greetee parameter and Data Grid returns "Hello ${value}".
14.2.1.1. Script metadata リンクのコピーリンクがクリップボードにコピーされました!
Metadata provides additional information about scripts that Data Grid Server uses when running scripts.
Script metadata are property=value pairs that you add to comments in the first lines of scripts, such as the following example:
// name=test, language=javascript
// mode=local, parameters=[a,b,c]
-
Use comment styles that match the scripting language (
//,;;,#). -
Separate
property=valuepairs with commas. - Separate values with single (') or double (") quote characters.
| Property | Description |
|---|---|
|
| Defines the execution mode and has the following values:
|
|
| Specifies the ScriptEngine that executes the script. |
|
| Specifies filename extensions as an alternative method to set the ScriptEngine. |
|
| Specifies roles that users must have to execute scripts. |
|
| Specifies an array of valid parameter names for this script. Invocations which specify parameters not included in this list cause exceptions. |
|
| Optionally sets the MediaType (MIME type) for storing data as well as parameter and return values. This property is useful for remote clients that support particular data formats only.
Currently you can set only |
14.2.1.2. Script bindings リンクのコピーリンクがクリップボードにコピーされました!
Data Grid exposes internal objects as bindings for script execution.
| Binding | Description |
|---|---|
|
| Specifies the cache against which the script is run. |
|
| Specifies the marshaller to use for serializing data to the cache. |
|
|
Specifies the |
|
| Specifies the instance of the script manager that runs the script. You can use this binding to run other scripts from a script. |
14.2.1.3. Script parameters リンクのコピーリンクがクリップボードにコピーされました!
Data Grid lets you pass named parameters as bindings for running scripts.
Parameters are name,value pairs, where name is a string and value is any value that the marshaller can interpret.
The following example script has two parameters, multiplicand and multiplier. The script takes the value of multiplicand and multiplies it with the value of multiplier.
// mode=local,language=javascript
multiplicand * multiplier
When you run the preceding script, Data Grid responds with the result of the expression evaluation.