Basic Gridify Example
This example contains HelloWorld example that is using Gridify ![]()
String 'Hello World' is passed as an argument to GridifyHelloWorldBasicExample.sayIt(String) method. Since this method is annotated with @Gridify ![]()
![]()
Package:
org.gridgain.examples.helloworld.gridify.basic
There is one class 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.
GridifyHelloWorldBasicExample.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.
In order to grid-enable helloWorld method, we add @Gridify ![]()
@Gridify private 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(); }
Source Code (edited)
package org.gridgain.examples.helloworld.gridify.basic; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; import org.gridgain.grid.gridify.aop.spring.*; public final class GridifyHelloWorldBasicExample { @Gridify private 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(); } 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 node. int phraseLen = sayIt("Hello World"); System.out.println(">>>"); System.out.println(">>> Finished executing Gridify \"Hello World\" example with all defaults."); System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'."); System.out.println(">>> You should see print out of 'Hello World' on one of the remote nodes."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
