Chapter 37. Remote Task Execution

37.1. Remote Task Execution

Tasks, or business logic, may be executed directly on JBoss Data Grid servers, allowing this logic to be executed close to the data and using the resources of all nodes in the cluster. These tasks may be bundled in java executables which are deployed to the server instances, and once deployed the tasks may be executed programmatically.

37.2. Creating a Remote Task

To create a task for remote execution a .jar file must be created containing a class that implements org.infinispan.tasks.ServerTask. This interface contains the following methods that must be implemented:

  • void setTaskContext(TaskContext taskContext) - sets the task context; should be used to access caches and other resources necessary.
  • String getName() - provides a unique name for the task. This is the name that will be used for execution by the TaskManager.

In addition to the above the following may be implemented; however, these are not required for execution:

  • TaskExecutionMethod getExecutionMode() - Determines if the task is executed on one node, TaskExecutionMode.ONE_NODE, or on all nodes, TaskExecutionMode.ALL_NODES. Execution on one node is enabled by default.
  • Optional<String> getAllowedRole() - Sets the role that may execute this task. By default no role is provided, indicating that no additional role is required for execution. Additional information on executing tasks is found in Running Remote Tasks.
  • Set<String> getParameters() - A collection of named parameters for use with the task.

37.3. Installing Remote Tasks

Once a remote task has been created, and bundled into a .jar, it may be deployed to the server by using either of the following methods:

Option 1: Using the Deployments Directory

  1. Copy the deployment into the deployments/ directory of the JBoss Data Grid server as seen in the following example:

    $] cp /path/to/sample_task.jar $JDG_HOME/standalone/deployments/

Option 2: Using the CLI.. Connect to the JDG server by running the below command:

+

[$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
  1. Deploy the .jar file by executing the following command:

    deploy /path/to/sample_task.jar
Note

When JBoss Data Grid is used in domain mode the server groups must be specified using either the --all-server-groups or --server-groups parameters.

37.4. Removing Remote Tasks

Remote tasks may be removed from the running instances by using the JBoss Data Grid CLI. Follow the below instructions to remove a remote task:

  1. Connect to the JDG server by running the below command:

    [$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
  2. Remove the .jar file by using the undeploy command, as seen below:

    undeploy /path/to/sample_task.jar
    Note

    When JBoss Data Grid is used in domain mode the server groups must be specified using either the --all-relevant-server-groups or --server-groups parameters.

37.5. Running Remote Tasks

If authorization is disabled on the server then anyone may execute remote tasks once they have been installed. Otherwise, only users with EXEC permissions will be allowed to run previously installed tasks. If a remote task has additional users specified, via the getAllowedRole method, then users must also belong to this role to execute the script.

To execute a previously deployed task call execute(String taskName, Map parameters) on the desired cache. The following example demonstrates executing a task with the name 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", "value");

String result = (String) remoteCache.execute(TASK_NAME, params);