Grid Task Deployment From Gar File Example.
Package:
org.gridgain.examples.deployment.gar
This example demonstrates a simple use of GridGain grid with GAR file.
The main purpose of this example is to demonstrate how grid task can be packaged into GAR file, how GAR ant task can be used and how various resources (such as Spring XML and properties files) can be accessed from within GAR deployment.
Message property key "HELLO.MSG" is passed as an argument for execution of GridGarHelloWorldTask.java task which is loaded from GAR file. GridGarHelloWorldTask.java gets GridGarHelloWorldBean.java bean from GAR/lib/depend.jar via Spring. Message resource example.properties also loaded from imported GAR/lib/depend.jar. 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.
|
Both GridGarHelloWorldTask.java and GridGarHelloWorldBean.java classes are placed outside of this project for example purpose. They are assumed to be built into GAR file by running build.xml Ant script supplied with this example. |
Grid task GridGarHelloWorldTask.java handles actual splitting into sub-jobs, remote execution, and result aggregation
Before running example, make the following steps:
- Create GAR file (helloworld.gar) with Ant script. Go in folder GRIDGAIN_HOME/examples/gar/build and run ant in command line.
- Copy GRIDGAIN_HOME/examples/gar/deploy/helloworld.gar in folder GRIDGAIN_HOME/work/deployment/file/
- You should run the following sample with Spring XML configuration file shipped with GridGain and located GRIDGAIN_HOME/examples/config/spring-gar.xml.
- You should pass a path to Spring XML configuration file as 1st command line argument into this example.
|
spring-gar.xml starts GridGain with GridUriDeploymentSpi |
There are three classes implemented for this example:
Running Grid Node
This example will need not any remote nodes.
GridGarHelloWorldExample.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.
4. Add Grid Task Execution.
grid.execute("GridGarHelloWorldTask", "HELLOWORLD.MSG").get();
Note that method grid.execute(...) returns GridTaskFuture ![]()
![]()
Full Source Code
package org.gridgain.examples.deployment.gar; import org.gridgain.grid.*; import org.gridgain.grid.spi.deployment.uri.*; public final class GridGarHelloWorldExample { /** * Ensure singleton. */ private GridGarHelloWorldExample() { // No-op. } /** * Execute <tt>HelloWorld</tt> example 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(); // Execute Hello World task from GAR file. grid.execute("GridGarHelloWorldTask", "HELLOWORLD.MSG").get(); System.out.println(">>>"); System.out.println(">>> Finished executing Grid \"Hello World\" example with custom task."); System.out.println(">>> You should see print out of 'Hello' on one node and 'World' on another node."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
GridGarHelloWorldTask.java
This class defines grid task for this example. Grid task is responsible for splitting the task into jobs. This particular implementation splits given string into individual words and creates grid jobs for each word. Task class in that example should be placed in GAR file.
Full Source Code
package org.gridgain.examples.gar; import org.gridgain.grid.*; import org.springframework.beans.factory.xml.*; import org.springframework.core.io.*; import java.util.*; import java.io.*; @GridTaskName("GridGarHelloWorldTask") public class GridGarHelloWorldTask extends GridTaskSplitAdapter<String, Object> { /** * {@inheritDoc} */ @Override public Collection<? extends GridJob> split(int gridSize, String arg) throws GridException { // Create Spring context. XmlBeanFactory factory = new XmlBeanFactory( new ClassPathResource("org/gridgain/examples/gar/gar-spring-bean.xml", getClass().getClassLoader())); factory.setBeanClassLoader(getClass().getClassLoader()); // Load imported bean from GAR/lib folder. GridGarHelloWorldBean bean = (GridGarHelloWorldBean)factory.getBean("example.bean"); // Split the passed in phrase into multiple words separated by spaces. List<String> words = Arrays.asList((bean.getMessage(arg)).split(" ")); List<GridJob> jobs = new ArrayList<GridJob>(words.size()); for (String word : words) { // Every job gets its own word as an argument. jobs.add(new GridJobAdapter<String>(word) { /* * Simply prints the job's argument. */ public Serializable execute() { System.out.println(">>>"); System.out.println(">>> Printing '" + getArgument() + "' on this node from grid job."); System.out.println(">>>"); // This job does not return any result. return null; } }); } return jobs; } /** * {@inheritDoc} */ public Object reduce(List<GridJobResult> results) throws GridException { // HelloWorld example has not return value, hence there is nothing // to reduce. return null; } }
GridGarHelloWorldBean.java
Imported class which should be placed in JAR file in GAR/lib folder. Loads message resource file via class loader.
Full Source Code
package org.gridgain.examples.gar; import java.io.*; import java.util.*; import org.gridgain.grid.util.*; public class GridGarHelloWorldBean { /** */ public static final String RESOURCE = "org/gridgain/examples/gar/gar-example.properties"; /** * Gets keyed message. * * @param key Message key. * @return Keyed message. */ public String getMessage(String key) { InputStream in = null; try { in = getClass().getClassLoader().getResourceAsStream(RESOURCE); Properties props = new Properties(); props.load(in); return props.getProperty(key); } catch (IOException e) { e.printStackTrace(); } finally{ GridUtils.close(in, null); } return null; } }
