Search in sources :

Example 11 with CacheManager

use of net.sf.ehcache.CacheManager in project camel by apache.

the class CacheConfigurationFileTest method testConfigurationFile.

@Test
public void testConfigurationFile() throws Exception {
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(CacheConstants.CACHE_KEY, "myKey");
    map.put(CacheConstants.CACHE_OPERATION, "ADD");
    template.sendBodyAndHeaders("direct:start", "Hello World", map);
    assertMockEndpointsSatisfied();
    CacheManager cacheManager = cache.getCacheManagerFactory().getInstance();
    assertNotNull(cacheManager);
    assertEquals("target/mytemp", cacheManager.getConfiguration().getDiskStoreConfiguration().getPath());
}
Also used : HashMap(java.util.HashMap) CacheManager(net.sf.ehcache.CacheManager) Test(org.junit.Test)

Example 12 with CacheManager

use of net.sf.ehcache.CacheManager in project camel by apache.

the class CacheRoutesManagementTest method testConfig.

@Test
public void testConfig() throws Exception {
    //do some routes
    producerTemplate1.send(templateProcessor);
    producerTemplate2.send(templateProcessor);
    // Now do some routes to let endpoints be initialized
    template.sendBody("direct:add1", "Hello World");
    template.sendBody("direct:add2", "Hello World");
    //Now should not be null
    CacheManager cacheManager = cmfRef.getCacheManager();
    assertNotNull("CacheManager initialized", cacheManager);
    Cache cache = cmfRef.getCacheManager().getCache("foo");
    // Is cache alive
    assertEquals("Is cache still alive", Status.STATUS_ALIVE, cache.getStatus());
    context.stopRoute(ROUTE1_ID);
    // Is cache still alive?
    assertEquals("Is cache still alive", Status.STATUS_ALIVE, cache.getStatus());
    context.stop();
    // Was the cache shutdowned with context?
    assertEquals("Is cache still alive", Status.STATUS_SHUTDOWN, cache.getStatus());
}
Also used : CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache) Test(org.junit.Test) BaseCacheTest(org.apache.camel.component.BaseCacheTest)

Example 13 with CacheManager

use of net.sf.ehcache.CacheManager in project camel by apache.

the class DefaultCacheManagerFactoryTest method testEHCacheCompatiblity.

@Test
public void testEHCacheCompatiblity() throws Exception {
    // get the default cache manager
    CacheManagerFactory factory = new DefaultCacheManagerFactory();
    CacheManager manager = factory.getInstance();
    assertEquals(Status.STATUS_ALIVE, manager.getStatus());
    // create another unrelated cache manager
    Configuration conf = ConfigurationFactory.parseConfiguration(DefaultCacheManagerFactory.class.getResource("/test-ehcache.xml"));
    assertNotNull(conf);
    conf.setName("otherCache");
    CacheManager other = CacheManager.create(conf);
    assertEquals(Status.STATUS_ALIVE, other.getStatus());
    // shutdown this unrelated cache manager 
    other.shutdown();
    assertEquals(Status.STATUS_SHUTDOWN, other.getStatus());
    // the default cache manager should be still running
    assertEquals(Status.STATUS_ALIVE, manager.getStatus());
    factory.doStop();
    // the default cache manger is shutdown
    assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus());
}
Also used : Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager) Test(org.junit.Test)

Example 14 with CacheManager

use of net.sf.ehcache.CacheManager in project gocd by gocd.

the class GoCacheFactory method createCacheManager.

private CacheManager createCacheManager() throws UnsupportedEncodingException {
    Configuration configuration = new Configuration();
    configuration.setUpdateCheck(false);
    configuration.addDiskStore(diskStore());
    configuration.setDefaultCacheConfiguration(new CacheConfiguration("cache", 10000));
    return new CacheManager(configuration);
}
Also used : DiskStoreConfiguration(net.sf.ehcache.config.DiskStoreConfiguration) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration)

Example 15 with CacheManager

use of net.sf.ehcache.CacheManager in project sling by apache.

the class CacheManagerServiceImpl method activate.

@Activate
public void activate(Map<String, Object> properties) throws FileNotFoundException, IOException {
    String config = toString(properties.get(CACHE_CONFIG), DEFAULT_CACHE_CONFIG);
    File configFile = new File(config);
    if (configFile.exists()) {
        LOGGER.info("Configuring Cache from {} ", configFile.getAbsolutePath());
        InputStream in = null;
        try {
            in = processConfig(new FileInputStream(configFile), properties);
            cacheManager = new CacheManager(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    } else {
        LOGGER.info("Configuring Cache from Classpath Default {} ", CONFIG_PATH);
        InputStream in = processConfig(this.getClass().getClassLoader().getResourceAsStream(CONFIG_PATH), properties);
        if (in == null) {
            throw new IOException("Unable to open config at classpath location " + CONFIG_PATH);
        }
        cacheManager = new CacheManager(in);
        in.close();
    }
    final WeakReference<CacheManagerServiceImpl> ref = new WeakReference<CacheManagerServiceImpl>(this);
    /*
		 * Add in a shutdown hook, for safety
		 */
    Runtime.getRuntime().addShutdownHook(new Thread() {

        /*
			 * (non-Javadoc)
			 * 
			 * @see java.lang.Thread#run()
			 */
        @Override
        public void run() {
            try {
                CacheManagerServiceImpl cm = ref.get();
                if (cm != null) {
                    cm.deactivate();
                }
            } catch (Throwable t) {
                LOGGER.debug(t.getMessage(), t);
            }
        }
    });
    // register the cache manager with JMX
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) WeakReference(java.lang.ref.WeakReference) CacheManager(net.sf.ehcache.CacheManager) File(java.io.File) MBeanServer(javax.management.MBeanServer) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

CacheManager (net.sf.ehcache.CacheManager)102 Cache (net.sf.ehcache.Cache)55 ClassPathResource (org.springframework.core.io.ClassPathResource)21 Element (net.sf.ehcache.Element)20 Configuration (net.sf.ehcache.config.Configuration)18 Test (org.junit.Test)18 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)17 MarkupCache (org.apache.wicket.markup.MarkupCache)10 CacheException (net.sf.ehcache.CacheException)9 IOException (java.io.IOException)7 Ehcache (net.sf.ehcache.Ehcache)7 UpdatingSelfPopulatingCache (net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache)6 URL (java.net.URL)5 BlockingCache (net.sf.ehcache.constructs.blocking.BlockingCache)5 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)4 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)4 Around (org.aspectj.lang.annotation.Around)4 Before (org.junit.Before)4 File (java.io.File)3