Basic Grid Task Example
Package:
org.gridgain.examples.helloworld.grid
This example contains HelloWorld example that is using direct grid task execution. It demonstrates a simple use of GridGain grid. String "Hello World" is passed as an argument for execution of GridHelloWorldTask task. The task will split the passed in string into separate words and execute each word on a different node. As an outcome, one node will print out word "Hello" and the other will printout word "World". If there is only one node participating, then it will print out both words.
There are two classes implemented for this example:
Running Grid Node
This example will need one remote node to be running. Note that you don't need another machine for it - you can start remote node on the same machine you are running example on.
To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.
GridHelloWorldExample.java
1. Import GridGain classes.
import org.gridgain.grid.*;
2. Add Grid Start and Stop.
GridFactory.start();
try {
...
}
finally {
GridFactory.stop(true);
}
finally clause allows for graceful grid shutdown in case of the exceptions.
3. Add Grid Task Execution.
GridTaskFuture<Object> future = grid.execute(GridHelloWorldTask.class, "Hello World"); // Wait for the task to complete. fugure.get();
Note that method grid.execute(...) returns GridTaskFuture ![]()
![]()
Source Code (edited)
package org.gridgain.examples.helloworld.api; import org.gridgain.grid.*; public final class GridHelloWorldExample { public static void main(String[] args) throws GridException { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { Grid grid = GridFactory.getGrid(); // Execute Hello World task. GridTaskFuture<Integer> future = grid.execute(GridHelloWorldTask.class, "Hello World"); // Wait for task completion. int phraseLen = future.get(); System.out.println(">>>"); System.out.println(">>> Finished executing Grid \"Hello World\" example with custom task."); System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'."); System.out.println(">>> You should see print out of 'Hello' on one node and 'World' on another node."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
GridHelloWorldTask.java
This is a grid task implementation that is responsible for split and aggregate (a.k.a map/reduce) logic. Note that this implementation uses GridTaskSplitAdapter ![]()
Source Code (edited)
package org.gridgain.examples.helloworld.api; import org.gridgain.grid.*; import java.util.*; import java.io.*; public class GridHelloWorldTask extends GridTaskSplitAdapter<String, Integer> { /** Auto-injected grid logger. */ @GridLoggerResource private GridLogger log = null; @Override public Collection<? extends GridJob> split(int gridSize, String phrase) throws GridException { // Split the passed in phrase into multiple words separated by spaces. String[] words = phrase.split(" "); List<GridJob> jobs = new ArrayList<GridJob>(words.length); for (String word : words) { // Every job gets its own word as an argument. jobs.add(new GridJobAdapter<String>(word) { /* * Simply prints the word passed into the job and * returns number of letters in that word. */ public Serializable execute() { String word = getArgument(); if (log.isInfoEnabled() == true) { log.info(">>>"); log.info(">>> Printing '" + word + "' on this node from grid job."); log.info(">>>"); } // Return number of letters in the word. return word.length(); } }); } return jobs; } /** * Sums up all letters from all jobs and returns a * total number of letters in the phrase. * * @param results Job results. * @return Number of letters for the phrase passed into * <tt>split(gridSize, phrase)</tt> method above. * @throws GridException If reduce failed. */ public Integer reduce(List<GridJobResult> results) throws GridException { int totalCharCnt = 0; for (GridJobResult res : results) { // Every job returned a number of letters // for the word it was responsible for. Integer charCnt = res.getData(); totalCharCnt += charCnt; } // Account for spaces. For simplicity we assume one space between words. totalCharCnt += results.size() - 1; // Total number of characters in the phrase // passed into task execution. return totalCharCnt; } }
