Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Cheat Sheet: GridGain vs. GigaSpaces

DZone's Guide to

Cheat Sheet: GridGain vs. GigaSpaces

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 ;-)

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

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 }}