第41章 リモートタスクの実行
41.1. リモートタスクの実行
タスクまたはビジネスロジックは直接 JBoss Data Grid サーバーで実行できるため、タスクの実行はデータに近く、クラスターのすべてのノードにあるリソースを使用します。
タスクを Java 実行可能ファイルにバンドルし、プログラミングで実行可能ファイルを実行できるサーバーインスタンスにデプロイすることができます。
41.2. リモートタスクの作成
リモート実行のタスクを作成するには、 org.infinispan.tasks.ServerTask インターフェースを実装するクラスが含まれる .jar ファイルを作成する必要があります。
実装には以下のメソッドが必要になります。
-
void setTaskContext(TaskContext taskContext): タスクコンテキストを設定します。このメソッドを使用して、キャッシュや必要なその他のリソースにアクセスします。 -
String getName(): タスクに一意な名前を提供します。この名前はTaskManagerによる実行に使用されます。
以下は実装の任意のメソッドになります。
-
TaskExecutionMethod getExecutionMode():TaskExecutionMode.ONE_NODEのようにタスクが 1 つのノードで実行されるか、またはTaskExecutionMode.ALL_NODESのようにすべてのノードで実行されるかを決定します。デフォルトでは 1 つのノードで実行されます。 -
Optional<String> getAllowedRole(): ユーザーがタスクの実行に必要なロールを設定します。デフォルトでは追加のユーザーロールは設定されません。詳細は「リモートタスクの実行」を参照してください。 -
Set<String> getParameters(): タスクに使用する名前付きパラメーターを指定します。
41.3. リモートタスクの例
以下には、org.infinispan.tasks.ServerTask インターフェースを実装するクラスの例が含まれています。
public class HelloTask implements ServerTask<String> {
private TaskContext ctx;
//Set the task context.
@Override
public void setTaskContext(TaskContext ctx) {
this.ctx = ctx;
}
//Take the name of a person as a parameter.
//Return a greeting with that person's name.
@Override
public String call() throws Exception {
String name = (String) ctx.getParameters().get().get("name");
return "Hello " + name;
}
//Return a unique name that clients can use to invoke the task.
@Override
public String getName() {
return "hello-task";
}
}41.4. リモートタスクのインストール
リモートタスクを作成し、.jar ファイルにバンドルしたら、以下のオプションの 1 つを使用して JBoss Data Grid サーバーインスタンスにデプロイできます。
オプション 1: deployments ディレクトリーへコピーする
.jarファイルを deployments/ ディレクトリーにコピーします。$] cp /path/to/sample_task.jar $JDG_HOME/standalone/deployments/
オプション 2: CLI でコピーする
JBoss Data Grid サーバーに接続します。
[$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
.jarファイルをデプロイします。deploy /path/to/sample_task.jar
注記JBoss Data Grid がドメインモードである場合、
--all-server-groupsまたは--server-groupsパラメーターを使用してサーバーグループを市営する必要があります。
41.5. リモートタスクの削除
JBoss Data Grid の実行中のインスタンスからリモートタスクを削除するには、以下を行います。
JBoss Data Grid サーバーに接続します。
[$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
undeployコマンドを実行して.jarファイルを削除します。undeploy /path/to/sample_task.jar
注記JBoss Data Grid がドメインモードである場合、
--all-server-groupsまたは--server-groupsパラメーターを使用してサーバーグループを市営する必要があります。
41.6. リモートタスクの実行
JBoss Data Grid サーバーで承認が有効になっている場合、 EXEC パーミッションを持つユーザーのみがリモートタスクを実行できます。承認が有効になっていない場合は、すべてのユーザーがリモートタスクを実行できます。
リモートタスクには、getAllowedRole メソッドで指定された追加のユーザーロールを付与することができます。この場合、ユーザーはそのロールに属さないとリモートタスクを実行できません。
以前デプロイしたタスクを実行するには、対象のキャッシュで execute(String taskName, Map parameters) を呼び出します。
以下の例は、sampleTask という名前のタスクを実行する方法を示しています。
import org.infinispan.client.hotrod.*;
import java.util.*;
[...]
String TASK_NAME = "sampleTask";
RemoteCacheManager rcm = new RemoteCacheManager();
RemoteCache remoteCache = rcm.getCache();
// Assume the task takes a single parameter, and will return a result
Map<String, String> params = new HashMap<>();
params.put("name", "James");
String result = (String) remoteCache.execute(TASK_NAME, params);
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.