Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Example Configuration And Setup > GridifyHelloWorldTaskExample.java
GridifyHelloWorldTaskExample.java
Added by architect, last edited by architect on Feb 08, 2007
Labels: 
(None)


/*
 * LICENSE AGREEMENT
 * 
 * GRIDGAIN 1.0 - GRID COMPUTING FOR JAVA.
 * COPYRIGHT (C) 2005-2007 GRIDGAIN SYSTEMS. ALL RIGHTS RESERVED.
 * 
 * THIS IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
 * MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
 * LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
 * VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER 
 * VERSION.
 * 
 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  SEE THE 
 * GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
 * 
 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
 * LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE 
 * SOFTWARE FOUNDATION, INC., 51 FRANKLIN ST, FIFTH FLOOR, BOSTON, MA  
 * 02110-1301 USA
 */


package org.gridgain.examples.helloworld.gridify.task;

import org.apache.log4j.*;
import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;
import org.gridgain.grid.gridify.aop.spring.*;

/**
 * This example demonstrates a simple use of GridGain grid with
 * <tt>Gridify</tt> annotation and cusomt grid task.
 * <p>
 * String "Hello, World!" is passed as an argument to
 * {@link #helloWorld(String)} method. Since this method is annotated with
 * <tt>Gridify</tt> annotation it is automatically grid-enabled. As an outcome,
 * two participating nodes will print out a single word from "Hello, World!"
 * string. One node will print out "Hello, " and the other will print out
 * "World!". If there is only one node participating, then it will
 * print out both words.
 * <p>
 * Grid job {@link GridifyHelloWorldJob} handles actual splitting
 * into sub-jobs, remote execution, and result aggregation (see {@link GridJob}).
 * <p>
 * <h1 class="header">Jboss AOP</h1>
 * The following configuration needs to be applied to enable JBoss byte code
 * weaving.
 * <ul>
 * <li>
 *      The following JVM configuration must be present:
 *      <ul>
 *      <li><tt>-javaagent:[GRIDGAIN_HOME]/libs/jboss-aop-jdk50-4.0.4.jar</tt></li>
 *      <li><tt>-Djboss.aop.class.path=[path to gridgain.jar]</tt></li>
 *      <li><tt>-Djboss.aop.exclude=org,com -Djboss.aop.include=org.gridgain.examples</tt></li>
 *      </ul>
 * </li>
 * <li>
 *      The following JARs should be in a classpath:
 *      <ul>
 *      <li><tt>[GRIDGAIN_HOME]/libs/javassist-4.0.4.jar</tt></li>
 *      <li><tt>[GRIDGAIN_HOME]/libs/jboss-aop-jdk50-4.0.4.jar</tt></li>
 *      <li><tt>[GRIDGAIN_HOME]/libs/jboss-aspect-library-jdk50-4.0.4.jar</tt></li>
 *      <li><tt>[GRIDGAIN_HOME]/libs/jboss-common-4.0.4.jar</tt></li>
 *      <li><tt>[GRIDGAIN_HOME]/libs/trove-1.0.2.jar</tt></li>
 *      </ul>
 *      Note that these JARs are usually in classpath by default when using GridGain.
 * </li>
 * </ul>
 * <p>
 * <h1 class="header">Spring AOP</h1>
 * 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 {@link GridifySpringEnhancer#enhance(Object)} for the object
 * that has method with {@link Gridify} annotation.
 * <p>
 * Note that this method of weaving is rather inconvenient and AspectJ or JbossAOP is
 * recommended over it. 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.
 * <p>
 * <h1 class="header">AspectJ AOP</h1>
 * The following configuration needs to be applied to enable AspectJ byte code
 * weaving.
 * <ul>
 * <li>
 *      JVM configuration should include:
 *      <tt>-javaagent:[GRIDGAIN_HOME]/libs/aspectjweaver-1.5.3.jar</tt>
 * </li>
 * <li>
 *      Classpath should contain the <tt>[GRIDGAIN_HOME]/config/aop/aspectj</tt> folder.
 * </li>
 * </ul>
 *
 * @author 2005-2007 Copyright (C) GridGain Systems. All Rights Reserved.
 * @version 1.0.0-07022007
 */
public final class GridifyHelloWorldTaskExample {
    /** Task type ID used for this example. */
    public static final String TASK_TYPE_ID = "GridifyHelloWorldExampleTask";

    /** New line separator. */
    private static final String NL = System.getProperty("line.separator");

    /** Log4j logger. */
    private static final Logger log = Logger.getLogger(GridifyHelloWorldTaskExample.class);

    /**
     * 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(taskTypeId = TASK_TYPE_ID, timeout = 3000)
    public static void helloWorld(String arg) {
        log.info(NL +
            ">>>" + NL +
            ">>> Executing '" + arg + "' on this node." + NL +
            ">>>");
    }

    /**
     * Execute <tt>HelloWorld</tt> example grid-enabled with
     * <tt>Gridify</tt> annotation.
     *
     * @param args Command line arguments, none required.
     * @throws GridException If example execution failed.
     */
    public static void main(String[] args) throws GridException {
        GridFactory.start();

        try {
            Grid grid = GridFactory.getGrid();

            // Explicitly deploy task prior to executing.
            // Since peer class loading is enabled, all
            // participating nodes will deploy this task
            // from this node automatically.
            // Since we use default GridClassNameJobResolver,
            // we pass in GridifyHelloWorldJob full class name as
            // root job type ID for the task.
            grid.deployTask(new GridTaskAdapter(TASK_TYPE_ID, GridifyHelloWorldJob.class.getName()));

            // This method will be executed on a remote grid node.
            helloWorld("Hello, World!");

            if (log.isInfoEnabled() == true) {
                log.info(">>> Executed Gridify \"Hello, World!\" <<<");
            }
        }
        finally {
            GridFactory.stop(true);
        }
    }
}

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