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;
}
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);
}
Aggregations