Search in sources :

Example 1 with CacheLoader

use of net.sf.ehcache.loader.CacheLoader in project killbill by killbill.

the class EhCacheCacheManagerProvider method get.

@Override
public CacheManager get() {
    final CacheManager cacheManager;
    try {
        final InputStream inputStream = UriAccessor.accessUri(cacheConfig.getCacheConfigLocation());
        cacheManager = CacheManager.create(inputStream);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    } catch (final URISyntaxException e) {
        throw new RuntimeException(e);
    }
    for (final BaseCacheLoader cacheLoader : cacheLoaders) {
        cacheLoader.init();
        final Ehcache cache = cacheManager.getEhcache(cacheLoader.getCacheType().getCacheName());
        if (cache == null) {
            logger.warn("Cache for cacheName='{}' not configured - check your ehcache.xml", cacheLoader.getCacheType().getCacheName());
            continue;
        }
        // Make sure we start from a clean state - this is mainly useful for tests
        for (final CacheLoader existingCacheLoader : cache.getRegisteredCacheLoaders()) {
            cache.unregisterCacheLoader(existingCacheLoader);
        }
        cache.registerCacheLoader(cacheLoader);
        // Instrument the cache
        final Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
        try {
            cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
        } catch (final CacheException e) {
            logger.warn("Unable to instrument cache {}: {}", cache.getName(), e.getMessage());
        }
    }
    return cacheManager;
}
Also used : CacheException(net.sf.ehcache.CacheException) InputStream(java.io.InputStream) CacheManager(net.sf.ehcache.CacheManager) InstrumentedEhcache(com.codahale.metrics.ehcache.InstrumentedEhcache) Ehcache(net.sf.ehcache.Ehcache) CacheLoader(net.sf.ehcache.loader.CacheLoader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 2 with CacheLoader

use of net.sf.ehcache.loader.CacheLoader in project camel by apache.

the class CacheRegistryRefTest method testConfig.

@Test
public void testConfig() throws Exception {
    producerTemplate.send(new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
            Message in = exchange.getIn();
            in.setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD);
            in.setHeader(CacheConstants.CACHE_KEY, "greeting");
            in.setBody("Hello World");
        }
    });
    CacheManager cm = cacheEndpoint.getCacheManagerFactory().getInstance();
    Cache cache = cm.getCache(cacheEndpoint.getConfig().getCacheName());
    Set<CacheEventListener> ehcacheEventListners = cache.getCacheEventNotificationService().getCacheEventListeners();
    List<CacheLoader> cacheLoaders = cache.getRegisteredCacheLoaders();
    CacheEventListenerRegistry configuredEventRegistry = cacheEndpoint.getConfig().getEventListenerRegistry();
    CacheLoaderRegistry configuredLoaderRegistry = cacheEndpoint.getConfig().getCacheLoaderRegistry();
    //Test if CacheEventListenerRegistry was referenced correctly
    assertEquals("CacheEventListenerRegistry size", 1, configuredEventRegistry.size());
    //Expecting 1 loader to be configured via spring
    assertEquals("configuredLoaderRegistry size", 1, configuredLoaderRegistry.size());
    //Expecting 1 listener added by us: TestCacheEventListener
    assertEquals("Number of registered listeners", 1, ehcacheEventListners.size());
    assertEquals("Number of registered loaders", 1, cacheLoaders.size());
    //Is our TestCacheEventListener really invoked?
    int puts = 0;
    for (Object listener : ehcacheEventListners) {
        if (listener instanceof TestCacheEventListener) {
            puts = ((TestCacheEventListener) listener).getNumberOfPuts();
            break;
        }
    }
    assertEquals("TestCacheEventListener put invocations", 1, puts);
    //Is cache loader initialized by ehcache
    assertEquals("loader initialized", cacheLoaders.get(0).getStatus(), Status.STATUS_ALIVE);
}
Also used : CacheEventListener(net.sf.ehcache.event.CacheEventListener) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) CacheException(net.sf.ehcache.CacheException) Exchange(org.apache.camel.Exchange) CacheManager(net.sf.ehcache.CacheManager) CacheLoader(net.sf.ehcache.loader.CacheLoader) Cache(net.sf.ehcache.Cache) Test(org.junit.Test) BaseCacheTest(org.apache.camel.component.BaseCacheTest)

Aggregations

CacheException (net.sf.ehcache.CacheException)2 CacheManager (net.sf.ehcache.CacheManager)2 CacheLoader (net.sf.ehcache.loader.CacheLoader)2 InstrumentedEhcache (com.codahale.metrics.ehcache.InstrumentedEhcache)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Cache (net.sf.ehcache.Cache)1 Ehcache (net.sf.ehcache.Ehcache)1 CacheEventListener (net.sf.ehcache.event.CacheEventListener)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Processor (org.apache.camel.Processor)1 BaseCacheTest (org.apache.camel.component.BaseCacheTest)1 Test (org.junit.Test)1