Grid Spring Bean Example
Package:
org.gridgain.examples.helloworld.spring
This example contains HelloWorld example that shows two new features of the GridGain. One of them is a Grid injection from Spring file and another one is the execution with Grid executor service. Grid bean will be obtained from Spring application context and then "Hello world" Callable will be executed with Grid executor service. Last will simply print out "Hello world" phrase on remote node.
There is only one class implemented for this example and one simple Spring configuration file:
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 acknowledgement.
GridSpringBeanHelloWorldExample.java
1. Import GridGain classes.
import org.gridgain.grid.*;
2. Obtain Grid bean from Spring file.
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("org/gridgain/examples/helloworld/spring/spring.xml"); // Get Grid from Spring. Grid grid = (Grid)ctx.getBean("mySpringBean"); ... // Destroy Spring context. ctx.destroy();
3. Add Grid Callable Execution.
ExecutorService exec = grid.newGridExecutorService();
Future<String> res = exec.submit(new GridHelloWorldCallable());
// Wait for the task to complete.
res.get();
Source Code (edited)
package org.gridgain.examples.helloworld.spring; import java.io.*; import java.util.concurrent.*; import org.gridgain.grid.*; import org.springframework.context.support.*; public final class GridSpringBeanHelloWorldExample { private GridSpringBeanHelloWorldExample() { // No-op. } public static void main(String[] args) throws Exception { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("org/gridgain/examples/helloworld/spring/spring.xml"); // Get Grid from Spring. Grid grid = (Grid)ctx.getBean("mySpringBean"); ExecutorService exec = grid.newGridExecutorService(); Future<String> res = exec.submit(new GridHelloWorldCallable()); // Wait for callable completion. res.get(); System.out.println(">>>"); System.out.println(">>> Finished executing Grid \"Spring bean\" example."); System.out.println(">>> You should see printed out of 'Hello world' on one of the nodes."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); System.out.println(">>>"); ctx.destroy(); } private static final class GridHelloWorldCallable implements Callable<String>, Serializable { public String call() { System.out.println("Hello world."); return null; } } }
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <description>Main Spring file for grid configuration.</description> <bean id="mySpringBean" class="org.gridgain.grid.GridSpringBean" scope="singleton"> <property name="configuration"> <bean id="grid.cfg" class="org.gridgain.grid.GridConfigurationAdapter" scope="singleton"> <property name="gridName" value="mySpringGrid"/> </bean> </property> </bean> </beans>
