13.3. 运行服务器端任务和脚本
在 Data Grid 服务器上执行任务和自定义脚本。
13.3.1. 运行任务和脚本 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
使用命令行界面在 Data Grid 集群中运行的任务和脚本。
流程
- 创建与 Data Grid 的 CLI 连接。
使用
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);