RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Grid Enable Your Java Applications with GridGain : Page 2

Grid computing, the idea of executing code among various computers to solve complex time- and resource-intensive algorithms, has intrigued researchers for decades. GridGain lets Java developers tap into the grid.

Grid Enabling with GridGain
Along the lines of the Einstein tenet, "Make everything as simple as possible, but not simpler," GridGain has made grid computing easy. It implements all the low-level details, which the developer may or may not need to know. The following section walks through a code example from the downloadable source code to demonstrate the power of grid parallel processing with GridGain.

The GridMatrixMultiplier.java file has the code to multiply any two given matrices. The code populates the matrices with sequential numbers for demonstration (but you could change that as needed):

public int compute(int arr1[], int arr2[]){
		int result =0;
		for(int i=0; i < arr1.length ; i++)
	 	{result += arr1[i] * arr2[i];}
		return result;	}

If you add the @Gridify annotation to the method, the byte code for the method is shipped to a remote node, executed, and the results returned. Analogous to the JVM inside a browser communicating with a web server to download and execute applet byte code, the Gridify annotation ships the byte code of the class and its dependencies to a remote node within the topology, where it is executed.

To see how GridGain handles failover, increase the matrix size in GridMatrixMultiplier.java and start up multiple nodes. When you see the print statement in all the nodes, kill one of the nodes and observe the failover and recovery part. GridGain will handle failover transparently without affecting the result of the computation.

While a grid application is running, try adding a new node to the topology. The load balancer immediately will assign some processing to the new node. When all the nodes in the topology go down, the main application will run as a local node.

GridHierarchyTest.java demonstrates the fact that not only the class that has the Gridify method is shipped to the remote node, but also all the dependent class hierarchies as needed. In this example, Child.java invokes the Parent.java print method, which in turn calls the PrintHelper.java print method.

In the examples above, the Gridify method could be run on any node. If you needed different sections of the code to be executed in different topologies, GridGain provides GridTask and GridJob classes along with the corresponding Adapter classes for custom processing. GridTask provides callback methods for defining logic to split and aggregate the results, while GridJob is a single unit of distributable work.

Practical Performance Limitations
Because remote execution causes network delays, distributed computing may be overkill for relatively small applications. Time- and resource-intensive applications, such as those for simulation models, likely will benefit most from the GridGain framework, but the increased hardware utilization that it provides could impact IT hardware vendors.

Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date