Dmitriy Setrakyan manages daily operations of GridGain Systems and brings over 12 years of experience to GridGain Systems which spans all areas of application software development from design and architecture to team management and quality assurance. His experience includes architecture and leadership in development of distributed middleware platforms, financial trading systems, CRM applications, and more. Dmitriy is a DZone MVB and is not an employee of DZone and has posted 54 posts at DZone. You can read more from them at their website. View Full User Profile

GridGain: Hello World Grid-Enabled Executor Service

05.29.2008
| 4787 views |
  • submit to reddit

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

0
Average: 5 (1 vote)
Published at DZone with permission of Dmitriy Setrakyan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)