This example does the following:
- Creates a list of Callable commands to scale certain images with different scale parameters.
- Invokes these commands via GridGain grid Executor and prints scaled images data.
Starting Remote Nodes
To try this example you should start remote grid instances. You can start as many as you like by executing the following script (which simply invokes GridJbossCacheExampleNodeLoader.java): GRIDGAIN_HOME/examples/jbosscache/start-node.bat|sh
Once remote instances are started, you can execute this example from Eclipse, Idea, or NetBeans (or any other IDE) by simply hitting run button. You will witness that all nodes discover each other and will participate in task execution.
Full Source Code
GridExecutorExample.java
package org.gridgain.examples.executor; import java.util.concurrent.*; import java.util.*; import org.gridgain.grid.*; public final class GridExecutorExample { /** * Ensure singleton. */ private GridExecutorExample() { // No-op. } /** * Execute <tt>Executor</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 Exception If example execution failed. */ public static void main(String[] args) throws Exception { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { Grid grid = GridFactory.getGrid(); ExecutorService exec = grid.newGridExecutorService(); boolean archive = true; String rsrc1 = "org/gridgain/examples/executor/example1.jpg"; Future<GridExecutorImage> future = exec.submit(new GridExecutorImageScaleCommand(0.9d, rsrc1, archive)); GridExecutorImage img = future.get(); System.out.println(); System.out.println("Received execution result for first submitted command: " + img); String rsrc2 = "org/gridgain/examples/executor/example2.gif"; String rsrc3 = "org/gridgain/examples/executor/example3.png"; List<Callable<GridExecutorImage>> cmds= new ArrayList<Callable<GridExecutorImage>>(2); cmds.add(new GridExecutorImageScaleCommand(1.1d, rsrc2, archive)); cmds.add(new GridExecutorImageScaleCommand(1.5d, rsrc3, archive)); List<Future<GridExecutorImage>> futures = exec.invokeAll(cmds); for (Future<GridExecutorImage> imgFuture : futures) { GridExecutorImage convImg = imgFuture.get(); System.out.println(); System.out.println("Received execution result for commands batch: " + convImg); } System.out.println(">>>"); System.out.println(">>> Finished executing Grid Executor example."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); exec.shutdown(); } finally { GridFactory.stop(true); } } }
