Main bootstrap class for Gridify ![]()
![]()
Refer to GridifyPrimeTask.java for documentation on how prime numbers are calculated.
Starting Remote Nodes.
To try this example you should (but don't have to) start remote grid instances. You can start as many as you like by executing the following script:
GRIDGAIN_HOME/bin/gridgain.bat|sh
Once remote instances are started, you can execute this example from Eclipse, Idea, or NetBeans (or any other IDE) by simply hitting run button. You will witness that all nodes discover each other and all of the nodes will participate in task execution (check node output).
Note that when running this example on a multi-core box, simply starting additional grid node on the same box will speed up prime number calculation by a factor of 2.
XML Configuration.
If no specific configuration is provided, GridGain will start with all defaults. For information about GridGain default configuration refer to GridFactory ![]()
GRIDGAIN_HOME/bin/gridgain.bat|sh examples/config/specific-config-file.xml
GridGain examples come with multiple configuration files you can try. All configuration files are located under GRIDGAIN_HOME/examples/config folder. You are free to try any of these configurations, but whenever using 3rd party configurations, such as JBoss JMS, ActiveMQ JMS, Sun MQ JMS, or GigaSpaces, make sure to download these respective products and include all the necessary libraries into classpath at node startup. All these libraries are already specified in commented format in GRIDGAIN_HOME/bin/setenv.bat|sh files which get executed automatically by GridGain startup scripts. You can simply uncomment the necessary classpath portions as you need.
AOP Configuration.
In order for this example to execute on the grid, any of the following AOP configurations must be provided (only on the task initiating node).
Jboss AOP.
The following configuration needs to be applied to enable JBoss byte code weaving. Note that GridGain is not shipped with JBoss and necessary libraries will have to be downloaded separately (they come standard if you have JBoss installed already):
The following JVM configuration must be present:
- -javaagent:path to jboss-aop-jdk50-4.x.x.jar
- -Djboss.aop.class.path=path to gridgain.jar
- -Djboss.aop.exclude=org,com -Djboss.aop.include=org.gridgain.examples
The following JARs should be in a classpath:
- javassist-3.x.x.jar
- jboss-aop-jdk50-4.x.x.jar
- jboss-aspect-library-jdk50-4.x.x.jar
- jboss-common-4.x.x.jar
- trove-1.0.2.jar
AspectJ AOP
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.
Spring AOP
Spring AOP framework is based on dynamic proxy implementation and doesn't require any specific runtime parameters for online weaving. All weaving is on-demand and should be performed by calling method GridifySpringEnhancer.enhance(Object) for the object that has method with Gridify ![]()
|
This method of weaving is rather inconvenient and AspectJ or JBoss AOP are more recommended. Spring AOP can be used in situation when code augmentation is undesired and cannot be used. It also allows for very fine grained control of what gets weaved. |
|
This example as is cannot be used with Spring AOP. To see an example of grid-enabling with Spring AOP refer to GridifySpringHelloWorldExample.java example. |
Full Source Code
package org.gridgain.examples.primenumbers.gridify; import java.util.*; import org.gridgain.examples.helloworld.gridify.spring.*; import org.gridgain.examples.primenumbers.*; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; import org.gridgain.grid.gridify.aop.spring.*; public final class GridifyPrimeExample { /** * Enforces singleton. */ private GridifyPrimeExample() { // No-op. } /** * Starts up grid and checks all provided values for prime. * * @param args Command line arguments, none required but if provided * first one should point to the Spring XML 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 { Grid grid = null; // Start grid. if (args.length == 0) { grid = GridFactory.start(); } else { grid = GridFactory.start(args[0]); } // Values we want to check for prime. long[] checkVals = { 32452841, 32452843, 32452847, 32452849, 236887699, 217645199 }; System.out.println(">>>"); System.out.println(">>> Starting to check the following numbers for primes: " + Arrays.toString(checkVals)); try { long start = System.currentTimeMillis(); for (long checkVal : checkVals) { // This method will be executed on the Grid as it is // annotated with @Gridify annotation. Long divisor = GridPrimeChecker.checkPrime(checkVal, 2, checkVal); // If divisor is null, then the number is prime. if (divisor == null) { System.out.println("Value '" + checkVal + "'is a prime number."); } else { System.out.println("Value '" + checkVal + "' is divisible by '" + divisor + '\''); } } long totalTime = System.currentTimeMillis() - start; System.out.println(">>> Total time to calculate all primes (milliseconds): " + totalTime); System.out.println(">>>"); } finally { // Stop grid. GridFactory.stop(grid.getName(), true); } } }
