Description
Creates ExecutorService which will execute all submitted Callable and Runnable tasks on the grid. User may run Callable and Runnable tasks just like normally with java.util.ExecutorService, but these tasks must implement Serializable interface.
The execution will happen either locally or remotely, depending on configuration of Load Balancing SPI and Topology SPI. Distributed ExecutorService delegates commands execution to already started Grid instance. Every submitted task will be serialized and transfered to any node in grid.
Here is an example of an ExecutorService to show how it can be used.
public static void main(String[] args) throws GridException { GridFactory.start(); try { Grid grid = GridFactory.getGrid(); ExecutorService srvc = grid.newGridExecutorService(); List<Callable<String>> cmds = new ArrayList<Callable<String>>(2); String testVal1 = "test-value-1"; String testVal2 = "test-value-2"; cmds.add(new FooCallable<String>(testVal1)); cmds.add(new FooCallable<String>(testVal2)); List<Future<String>> futures = srvc.invokeAll(cmds); // Wait for command completion. String res1 = futures.get(0).get(); String res2 = futures.get(1).get(); // Print out results. System.out.println("Results [res1=" + res1 + ", res2=" + res2 + ']'); } finally { GridFactory.stop(true); }
where simple FooCallable
private static class FooCallable<T> implements Callable<T>, Serializable { /** */ private T data = null; /** * @param data Some data. */ FooCallable(T data) { this.data = data; } /** * {@inheritDoc} */ public T call() throws Exception { System.out.println("Message: " + data); return data; } }
