Gridify With Custom Task Example
This example contains HelloWorld example that is using Gridify ![]()
String 'Hello World' is passed as an argument to GridifyHelloWorldTaskExample.sayIt(String) method. Since this method is annotated with @Gridify ![]()
![]()
Package:
org.gridgain.examples.helloworld.gridify.task
There are two classes implemented for this example:
AspectJ AOP Configuration
We will use AspectJ AOP for this example. To use other AOP implementations (such as JBoss AOP, or Spring AOP), refer to AOP Configuration documentation.
The following configuration needs to be applied to enable AspectJ byte code weaving.
- JVM configuration should include: -javaagent:[GRIDGAIN_HOME]/libs/aspectjweaver-1.5.3.jar
- Classpath should contain the [GRIDGAIN_HOME]/config/aop/aspectj folder.
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.
GridifyHelloWorldTaskExample.java
1. Import GridGain classes.
import org.gridgain.grid.*; import org.gridgain.grid.gridify.*;
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 @Gridify Annotation.
We add @Gridify ![]()
@Gridify(taskClass = GridifyHelloWorldTask.class, timeout = 3000) public static int sayIt(String phrase) { // Simply print out the argument. System.out.println(">>>"); System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method."); System.out.println(">>>"); return phrase.length(); }
Full Source Code
package org.gridgain.examples.helloworld.gridify.task; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; public final class GridifyHelloWorldTaskExample { /** * Ensure singleton. */ private GridifyHelloWorldTaskExample() { // No-op. } /** * Method grid-enabled with {@link Gridify} annotation. Simply prints * out the argument passed in. Note that in this case instead of * using default task, we provide our own task which will split * the passed in string into separate words to be printed on * remote nodes. * * @param arg String to print. */ @Gridify(taskClass = GridifyHelloWorldTask.class, timeout = 3000) public static int sayIt(String phrase) { // Simply print out the argument. System.out.println(">>>"); System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method."); System.out.println(">>>"); return phrase.length(); } /** * Execute <tt>HelloWorld</tt> example grid-enabled with * <tt>Gridify</tt> annotation. * * @param args Command line arguments, none required but if provided * first one should point to the Spring configuration file. See * <tt>"examples/config/"</tt> for configuration file examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { // This method will be executed on a remote grid nodes. int phraseLen = sayIt("Hello World"); System.out.println(">>>"); System.out.println(">>> Finished executing Gridify \"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); } } }
GridifyHelloWorldTask.java
1. Import GridGain classes.
import org.gridgain.grid.*; import org.gridgain.grid.gridify.*;
2. Split Logic.
This is a grid task implementation that is responsible for split and aggregate (a.k.a map/reduce) logic. Note that this implementation uses GridifyTaskSplitAdapter ![]()
This grid task is responsible for splitting the passed in string into separate words and then passing each word into its own grid job for execution on remote nodes.
Full Source Code
package org.gridgain.examples.helloworld.gridify.task; import java.io.*; import java.util.*; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; /** * This grid task is responsible for splitting the passed in string into * separate words and then passing each word into its own grid job * for execution on remote nodes. */ public class GridifyHelloWorldTask extends GridifyTaskSplitAdapter<Integer> { /** * {@inheritDoc} */ @Override protected Collection<? extends GridJob> split(int gridSize, GridifyArgument arg) throws GridException { String[] words = ((String)arg.getMethodParameters()[0]).split(" "); List<GridJobAdapter<String>> jobs = new ArrayList<GridJobAdapter<String>>(words.length); for (String word : words) { // Every job gets its own word as an argument. jobs.add(new GridJobAdapter<String>(word) { /** * Simply executes {@link GridifyHelloWorldTaskExample#sayIt(String)} method * with passed in argument. */ public Serializable execute() throws GridException { // Execute gridified method. // Note that since we are calling this method from within the grid job // AOP-based grid enabling will not cross-cut it and method will just // execute normally. return GridifyHelloWorldTaskExample.sayIt(getArgument()); } }); } return jobs; } /** * Sums up all characters from all jobs and returns a * total number of characters in the initial phrase. * * @param results Job results. * @return Number of characters for the 'phrase' passed into * {@link GridifyHelloWorldTaskExample#sayIt(String)} method. * @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 phrase 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; } }
