Over a million developers have joined DZone.

Cheat Sheet: GridGain vs. GigaSpaces

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

GridGain - Grid Computing Made SimpleIn his SyncRemoting Cookbook blog, Owen Taylor from GigaSpaces outlines a cheat-sheet that he created for writing GigaSpaces apps. While you can go ahead and read all the required steps from Owen's blog, I just want to counter it with GridGain cheat sheet you would use for creating the same app:

  1. Create GridTask implementation (you need to implement 2 methods, 'map(..) or split(..)' and 'reduce(..)').
  2. Start multiple stand-alone GridGain nodes (if not already started) by executing gridgain.sh or gridgain.bat scripts.
  3. Execute the task you created directly from IDE of your choice (Elipse, IDEA, NetBeans, ...) by calling Grid.execute(MyTask.class, ..) method.

This comparison kind of speaks for itself. Note how there are no explicit deployment steps either. With GridGain, all classes are peer-class-loaded automatically.

Below is all the code that you would need to write with GridGain. First we create MyWidgetTask which extends convenience GridTaskSplitAdapter adapter and implements 2 methods, 'split(..)' and 'reduce(..)'.

public class MyWidgetTask extends GridTaskSplitAdapter<Object, Double> {
// Split task into multiple jobs to run on remote nodes.
public Collection<? extends GridJob> split(int gridSize, Object arg) {
List<GridJobAdapter<Object>> jobs =
new ArrayList<GridJobAdapter<Object>>(gridSize);

for (int i = 0; i < gridSize; i++) {
// Create jobs to sent to remote nodes.
jobs.add(new GridJobAdapter<Object>() {
// This code will run on remote grid nodes.
public Serializable execute() {
// Access jboss cache
// (could be any other cache or even local HashMap).
Map<Integer, Widget> widgets = cache.getRoot().
getChild(Fqn.fromString("/example/widgets")).getData();

double priceTotal = 0.0d;

// Calculate average price of all widgets
// stored on the grid node this job is
// executing on.
for (Widget widget : widgets.values()) {
priceTotal += widget.getPrice();
}

return priceTotal / widgets.size();
}
});
}
}

// Combines all results received from remote nodes.
public Double reduce(List<GridJobResult> results) {
double priceTotal = 0.0d;

for (GridJobResult res : results) {
double price = res.getData();

priceTotal += price;
}

// Return average widget price.
return priceTotal / results.size();
}
}

Now, let's execute our task on the Grid:

public class MyWidgetExample {
public static void main(String[] args) {
Grid grid = GridFactory.start();

try {
GridTaskFuture<Double> future = grid.execute(MyWidgetTask.class, null);

System.out.println("Average widget price is: " + future.get());
}
finally {
GridFactory.stop(true);
}
}
}

This is it as far as coding. GridGain also does not require any XML configuration unless you want to change the defaults.

Oh yeah... I also forgot to mention that GridGain is Open Source and is absolutely free to use ;-)

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}