13.3. 运行服务器端任务和脚本

在 Data Grid 服务器上执行任务和自定义脚本。

13.3.1. 运行任务和脚本

使用命令行界面在 Data Grid 集群中运行的任务和脚本。

流程

  1. 创建与 Data Grid 的 CLI 连接。
  2. 使用 task 命令运行任务和脚本,如下例所示:

    • 执行名为 multipler.js 的脚本并指定两个参数:

      [//containers/default]> task exec multipler.js -Pmultiplicand=10 -Pmultiplier=20
      200.0
    • 执行名为 @@cache@names 的任务,以检索所有可用缓存的列表:

      //containers/default]> task exec @@cache@names
      ["___protobuf_metadata","mycache","___script_cache"]

13.3.2. 以编程方式运行脚本

调用 execute () 方法以使用 Hot Rod RemoteCache 接口运行脚本,如下例所示:

RemoteCache<String, Integer> cache = cacheManager.getCache();
// Create parameters for script execution.
Map<String, Object> params = new HashMap<>();
params.put("multiplicand", 10);
params.put("multiplier", 20);
// Run the script with the parameters.
Object result = cache.execute("multiplication.js", params);

13.3.3. 以编程方式运行任务

调用 execute () 方法以使用 Hot Rod RemoteCache 接口运行任务,如下例所示:

// Add configuration for a locally running server.
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host("127.0.0.1").port(11222);

// Connect to the server.
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());

// Retrieve the remote cache.
RemoteCache<String, String> cache = cacheManager.getCache();

// Create task parameters.
Map<String, String> parameters = new HashMap<>();
parameters.put("name", "developer");

// Run the server task.
String greet = cache.execute("hello-task", parameters);
System.out.println(greet);