Chapter 41. Remote Task Execution
41.1. Remote Task Execution
Tasks, or business logic, can run directly on JBoss Data Grid servers, which means task execution is close to the data and uses the resources of all nodes in the cluster.
You can bundle tasks in Java executable files and deploy them to server instances where you can run the executables programmatically.
41.2. Creating Remote Tasks
To create a task for remote execution, you must create a .jar file that contains a class that implements the org.infinispan.tasks.ServerTask interface.
The following methods are required in the implementation:
-
void setTaskContext(TaskContext taskContext)Sets the task context. Use this method to access caches and other necessary resources. -
String getName()Provides a unique name for the task. This name is used for execution byTaskManager.
The following methods are optional in the implementation:
-
TaskExecutionMethod getExecutionMode()Determines if the task is executed on one node, as inTaskExecutionMode.ONE_NODE, or on all nodes, as inTaskExecutionMode.ALL_NODES. Execution on one node is the default. -
Optional<String> getAllowedRole()Sets a role that users must have to run a task. No additional user role is set by default. For more information, see Running Remote Tasks. -
Set<String> getParameters()Specifies named parameters for use with the task.
41.3. Remote Task Example
The following provides an example class that implements the org.infinispan.tasks.ServerTask interface:
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. Installing Remote Tasks
After you create a remote task and bundle it into a .jar file, you can deploy it to a JBoss Data Grid server instance with one of the following options:
Option 1: Copy to the Deployments Directory
Copy the
.jarfile to the deployments/ directory.$] cp /path/to/sample_task.jar $JDG_HOME/standalone/deployments/
Option 2: Deploy with the CLI
Connect to the JBoss Data Grid server.
[$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
Deploy the
.jarfile.deploy /path/to/sample_task.jar
NoteIf JBoss Data Grid is in domain mode, you must specify the server groups with either the
--all-server-groupsor--server-groupsparameter.
41.5. Removing Remote Tasks
You can remove remote tasks from the running instances of JBoss Data Grid as follows:
Connect to the JBoss Data Grid server.
[$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
Run the
undeploycommand to remove the.jarfile.undeploy /path/to/sample_task.jar
NoteIf JBoss Data Grid is in domain mode, you must specify the server groups with either the
--all-server-groupsor--server-groupsparameter.
41.6. Running Remote Tasks
If authorization is enabled on the JBoss Data Grid server, only users with EXEC permissions can run remote tasks. If authorization is not enabled, any user can run remote tasks.
Remote tasks can have additional user roles specified with the getAllowedRole method. In this case, users must belong to the role to run remote tasks.
To execute a previously deployed task call execute(String taskName, Map parameters) on the desired cache.
The following example shows how to run a task named 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.