However, this release does have a new feature I am very excited about - Grid-Enabled ExecutorService which executes all the tasks submitted to it on remote grid nodes. Basically, you use it as you would normally use java.util.concurrent.ExecutorService, but you get all the cool GridGain features right out of the box, such as peer-class-loading, fault-tolerance, load balancing, job scheduling and collision resolution, etc...
Here is a "Hello World" example that shows how simple it is to use it. Let's first create a simple java.util.concurrent.Callable that prints out a string and returns the number of characters in that string:
public class ExampleCallable implements Callable<Integer>, Serializable {
/** String argument. */
private String arg;
public ExampleCallable(String arg) {
this.arg = arg;
}
public Integer call() {
// Print out string passed in.
System.out.println(arg);
// Return number of characters.
return arg.length();
}
}
Now, let's execute our ExampleCallable on the grid:
public final class GridExecutorExample {
public static void main(String[] args) throws GridException {
// Start grid node.
GridFactory.start();
try {
Grid grid = GridFactory.getGrid();
// Create new grid-enabled ExecutorService
ExecutorService exec = grid.newGridExecutorService();
// Execute ExampleCallables on the grid.
Future<Integer> hello = exec.submit(new ExampleCallable("Hello");
Future<Integer> world = exec.submit(new ExampleCallable("World");
// Print out number of characters from both executions.
System.out.println("'Hello' character count: " + hello.get());
System.out.println("'World' character count: " + world.get());
// Close executor service.
exec.shutdown();
}
finally {
// Stop grid node.
GridFactory.stop(true);
}
}
}
To make it interesting, let's start a couple of stand alone grid nodes by simply executing gridgain.sh or gridgain.bat script under GRIDGAIN_HOME/bin folder (you can start them on the same physical box if you like).
Note that we don't need to do any deployment of our code to the grid. All required classes will be peer-class-loaded automatically.
After running our example, you should observe that one node will print word "Hello" and another node will print word "World".
Enjoy!
Add a comment