We will try to execute Dexecutor in a distributed mode using Hazelcast. For the demo we would be setting up multiple Hazelast nodes on single machine.
Refer Introducing Dexecutor, to get an introduction on Dexecutor and to understand the problem we would solve in a distribute fashion. In short:
We would be distributing the execution of dexecutor tasks on Hazelcast compute nodes in a single machine.
To do that one of the nodes would act as master and submit the tasks to Hazelcast compute nodes to be executed by other Hazelcast compute nodes using Dexecutor.
Here are the steps to do that :
Step 1: Add dexecutor-hazelcast dependency
<dependency> <groupId>com.github.dexecutor <artifactId>dexecutor-hazelcast <version>LATEST_RELEASE
Step 2: Get an Instance of Hazelcast IExecutorService from Hazelcast
Config cfg = new Config(); HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg); IExecutorService executorService = instance.getExecutorService("test");
Step 3 : Create Dexecutor using IExecutorService
if (isMaster) { DefaultDependentTasksExecutor<Integer, Integer> dexecutor = newTaskExecutor(executorService); buildGraph(dexecutor); dexecutor.execute(ExecutionConfig.TERMINATING); }
private DefaultDependentTasksExecutor<Integer, Integer> newTaskExecutor(IExecutorService executorService) { DependentTasksExecutorConfig<Integer, Integer> config = new DependentTasksExecutorConfig<Integer, Integer>( new HazelcastExecutionEngine<Integer, Integer>(executorService), new SleepyTaskProvider()); return new DefaultDependentTasksExecutor<Integer, Integer>(config); } private static class SleepyTaskProvider implements TaskProvider<Integer, Integer> { public Task<Integer, Integer> provideTask(final Integer id) { return new HazelcastTask(id); } }
Step 4: Execution
Open three terminals and execute the following :
Terminal #1
mvn test-compile exec:java -Djava.net.preferIPv4Stack=true -Dexec.mainClass="com.github.dexecutor.hazelcast.Node" -Dexec.classpathScope="test" -Dexec.args="s node-A"
Terminal #2
mvn test-compile exec:java -Djava.net.preferIPv4Stack=true -Dexec.mainClass="com.github.dexecutor.hazelcast.Node" -Dexec.classpathScope="test" -Dexec.args="s node-B"
Terminal #3
mvn test-compile exec:java -Djava.net.preferIPv4Stack=true -Dexec.mainClass="com.github.dexecutor.hazelcast.Node" -Dexec.classpathScope="test" -Dexec.args="m node-C"
Here is the Execution
Here is the Complete Node Implementation