Search in sources :

Example 1 with CacheException

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);
    }
}
Also used : Exchange(org.apache.camel.Exchange) CacheException(net.sf.ehcache.CacheException) CacheException(net.sf.ehcache.CacheException)

Example 2 with CacheException

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheException(net.sf.ehcache.CacheException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StateMachineConfig(org.killbill.automaton.StateMachineConfig) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) Test(org.testng.annotations.Test)

Example 3 with CacheException

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);
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) Element(net.sf.ehcache.Element) Cache(net.sf.ehcache.Cache)

Example 4 with CacheException

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);
    }
}
Also used : Serializable(java.io.Serializable) CacheException(net.sf.ehcache.CacheException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Cache(net.sf.ehcache.Cache)

Example 5 with CacheException

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();
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) CacheManager(net.sf.ehcache.CacheManager) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) BlockingCache(net.sf.ehcache.constructs.blocking.BlockingCache) UpdatingSelfPopulatingCache(net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache) Cache(net.sf.ehcache.Cache) Test(org.junit.Test)

Aggregations

CacheException (net.sf.ehcache.CacheException)17 Element (net.sf.ehcache.Element)6 Cache (net.sf.ehcache.Cache)5 CacheManager (net.sf.ehcache.CacheManager)4 Ehcache (net.sf.ehcache.Ehcache)3 InstrumentedEhcache (com.codahale.metrics.ehcache.InstrumentedEhcache)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 Iterator (java.util.Iterator)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 StateMachineConfig (org.killbill.automaton.StateMachineConfig)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Test (org.testng.annotations.Test)2 FileInputStream (java.io.FileInputStream)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1