이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.