第14章 Running scripts and tasks on Data Grid Server
Add tasks and scripts to Data Grid Server deployments for remote execution from the Command Line Interface (CLI) and Hot Rod or REST clients. You can implement tasks as custom Java classes or define scripts in languages such as JavaScript.
14.1. Adding tasks to Data Grid Server deployments リンクのコピーリンクがクリップボードにコピーされました!
Add your custom server task classes to Data Grid Server.
Prerequisites
Stop Data Grid Server if it is running.
Data Grid Server does not support runtime deployment of custom classes.
Procedure
Add a
META-INF/services/org.infinispan.tasks.ServerTaskfile that contains the fully qualified names of server tasks, for example:example.HelloTask- Package your server task implementation in a JAR file.
-
Copy the JAR file to the
$RHDG_HOME/server/libdirectory of your Data Grid Server installation. - Add your classes to the deserialization allow list in your Data Grid configuration. Alternatively set the allow list using system properties.
14.1.1. Data Grid Server tasks リンクのコピーリンクがクリップボードにコピーされました!
Data Grid Server tasks are classes that extend the org.infinispan.tasks.ServerTask interface and generally include the following method calls:
setTaskContext()-
Allows access to execution context information including task parameters, cache references on which tasks are executed, and so on. In most cases, implementations store this information locally and use it when tasks are actually executed. When using
SHAREDinstantiation mode, the task should use aThreadLocalto store theTaskContextfor concurrent invocations. getName()- Returns unique names for tasks. Clients invoke tasks with these names.
getExecutionMode()Returns the execution mode for tasks.
-
TaskExecutionMode.ONE_NODEonly the node that handles the request executes the script. Although scripts can still invoke clustered operations. This is the default. -
TaskExecutionMode.ALL_NODESData Grid uses clustered executors to run scripts across nodes. For example, server tasks that invoke stream processing need to be executed on a single node because stream processing is distributed to all nodes.
-
getInstantiationMode()Returns the instantiation mode for tasks.
-
TaskInstantiationMode.SHAREDcreates a single instance that is reused for every task execution on the same server. This is the default. -
TaskInstantiationMode.ISOLATEDcreates a new instance for every invocation.
-
call()-
Computes a result. This method is defined in the
java.util.concurrent.Callableinterface and is invoked with server tasks.
Server task implementations must adhere to service loader pattern requirements. For example, implementations must have a zero-argument constructors.
The following HelloTask class implementation provides an example task that has one parameter. It also illustrates the use of a ThreadLocal to store the TaskContext for concurrent invocations.
package example;
import org.infinispan.tasks.ServerTask;
import org.infinispan.tasks.TaskContext;
public class HelloTask implements ServerTask<String> {
private static final ThreadLocal<TaskContext> taskContext = new ThreadLocal<>();
@Override
public void setTaskContext(TaskContext ctx) {
taskContext.set(ctx);
}
@Override
public String call() throws Exception {
TaskContext ctx = taskContext.get();
String name = (String) ctx.getParameters().get().get("name");
return "Hello " + name;
}
@Override
public String getName() {
return "hello-task";
}
}