Over a million developers have joined DZone.

GridGain: Hello World Grid-Enabled Executor Service

DZone's Guide to

GridGain: Hello World Grid-Enabled Executor Service

· ·
Free Resource

GridGain - Grid Computing Made SimpleGridGain 2.0.3 has been released. For the most part it was a stability release which had undergone tremendous amount of testing and as a result is a great improvement to overall fault-tolerance and scalability of the product. We have tested all sorts of scenarios where nodes kept joining and leaving grid at will under significant load and introduced a lot of performance improvements.

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.

// 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.

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.
finally {
// Stop grid node.


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".



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}