Search in sources :

Example 16 with CacheException

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

the class CacheProducer method createElementFromBody.

private Element createElementFromBody(String key, Exchange exchange, String cacheOperation) throws NoTypeConversionAvailableException {
    Element element;
    Object body = exchange.getIn().getBody();
    if (body == null) {
        throw new CacheException("Body cannot be null for operation " + cacheOperation);
    } else if (body instanceof Serializable) {
        element = new Element(key, body);
    } else if (config.isObjectCache()) {
        element = new Element(key, body);
    } else {
        InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
        // Read InputStream into a byte[] buffer
        element = new Element(key, exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, is));
    }
    // set overrides for the cache expiration and such
    final Integer ttl = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_TTL, Integer.class);
    if (ttl != null) {
        element.setTimeToLive(ttl);
    }
    final Integer idle = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_IDLE, Integer.class);
    if (idle != null) {
        element.setTimeToIdle(idle);
    }
    final Boolean flag = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_ETERNAL, Boolean.class);
    if (flag != null) {
        element.setEternal(flag);
    }
    return element;
}
Also used : Serializable(java.io.Serializable) CacheException(net.sf.ehcache.CacheException) InputStream(java.io.InputStream) Element(net.sf.ehcache.Element)

Example 17 with CacheException

use of net.sf.ehcache.CacheException in project jforum2 by rafaelsteil.

the class EhCacheEngine method add.

public void add(String fullyQualifiedName, String key, Object value) {
    if (!manager.cacheExists(fullyQualifiedName)) {
        try {
            manager.addCache(fullyQualifiedName);
        } catch (CacheException ce) {
            log.error(ce, ce);
            throw new RuntimeException(ce);
        }
    }
    Cache cache = manager.getCache(fullyQualifiedName);
    Element element = new Element(key, (Serializable) value);
    cache.put(element);
}
Also used : CacheException(net.sf.ehcache.CacheException) Element(net.sf.ehcache.Element) Cache(net.sf.ehcache.Cache)

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