Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Example Configuration And Setup > GridHelloWorldExample.java
GridHelloWorldExample.java
Added by architect, last edited by architect on Apr 12, 2007  (view change)
Labels: 
(None)


Step-By-Step

1. Import GridGain classes.

import org.gridgain.grid.*;

2. Define Grid Task ID.

public static final String TASK_TYPE_ID = "GridHelloWordTask";

You can use a constant or use direct String value.

3. Add Grid Start and Stop.

GridFactory.start();
        
try {
    ...
}
finally {
    GridFactory.stop(true);
}

finally clause allows for graceful grid shutdown in case of the exceptions.

4. Add Grid Task Local Deployment.

grid.deployTask(new GridTaskAdapter(TASK_TYPE_ID, GridHelloWorldJob.class.getName()));

Notice that we use an adapter for GridTask that takes task type ID and class name of the root job.

5. Add Grid Task Execution.

grid.execute(TASK_TYPE_ID, "Hello, World!");

Full Source Code

You can copy and paste this source code into your Java editor:

/*
 * 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.grid;

import org.apache.log4j.*;
import org.gridgain.grid.*;

/**
 * This example demonstrates a simple use of GridGain grid.
 * <p>
 * String "Hello, World!" is passed as an argument for execution
 * of <tt>HelloWorld</tt> task. 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 printout
 * "World!". If there is only one node participating, then it will
 * print out both words.
 * <p>
 * Grid job {@link GridHelloWorldJob} handles actual splitting
 * into sub-jobs, remote execution, and result aggregation
 * (see {@link GridJob}).
 *
 * @author 2005-2007 Copyright (C) GridGain Systems. All Rights Reserved.
 * @version 1.0.0-07022007
 */
public final class GridHelloWorldExample {
    /** Task type ID (value is <tt>GridHelloWorldTask</tt>). */
    public static final String TASK_TYPE_ID = "GridHelloWordTask";

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

    /**
     * Ensure singleton.
     */
    private GridHelloWorldExample() {
        // No-op.
    }
    
    /**
     * Execute <tt>HelloWorld</tt> example on the grid.
     * 
     * @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.
            grid.deployTask(new GridTaskAdapter(TASK_TYPE_ID, GridHelloWorldJob.class.getName()));
            
            // Execute Hello World task.
            grid.execute(TASK_TYPE_ID, "Hello, World!");
            
            if (log.isInfoEnabled() == true) {
                log.info(">>> Executed \"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