14.3. スクリプトおよびタスクの実行
コマンドラインインターフェイスを使用して、Data Grid Server デプロイメントでタスクおよびスクリプトを実行します。また、Hot Rod クライアントからスクリプトとタスクを実行することもできます。
前提条件
- スクリプトまたはタスクを Data Grid Server に追加します。
手順
- Data Grid への CLI 接続を作成します。
以下の例のように、
taskコマンドを使用して、タスクおよびスクリプトを実行します。multiplier.jsという名前のスクリプトを実行し、2 つのパラメーターを指定します。task exec multiplier.js -Pmultiplicand=10 -Pmultiplier=20 200.0@@cache@namesという名前のタスクを実行して、利用可能なすべてのキャッシュのリストを取得します。task exec @@cache@names ["___protobuf_metadata","mycache","___script_cache"]
プログラムによる実行
-
以下の例のように、
execute()を呼び出して、Hot RodRemoteCacheインターフェイスを使用してスクリプトを実行します。
スクリプト実行
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);
タスクの実行
// 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);