Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Affinity MapReduce with JBoss Cache > GridJbossCacheExampleNodeLoader.java
GridJbossCacheExampleNodeLoader.java
Added by ghost, last edited by ghost on Feb 05, 2008
Labels: 
(None)


Grid node startup class for JBoss Cache example. This class starts JBoss Cache and GridGain node.

Full Source Code

GridJbossCacheExampleNodeLoader.java
package org.gridgain.examples.jbosscache;

import java.util.concurrent.*;
import org.gridgain.grid.*;
import static org.gridgain.grid.GridFactoryState.STOPPED;

public final class GridJbossCacheExampleNodeLoader {
    /** Path to GridGain nodes Spring XML configuration. */
    public static final String GRID_CFG_PATH = "config/jbosscache/spring-jbosscache.xml";

    /** Start-stop latch. */
    private static final CountDownLatch latch = new CountDownLatch(1);

    /**
     * Loads grid node together with JBossCache.
     * 
     * @return Started grid node.
     * @throws GridException If node could not be started.
     */
    public static Grid loadNode() throws GridException {
        // This listener allows us to wait until CTRL-C is pressed.
        GridFactory.addListener(new GridFactoryListener() {
            /**
             * {@inheritDoc}
             */
            public void onStateChange(String name, GridFactoryState state) {
                if (state == STOPPED) {
                    // Stop JBoss Cache.
                    GridJbossCacheManager.getInstance().stop();

                    latch.countDown();
                }
            }
        });

        // Start JBoss Cache.
        GridJbossCacheManager.getInstance().start();

        // Start GridGain after cache is started.
        return GridFactory.start(GRID_CFG_PATH);
    }
    
    /**
     * Stops grid node together with jboss cache instance.
     */
    public static void unloadNode() {
        GridFactory.stop(true);
    }
    
    /**
     * Starts up data cache and Grid node.
     *
     * @param args Command line arguments (non  required).
     * @throws GridException If start failed.
     */
    public static void main(String[] args) throws GridException {
        loadNode();
        
        // Stay alive until CTRL-C is pressed.
        try {
            while (latch.getCount() > 0) {
                latch.await();
            }
        }
        catch (InterruptedException e) {
            System.err.println("Loader was interrupted (exiting): " + e.getMessage());
        }
    }
    
    /**
     * No-op constructor to ensure singleton.
     */
    private GridJbossCacheExampleNodeLoader() {
        // No-op.
    }
}

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