Explicit Task Deployment Example
This example demonstrates explicit deployment, execution and undeployment of a task on GridGain grid. A sample task GridDeploymentExampleTask is using GridTaskName ![]()
(will be available with 1.6.2 release)
Package:
org.gridgain.examples.deployment.direct
There are two classes implemented for this example:
Running Grid Node
This example will need one remote node to be running. Note that you don't need another machine for it - you can start remote node on the same machine you are running example on.
To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.
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. Define task name.
static final String TASK_NAME = "GridDeploymentExampleTask";
4. Deploy task.
grid.deployTask(GridDeploymentExampleTask.class);
5. Execute task.
grid.execute(TASK_NAME, null).get();
6. Undeploy task.
grid.undeployTask(TASK_NAME);
Full Source Code
package org.gridgain.examples.deployment.direct; import org.gridgain.grid.*; /** * 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 <tt>Grid.execute(...)</tt> 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. * <p> * 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. */ public final class GridDeploymentExample { /** Name of the deployed task. */ static final String TASK_NAME = "GridDeploymentExampleTask"; /** * Ensure singleton. */ private GridDeploymentExample() { // No-op. } /** * Deploy, execute and undeploy <tt>HelloWorld</tt> example on the grid. * * @param args Command line arguments, none required but if provided * first one should point to the Spring 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 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. 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
1. Import GridGain classes.
import org.gridgain.grid.*; import org.gridgain.grid.logger.*; import org.gridgain.grid.resources.*;
2. Add GridTaskName Annotation.
Method assigned custom name with @GridTaskName ![]()
@GridTaskName(GridDeploymentExample.TASK_NAME)
2. Split Logic.
This is a grid task implementation that is responsible map and reduce logic. Note that this task implements GridTask ![]()
Full Source Code
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.*; /** * 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 {@link Grid#deployTask(Class)} call and * then use {@link Grid#execute(String, Object)} method passing your * task name as first parameter. * <p> * Note that 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. */ @GridTaskName(GridDeploymentExample.TASK_NAME) public class GridDeploymentExampleTask extends GridTaskAdapter<String> { /** Grid logger. */ @GridLoggerResource private GridLogger log = null; /** Instance of grid. */ @GridInstanceResource private Grid grid = null; /** * {@inheritDoc} */ public Map<? extends GridJob, GridNode> map(List<GridNode> subgrid, String arg) throws GridException { GridJob job = new GridJobAdapter() { /* * Simply prints the job's argument. */ 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; } }; Map<GridJob, GridNode> jobs = new HashMap<GridJob, GridNode>(1); // Use only local node for this example. jobs.put(job, grid.getLocalNode()); return jobs; } /** * {@inheritDoc} */ public Serializable reduce(List<GridJobResult> results) throws GridException { // Nothing to reduce. return null; }
