Este contenido no está disponible en el idioma seleccionado.

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 by TaskManager.

The following methods are optional in the implementation:

  • TaskExecutionMethod getExecutionMode() Determines if the task is executed on one node, as in TaskExecutionMode.ONE_NODE, or on all nodes, as in TaskExecutionMode.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

  1. Copy the .jar file to the deployments/ directory.

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

Option 2: Deploy with the CLI

  1. Connect to the JBoss Data Grid server.

    [$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
  2. Deploy the .jar file.

    deploy /path/to/sample_task.jar
    Note

    If JBoss Data Grid is in domain mode, you must specify the server groups with either the --all-server-groups or --server-groups parameter.

41.5. Removing Remote Tasks

You can remove remote tasks from the running instances of JBoss Data Grid as follows:

  1. Connect to the JBoss Data Grid server.

    [$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
  2. Run the undeploy command to remove the .jar file.

    undeploy /path/to/sample_task.jar
    Note

    If JBoss Data Grid is in domain mode, you must specify the server groups with either the --all-server-groups or --server-groups parameter.

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);
Red Hat logoGithubRedditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

© 2024 Red Hat, Inc.