Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Grid Task Deployment - Direct
Grid Task Deployment - Direct
Added by ghost, last edited by morpheus on Feb 13, 2008  (view change)
Labels: 
(None)


Grid Direct Task Deployment Example.

Package:
org.gridgain.examples.deployment.direct

This example demonstrates how to explicitly deploy a task. Note that it is very rare when you would need such functionality as tasks are auto-deployed on demand first time you execute them. So in most cases you would just call any of the Grid.execute(...) methods directly. However, sometimes a task is not in local class path, so you may not even know the code it will execute, but you still need to execute it. For example, you have two independent components in the system, and one loads the task classes from some external source and deploys it; then another component can execute it just knowing the name of the task.
Also note that for simplicity of the example, the task we execute is in system classpath, so even in this case the deployment step is unnecessary.

There are two classes implemented for this example:

Running Grid Node

This example will need not any remote nodes.

GridDeploymentExample.java

1. Import GridGain classes.

import org.gridgain.grid.*;

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. Directly Deploy Task.

grid.deployTask(GridDeploymentExampleTask.class);

4. Add Grid Task Execution.

grid.execute(TASK_NAME, null).get();

Note that method grid.execute(...) returns GridTaskFuture Javadoc . Method get() on GridTaskFuture Javadoc allows to block until task execution is completed. This is a standard approach borrowed from java.util.concurrent.Future interface.

5. Directly Undeploy Task By Name.

grid.undeployTask(TASK_NAME);

Full Source Code

GridDeploymentExample.java
package org.gridgain.examples.deployment.direct;

import org.gridgain.grid.*;

public final class GridDeploymentExample {
    /** Name of the deployed task. */
    static final String TASK_NAME = "GridDeploymentExampleTask";

    /**
     * Ensure singleton.
     */
    private GridDeploymentExample() {
        // No-op.
    }

    /**
     * Deploys, executes and undeploys example task on the grid.
     *
     * @param args Command line arguments, none required but if provided
     *      first one should point to the Spring XML configuration file. See
     *      <tt>"examples/config/"</tt> for configuration file examples.
     * @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 {
            Grid grid = GridFactory.getGrid();


            // This task will be deployed on local node and then peer-loaded
            // onto remote nodes on demand. For this example this task is
            // available on the classpath, however in real life that may not
            // always be the case. In those cases you should use explicit
            // 'Grid.deployTask(Class)}' call and then use 'Grid.execute(String, Object)'
            // method passing your task name as first parameter.
            grid.deployTask(GridDeploymentExampleTask.class);

            for (Class<? extends GridTask<?, ?>> taskCls : grid.getLocalTasks().values()) {
                System.out.println(">>> Found locally deployed task: " + taskCls);
            }

            // Execute the task passing name as a parameter. The system will find
            // the deployed task by its name and execute it.
            grid.execute(TASK_NAME, null).get();

            // Undeploy task
            grid.undeployTask(TASK_NAME);

            System.out.println(">>>");
            System.out.println(">>> Finished executing Grid Direct Deployment Example.");
            System.out.println(">>> Check participating nodes output.");
            System.out.println(">>>");
        }
        finally {
            GridFactory.stop(true);
        }
    }
}

GridDeploymentExampleTask.java

Example task used to demonstrate direct task deployment through API.For this example this task as available on the classpath, however in real life that may not always be the case. In those cases * you should use explicit
Grid#deployTask(Class) call and then use Grid#execute(String, Object) method passing your task name as first parameter.

This task specifies explicit task name. Task name is optional and is added here for demonstration purpose. If not provided, it will default to the task class name.

Full Source Code

GridDeploymentExampleTask.java
package org.gridgain.examples.deployment.direct;

import java.io.*;
import java.util.*;
import org.gridgain.grid.*;
import org.gridgain.grid.logger.*;
import org.gridgain.grid.resources.*;

@GridTaskName(GridDeploymentExample.TASK_NAME)
public class GridDeploymentExampleTask extends GridTaskSplitAdapter<String, Object> {
    /** Grid logger. */
    @GridLoggerResource
    private GridLogger log = null;

    /**
     * {@inheritDoc}
     */
    @Override
    protected Collection<? extends GridJob> split(int gridSize, String arg) throws GridException {
        List<GridJob> jobs = new ArrayList<GridJob>(gridSize);

        for (int i = 0; i < gridSize; i++) {
            jobs.add(new GridJobAdapter<Serializable>() {
                /**
                 * {@inheritDoc}
                 */
                public Serializable execute() {
                    if (log.isInfoEnabled() == true) {
                        log.info(">>> Exectuting deployment example job on this node.");
                    }

                    // This job does not return any result.
                    return null;
                }
            });
        }

        return jobs;
    }

    /**
     * {@inheritDoc}
     */
    public Object reduce(List<GridJobResult> results) throws GridException {
        // Nothing to reduce.
        return null;
    }
}

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