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


This data manager handles getting data from and putting data into JBoss Cache. It is a simple convenience wrapper around JBoss Cache.

It simply configures JBoss cache to work over JGroups with the same configuration as GridGain node does over the same multiplexor.

Full Source Code

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

import java.io.*;
import org.gridgain.grid.util.*;
import org.gridgain.grid.*;
import org.jgroups.*;
import org.jboss.cache.factories.*;
import org.jboss.cache.config.*;
import org.jboss.cache.*;

public final class GridJbossCacheManager {
    /** Cache configuration path relative to GridGain installation. */
    public static final String CACHE_CFG_PATH = "config/jbosscache/jboss-cache.xml";

    /** JGroups configuration path relative to GridGain installation. */
    public static final String JGROUPS_CFG_PATH = "config/jgroups/multicast/jgroups.xml";

    /** JBoss Cache instance. */
    private Cache<Long, String> cache = null;

    /** Cache node for caching example data. */
    private Node<Long, String> cacheRoot = null;

    /** Singleton instance. */
    private static GridJbossCacheManager instance = new GridJbossCacheManager();

    /**
     * Gets singleton.
     *
     * @return Singleton.
     */
    public static GridJbossCacheManager getInstance() {
        return instance;
    }

    /**
     * Ensure singleton.
     */
    private GridJbossCacheManager() {
        // No-op.
    }

    /**
     * Gets test data from data cache by ID.
     *
     * @param id Test data ID.
     * @return Cached data (possibly <tt>null</tt>).
     */
    public String getDataFromCache(long id) {
        assert cacheRoot != null;

        // Try to get child node from cache.
        Node<Long, String> dataNode = cacheRoot.getChild(new Fqn<Long>(id));

        // If data node does not exist, we lazily create it.
        if (dataNode == null) {
            dataNode = cacheRoot.addChild(new Fqn<Long>(id));
        }

        assert dataNode != null;

        // Retrieve cached data from cache.
        return dataNode.get(id);
    }

    /**
     * Puts test data to the data cache.
     *
     * @param id index that associated with this test data.
     * @param data Data to store in cache.
     */
    public void putDataToCache(long id, String data) {
        assert cacheRoot != null;

        Node<Long, String> dataNode = cacheRoot.getChild(new Fqn<Long>(id));

        // If data node does not exist, we lazily create it.
        if (dataNode == null) {
            dataNode = cacheRoot.addChild(new Fqn<Long>(id));
        }

        assert dataNode != null;

        // Cache data.
        dataNode.put(id, data);
    }

    /**
     * Starts JBoss Cache instance.
     * 
     * @throws GridException If start failed.
     */
    public void start() throws GridException {
        File cacheCfg = GridUtils.resolveGridGainPath(CACHE_CFG_PATH);

        if (cacheCfg == null) {
            throw new GridException("Failed to find cache configuration file: " + CACHE_CFG_PATH);
        }

        File jgroupsCfg = GridUtils.resolveGridGainPath(JGROUPS_CFG_PATH);

        if (jgroupsCfg == null) {
            throw new GridException("Failed to find jgroups configuration: " + JGROUPS_CFG_PATH);
        }

        // Make sure JBoss Cache and GridGain are on the same "wave length".
        JChannelFactory factory = new JChannelFactory();

        try {
            factory.setMultiplexerConfig(jgroupsCfg.getCanonicalPath());
        }
        catch (Exception e) {
            throw new GridException("Failed to start Data Manager.", e);
        }

        try {
            XmlConfigurationParser parser = new XmlConfigurationParser();

            // Start JBoss Cache cache with shared JGroups configuration.
            Configuration svrCfg = parser.parseFile(cacheCfg.getCanonicalPath());

            svrCfg.getRuntimeConfig().setMuxChannelFactory(factory);

            CacheFactory<Long, String> dataFactory = DefaultCacheFactory.getInstance();

            // Instantiate and start JBoss Cache.
            cache = dataFactory.createCache(svrCfg);

            // Cache node for storing example data.
            cacheRoot = cache.getRoot().addChild(Fqn.fromString("/gridgain/example"));
        }
        catch (IOException e) {
            throw new GridException("Failed to start Data Manager.", e);
        }

        System.out.println("JBoss Cache data manager started.");
    }

    /**
     * Stops JBoss Cache instance.
     */
    public void stop() {
        if (cache != null) {
            cache.stop();
            cache.destroy();
        }

        System.out.println("JBoss Cache data manager stopped.");
    }
}

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