package org.gridgain.examples.jbosscache; import java.io.File; import org.jboss.cache.Cache; import org.jboss.cache.CacheFactory; import org.jboss.cache.DefaultCacheFactory; import org.jboss.cache.Fqn; import org.jboss.cache.Node; import org.jboss.cache.config.Configuration; import org.jboss.cache.config.parsing.XmlConfigurationParser; import org.jgroups.JChannelFactory; public final class GridJbossCacheManager { /** Cache configuration path relative to GridGain installation. */ public static final String CACHE_CFG_PATH = "C:/workspace/GridJBCache3/src/config/jboss-cache.xml"; /** JGroups configuration path relative to GridGain installation. */ public static final String JGROUPS_CFG_PATH = "C:/workspace/GridJBCache3/src/config/jgroups.xml"; /** JBoss Cache instance. */ private Cache cache = null; /** Cache node for caching example data. */ private Node 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. } public void start() throws GridException { File cacheCfg = new File(CACHE_CFG_PATH); if (cacheCfg == null) { throw new GridException("Failed to find cache configuration file: " + CACHE_CFG_PATH); } File jgroupsCfg = new File(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(); //null; try { //factory = new JChannelFactory(jgroupsCfg); 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 dataFactory = DefaultCacheFactory.getInstance(); // Instantiate and start JBoss Cache. cache = dataFactory.createCache(svrCfg); // Cache node for storing example data. cacheRoot = cache.getRoot().addChild(Fqn.fromString("/")); } catch (Exception e) { throw new GridException("Failed to start Data Manager.", e); } System.out.println("JBoss Cache data manager started."); } }