Dashboard > GridGain User Guide > Code Snippet 3
Code Snippet 3
Added by Nikita Ivanov, last edited by Nikita Ivanov on Feb 27, 2007  (view change)
Labels: 
(None)


HelloWorldExample.java

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

/**
 * Hello world example.
 */
public class HelloWorldExample {
    /** Task type ID. */
    public static final String TASK_TYPE_ID = "HelloWorldExampleTask";

    /**
     * Main entry point.
     *
     * @param args Command line arguments.
     * @throws GridException Thrown in case of any grid error.
     */
    public static void main(String[] args) throws GridException {
        GridFactory.start();

        try {
            // Deploy grid task manually.
            GridFactory.getGrid().deployTask(new GridTaskAdapter(TASK_TYPE_ID, HelloWorldJob.class.getName()));

            sayIt(args.length > 0 ? args[0] : "hello world!");
        }
        finally {
            GridFactory.stop(true);
        }
    }

    /**
     * Prints given <tt>text</tt> to standard output.
     *
     * @param text Text to print.
     */
    @Gridify(taskTypeId = TASK_TYPE_ID) // Mark this method for grid-enabling.
    public static void sayIt(String text) {
        System.out.println(text);
    }
}

HelloWorldJob.java

import org.gridgain.grid.gridify.*;
import org.gridgain.grid.*;
import java.io.*;
import java.util.*;

/**
 * Hello world job.
 */
public class HelloWorldJob extends GridifyJobAdapter {
    /** Injected job context. */
    @GridJobContextResource
    private GridJobContext jobCtx = null;

    /**
     * @see GridJob#split(Serializable,Collection)
     */
    public Collection<GridJobReference> split(Serializable arg, Collection<GridNode> subGrid) {
        GridifyArgument gridifyArg = (GridifyArgument)arg;

        // Only split if this is a root job and there are other available nodes.
        if (jobCtx.isRootJob() == true && subGrid.isEmpty() == false) {
            // Split the passed in phrase into multiple words.
            String[] words = ((String)gridifyArg.getMethodParameters()[0]).split(" ");

            List<GridJobReference> refs = new ArrayList<GridJobReference>(words.length);

            Iterator<GridNode> nodes = subGrid.iterator();

            // Try to assign every word to a different node
            // for execution.
            for (String word : words) {
                // Execute word on remote node.
                refs.add(new GridJobReferenceAdapter(jobCtx.getJobTypeId(), new GridifyArgumentAdapter(gridifyArg,
                    word), nodes.next()));

                // If we run out of available nodes, then recycle the
                // iterator to reuse nodes.
                if (nodes.hasNext() == false) {
                    nodes = subGrid.iterator();
                }
            }

            // Trigger remote execution.
            return refs;
        }

        // Trigger local execution.
        return null;
    }

    /**
     * @see GridJob#aggregate(List)
     */
    public Serializable aggregate(List<GridJobRemoteResult> results) {
        // Since this is a one-way job there's nothing to aggregate
        // since there will be no results.
        return null;
    }
}

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