Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > HelloWorld - Gridify With Interceptor
HelloWorld - Gridify With Interceptor
Added by usr1, last edited by morpheus on Mar 07, 2008  (view change)
Labels: 
(None)


Gridify With Interceptor

This example demonstrates how we can use GridifyInterceptor Javadoc to dynamically decide whether a method should be grid-enabled or not. In this example we grid-enable helloWorld method only if heap utilization is above 80%.

Package:
org.gridgain.examples.helloworld.gridify.interceptor

There are two 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.

GridifyHelloWorldInterceptorExample.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.

Note that we pass our custom GridifyHelloWorldIncerceptor as a parameter to Gridify Javadoc annotation.

@Gridify(interceptor = GridifyHelloWorldInterceptor.class)
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

GridifyHelloWorldInterceptorExample.java
package org.gridgain.examples.helloworld.gridify.interceptor;

import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;

public final class GridifyHelloWorldInterceptorExample {
    /**
     * Ensure singleton.
     */
    private GridifyHelloWorldInterceptorExample() {
        // No-op.
    }

    /**
     * Method grid-enabled with {@link Gridify} annotation. Simply prints
     * out the argument passed in.
     * <p>
     * Note that the custom interceptor is used to ensure that the method will
     * be grid-enabled only if used heap memory is above 80% of maximum heap
     * memory available.
     *
     * @param arg String to print.
     * @return Number of characters in the phrase.
     */
    @Gridify(interceptor = GridifyHelloWorldInterceptor.class)
    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 XML configuration file.
     * @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 node.
            int phraseLen = sayIt("Hello World");

            System.out.println(">>>");
            System.out.println(">>> Finished executing Gridify \"Hello World\" example with custom insterceptor.");
            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 node.");
            System.out.println(">>> Check all nodes for output (this node is also part of the grid).");
            System.out.println(">>>");
        }
        finally {
            GridFactory.stop(true);
        }
    }
}

GridifyHelloWorldInterceptor.java

1. Full Source Code

Note that implementation uses standard JDK management MBean to get an instant snapshot of memory heap usage.

GridifyHelloWorldInterceptor.java
package org.gridgain.examples.helloworld.gridify.interceptor;

import java.lang.management.*;
import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;

public class GridifyHelloWorldInterceptor implements GridifyInterceptor {
    /**
     * Grid-enables method only if heap utilized over 80%. Otherwise, method
     * method will proceed with local execution without calling grid at all.
     * 
     * @param gridify {@inheritDoc}
     * @param arg {@inheritDoc}
     * @return {@inheritDoc}
     * @throws GridException {@inheritDoc}
     */
    public boolean isGridify(Gridify gridify, GridifyArgument arg) throws GridException {
        MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

        // If heap memory for the local node is above 80% of maximum heap
        // memory available, return true which means that method will
        // be grid-enabled. Otherwise, return false, which means that
        // method will execute locally without grid.
        return heap.getUsed() > 0.8 * heap.getMax();
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators