Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of RemoteTask execution with embedded HotRod server #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<artifactId>infinispan-core</artifactId>
<version>${version.infinispan}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-server-runtime</artifactId>
<version>${version.infinispan}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-server-hotrod</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.infinispan.playground.embeddedhotrod;

import java.util.Map;

import org.infinispan.tasks.ServerTask;
import org.infinispan.tasks.TaskContext;

public class ServerTaskExample implements ServerTask {

public static final String TASK_NAME = "just-example";
private static final ThreadLocal<TaskContext> treadLocalContext = new ThreadLocal();

@Override
public void setTaskContext(TaskContext taskContext) {
treadLocalContext.set(taskContext);
}

@Override
public Object call() throws Exception {
TaskContext taskContext = treadLocalContext.get();
Map<String, Object> params = (Map<String, Object>) taskContext.getParameters().get();
int first = (Integer) params.get("first");
int second = (Integer) params.get("second");
return first + second;
}

@Override
public String getName() {
return TASK_NAME;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.infinispan.playground.embeddedhotrod;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.infinispan.Cache;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.server.Extensions;
import org.infinispan.server.hotrod.HotRodServer;
import org.infinispan.server.hotrod.configuration.HotRodServerConfiguration;
import org.infinispan.server.hotrod.configuration.HotRodServerConfigurationBuilder;
import org.infinispan.tasks.TaskManager;

public class SimpleEmbeddedHotRodServer {

Expand All @@ -18,6 +23,13 @@ public static void main(String[] args) throws IOException {
DefaultCacheManager defaultCacheManager = new DefaultCacheManager("infinispan.xml");
Cache<String, String> embeddedCache = defaultCacheManager.getCache("default");

// Load remote tasks into task manager
GlobalComponentRegistry gcr = defaultCacheManager.getGlobalComponentRegistry();
Extensions extensions = new Extensions();
extensions.load(SimpleEmbeddedHotRodServer.class.getClassLoader());
TaskManager taskManager = gcr.getComponent(TaskManager.class);
taskManager.registerTaskEngine(extensions.getServerTaskEngine(defaultCacheManager));

// Create a Hot Rod server which exposes the cache manager
HotRodServerConfiguration build = new HotRodServerConfigurationBuilder().build();
HotRodServer server = new HotRodServer();
Expand Down Expand Up @@ -71,6 +83,12 @@ public static void main(String[] args) throws IOException {
System.out.printf("%s...", s);
}

System.out.print("\nVerifying remote task execution");
Map<String, Object> params = new HashMap<>();
params.put("first", 40);
params.put("second", 2);
assert Integer.valueOf(42).equals(remoteCache.execute(ServerTaskExample.TASK_NAME, params));

System.out.println("\nDone !");
remoteCacheManager.stop();
server.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.infinispan.playground.embeddedhotrod.ServerTaskExample