Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > HelloWorld - Gridify With Spring Bean
HelloWorld - Gridify With Spring Bean
Added by ghost, last edited by ghost on Mar 08, 2008  (view change)
Labels: 
(None)


Gridify With Spring Bean Example

This example demonstrates a simple way to grid-enable a spring bean. GridGain will detect that a method is annotated with @Gridify Javadoc annotation, it will execute this method on remote node.

String "Hello World" is set into GridifySpringBeanHelloWolrd instance as its state. Since method GridifySpringBeanHelloWolrd#sayIt() is annotated with @Gridify Javadoc annotation it is automatically grid-enabled and is executed on remote grid node. The GridifySpringBeanHelloWolrd is initialized in spring-bean.xml Spring configuration file.

Package:
org.gridgain.examples.helloworld.gridify.springbean

There are three classes implemented for this example:

AspectJ AOP Configuration

We will use AspectJ AOP for this example. To use other AOP implementations (such as JBoss AOP, or Spring AOP), refer to AOP Configuration documentation.

The following configuration needs to be applied to enable AspectJ byte code weaving.

  • JVM configuration should include: -javaagent:[GRIDGAIN_HOME]/libs/aspectjweaver-1.5.3.jar
  • Classpath should contain the [GRIDGAIN_HOME]/config/aop/aspectj folder.

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.

GridifySpringBeanHelloWolrd.java

This class represents a stateful instance that needs to be executed on Grid. For simplicity the state is a simple Java string. Method GridifyHelloWorld.sayIt() simply prints out the state and returns number of characters in the state string.This bean is initialized from spring-bean.xml Spring configuration file.

Add @Gridify Annotation.

We add @Gridify Javadoc annotation specifying custom task class we use responsible for splitting method execution into sub-jobs and properly initializing the state.

@Gridify
public int sayIt() {
    // Simply print out the argument.
    System.out.println(">>>");
    System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method.");
    System.out.println(">>>");
      
    return phrase.length();
}

Full Source Code.

GridifySpringBeanHelloWolrd.java
package org.gridgain.examples.helloworld.gridify.springbean;

import org.gridgain.grid.gridify.*;

public class GridifySpringBeanHelloWolrd {
    /** Example state. */
    private String phrase = null;

    /**
     * Gets example state.
     *
     * @return Example state.
     */
    public String getPhrase() {
        return phrase;
    }

    /**
     * Sets phrase to print.
     *
     * @param phrase Phrase to print.
     */
    public void setPhrase(String phrase) {
        this.phrase = phrase;
    }

    /**
     * Method grid-enabled with {@link Gridify} annotation and will
     * be executed on the grid. It simply prints out the 'phrase' 
     * set in this instance) and returns the number of characters 
     * in the phrase.
     * 
     * @return Number of characters in the <tt>'phrase'</tt> string.
     */
    @Gridify
    public int sayIt() {
        // Simply print out the argument.
        System.out.println(">>>");
        System.out.println(">>> Printing '" + phrase + "' on this node from grid-enabled method.");
        System.out.println(">>>");
        
        return phrase.length();
    }
}

GridifySpringBeanHelloWorldExample.java

1. Import GridGain classes.

import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;

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.

Full Source Code

GridifySpringBeanHelloWorldExample.java
package org.gridgain.examples.helloworld.gridify.springbean;

import org.gridgain.grid.*;
import org.gridgain.grid.gridify.*;

public final class GridifyHelloWorldStateExample {
    /**
     * Ensure singleton.
     */
    private GridifySpringBeanHelloWorldExample() {
        // No-op.
    }

    /**
     * Execute <tt>HelloWorld</tt> example grid-enabled with <tt>Gridify</tt> annotation.
     *
     * @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 {
            // Initialize Spring factory.
            XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("org/gridgain/examples/helloworld/gridify/springbean/spring-bean.xml"));

            // Get the Spring bean to grid-enable.
            GridifySpringBeanHelloWolrd bean = (GridifySpringBeanHelloWolrd)factory.getBean("hello.world.bean");

            // This method will be executed on a remote grid nodes.
            int phraseLen = bean.sayIt();

            System.out.println(">>>");
            System.out.println(">>> Finished executing Gridify Spring Bean \"Hello World\" example.");
            System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'.");
            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);
        }
    }
}

spring-bean.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">
    <!--
        Example definition for the GridifyHelloWorldSpringBean.
    -->
    <bean id="hello.world.bean"
          class="org.gridgain.examples.helloworld.gridify.springbean.GridifySpringBeanHelloWolrd">
        <!-- State value. -->
        <property name="phrase" value="Hello World"/>
    </bean>
</beans>

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators