use of net.sf.ehcache.CacheException in project camel by apache.
the class CacheEventListener method dispatchExchange.
private void dispatchExchange(Ehcache cache, Element element, String operation) {
Exchange exchange;
LOG.debug("Consumer Dispatching the Exchange containing the Element {} in cache {}", element, cache.getName());
if (element == null) {
exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, "", "");
} else {
exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, (String) element.getObjectKey(), element.getObjectValue());
}
try {
cacheConsumer.getProcessor().process(exchange);
} catch (Exception e) {
throw new CacheException("Error in consumer while dispatching exchange containing key " + (element != null ? element.getObjectKey() : null) + " for further processing", e);
}
}
use of net.sf.ehcache.CacheException in project killbill by killbill.
the class TestStateMachineConfigCache method testExistingTenantStateMachineConfig.
@Test(groups = "fast")
public void testExistingTenantStateMachineConfig() throws PaymentApiException, URISyntaxException, IOException {
final String pluginName = UUID.randomUUID().toString();
final InternalCallContext differentMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(differentMultiTenantContext.getTenantRecordId()).thenReturn(55667788L);
final AtomicBoolean shouldThrow = new AtomicBoolean(false);
final Long multiTenantRecordId = multiTenantContext.getTenantRecordId();
final Long otherMultiTenantRecordId = otherMultiTenantContext.getTenantRecordId();
Mockito.when(tenantInternalApi.getPluginPaymentStateMachineConfig(Mockito.eq(pluginName), Mockito.any(InternalTenantContext.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
if (shouldThrow.get()) {
throw new RuntimeException();
}
final InternalTenantContext internalContext = (InternalTenantContext) invocation.getArguments()[1];
if (multiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return new String(ByteStreams.toByteArray(UriAccessor.accessUri(Resources.getResource(PaymentModule.DEFAULT_STATE_MACHINE_PAYMENT_XML).toExternalForm())));
} else if (otherMultiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return new String(ByteStreams.toByteArray(UriAccessor.accessUri(Resources.getResource(PaymentModule.DEFAULT_STATE_MACHINE_RETRY_XML).toExternalForm())));
} else {
return null;
}
}
});
// Verify the lookup for a non-cached tenant. No system config is set yet but EhCacheStateMachineConfigCache returns a default empty one
final StateMachineConfig defaultStateMachineConfig = stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, differentMultiTenantContext);
Assert.assertNotNull(defaultStateMachineConfig);
// Make sure the cache loader isn't invoked, see https://github.com/killbill/killbill/issues/300
shouldThrow.set(true);
final StateMachineConfig defaultStateMachineConfig2 = stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, differentMultiTenantContext);
Assert.assertNotNull(defaultStateMachineConfig2);
Assert.assertEquals(defaultStateMachineConfig2, defaultStateMachineConfig);
shouldThrow.set(false);
// Verify the lookup for this tenant
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(UUID.randomUUID().toString(), multiTenantContext), defaultStateMachineConfig);
final StateMachineConfig result = stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, multiTenantContext);
Assert.assertNotNull(result);
Assert.assertNotEquals(result, defaultStateMachineConfig);
Assert.assertEquals(result.getStateMachines().length, 8);
// Verify the lookup for another tenant
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(UUID.randomUUID().toString(), otherMultiTenantContext), defaultStateMachineConfig);
final StateMachineConfig otherResult = stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, otherMultiTenantContext);
Assert.assertNotNull(otherResult);
Assert.assertEquals(otherResult.getStateMachines().length, 1);
shouldThrow.set(true);
// Verify the lookup for this tenant
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, multiTenantContext), result);
// Verify the lookup with another context for the same tenant
final InternalCallContext sameMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(sameMultiTenantContext.getAccountRecordId()).thenReturn(9102L);
Mockito.when(sameMultiTenantContext.getTenantRecordId()).thenReturn(multiTenantRecordId);
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, sameMultiTenantContext), result);
// Verify the lookup with the other tenant
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, otherMultiTenantContext), otherResult);
// Verify clearing the cache works
stateMachineConfigCache.clearPaymentStateMachineConfig(pluginName, multiTenantContext);
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, otherMultiTenantContext), otherResult);
try {
stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, multiTenantContext);
Assert.fail();
} catch (final CacheException exception) {
Assert.assertTrue(exception.getCause() instanceof RuntimeException);
}
}
use of net.sf.ehcache.CacheException in project jforum2 by rafaelsteil.
the class EhCacheEngine method get.
public Object get(String fullyQualifiedName, String key) {
try {
if (!manager.cacheExists(fullyQualifiedName)) {
manager.addCache(fullyQualifiedName);
return null;
}
Cache cache = manager.getCache(fullyQualifiedName);
Element element = cache.get(key);
if (element != null) {
return element.getValue();
}
return null;
} catch (CacheException ce) {
log.error("EhCache could not be shutdown", ce);
throw new RuntimeException(ce);
}
}
use of net.sf.ehcache.CacheException in project jforum2 by rafaelsteil.
the class EhCacheEngine method getValues.
public Collection getValues(String fullyQualifiedName) {
try {
if (!manager.cacheExists(fullyQualifiedName)) {
manager.addCache(fullyQualifiedName);
return new ArrayList();
}
Cache cache = manager.getCache(fullyQualifiedName);
List values = new ArrayList(cache.getSize());
List keys = cache.getKeys();
for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
values.add(cache.get((Serializable) iter.next()));
}
return values;
} catch (CacheException ce) {
log.error("EhCache could not be shutdown", ce);
throw new RuntimeException(ce);
}
}
use of net.sf.ehcache.CacheException in project spring-framework by spring-projects.
the class EhCacheSupportTests method testCacheManagerConflict.
@Test
public void testCacheManagerConflict() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setCacheManagerName("myCacheManager");
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
assertTrue("Singleton property", cacheManagerFb.isSingleton());
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
Cache myCache1 = cm.getCache("myCache1");
assertTrue("No myCache1 defined", myCache1 == null);
EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean();
cacheManagerFb2.setCacheManagerName("myCacheManager");
cacheManagerFb2.afterPropertiesSet();
fail("Should have thrown CacheException because of naming conflict");
} catch (CacheException ex) {
// expected
} finally {
cacheManagerFb.destroy();
}
}
Aggregations