Este conteúdo não está disponível no idioma selecionado.

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";
   }

}
Copy to Clipboard Toggle word wrap

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/
    Copy to Clipboard Toggle word wrap

Option 2: Deploy with the CLI

  1. Connect to the JBoss Data Grid server.

    [$JDG_HOME] $ bin/cli.sh --connect --controller=$IP:$PORT
    Copy to Clipboard Toggle word wrap
  2. Deploy the .jar file.

    deploy /path/to/sample_task.jar
    Copy to Clipboard Toggle word wrap
    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
    Copy to Clipboard Toggle word wrap
  2. Run the undeploy command to remove the .jar file.

    undeploy /path/to/sample_task.jar
    Copy to Clipboard Toggle word wrap
    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);
Copy to Clipboard Toggle word wrap
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat