Dashboard > GridGain User Guide > Table Of Contents > Developers Guide > Executing Grid Task
Executing Grid Task
Added by architect, last edited by morpheus on Apr 01, 2008  (view change)
Labels: 
(None)


Grid-enabling is a process of making a piece of Java code to execute on the grid. In GridGain, there are two ways to do grid-enabling:

Direct Execution vs. Annotation-Based AOP

There is no better or worse between these two methods. They both have their areas of applicability. When creating grid task you basically have the same programming and development model as in JEE: you create a component, deploy it and execute it. With annotation-based grid-enabling you have an extra option of transparently attaching grid-enabling logic to existing code without modifying it (except for additional annotation).

API-Based Grid Task Execution

This method allows to grid-enable any arbitrary Java code. You have a full control on split and aggregate logic and all other aspects of grid task execution. Here is an example of direct grid task execution:

public static void main(String[] args) throws GridException {
    GridFactory.start();

    try {
        Grid grid = GridFactory.getGrid();

        // Execute task.
        GridTaskFuture<String> future = grid.execute(FooBarTask.class, "Argument");

        // Wait for task completion.
        String result = fugure.get();

        // Print out task result.
        System.out.println("Task result: " + result);
    }
    finally {
        GridFactory.stop(true);
    }
}

Refer to HelloWorld - Basic Grid Task for more coding examples.

Annotate Existing Method With Gridify Annotation

The only difference of this method vs. directly executing grid task is that you can annotate a regular Java method and it will become grid-enabled. Using this technique you can still have custom grid task that will handle annotation-based grid-enabling (including split & aggregate logic or passing state to remote jobs) but you will be limited to the boundaries of the method you are grid-enabling. Here is an example of such usage:

@Gridify(taskClass = FooBarTask.class, timeout = 3000)
public void sayIt(String arg) {
    // Some business logic.
}

Refer to HelloWorld - Gridify With Custom Task for more coding examples.

For information on how to configure AOP, refer to AOP Configuration section.

Serializable State

Note that when using @Gridify Javadoc annotation on non-static methods without specifying explicit grid task, the state of the whole instance will be serialized and sent out to remote node. Therefore the class must implement java.io.Serializable interface. If you cannot make the class Serializable, then you must implement custom grid task which will take care of proper state initialization (see HelloWorld - Gridify With State example). In either case, GridGain must be able to serialize the state passed to remote node.

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