Search in sources :

Example 16 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class AddValueToCache method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        Cache cache = retrieveCache();
        Optional<Expiry> hasExpiry = buildExpiry(msg);
        String cacheKey = msg.resolve(getKey());
        // should be hasExpiry.ifPresentOrElse() once we goto J11...
        if (hasExpiry.isPresent()) {
            TimeInterval expiryInterval = hasExpiry.get().expiresIn();
            log.trace("[{}] will expire in {}", cacheKey, expiryInterval);
            cache.put(cacheKey, getValueTranslator().getValueFromMessage(msg), hasExpiry.get().expiresIn());
        } else {
            log.trace("Expiry for [{}] taken from cache settings", cacheKey);
            cache.put(cacheKey, getValueTranslator().getValueFromMessage(msg));
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) CacheExpiry(com.adaptris.core.cache.CacheExpiry) Expiry(com.adaptris.core.cache.CacheExpiry.Expiry) ServiceException(com.adaptris.core.ServiceException) UnresolvedMetadataException(com.adaptris.core.UnresolvedMetadataException) Cache(com.adaptris.core.cache.Cache)

Example 17 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class CheckCacheService method eval.

protected boolean eval(AdaptrisMessage msg, FoundInCache callback) throws CoreException {
    int count = 0;
    int required = getCacheEntryEvaluators().size();
    Cache cache = retrieveCache();
    for (CacheEntryEvaluator lookup : getCacheEntryEvaluators()) {
        Object o = cache.get(lookup.getKey(msg));
        if (o != null) {
            count++;
            callback.handle(msg, o, lookup.valueTranslator());
        }
    }
    return count == required;
}
Also used : Cache(com.adaptris.core.cache.Cache)

Example 18 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class GetValueFromCache method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        Cache cache = retrieveCache();
        String key = msg.resolve(getKey());
        Object value = cache.get(key);
        if (value != null || !exceptionIfNotFound()) {
            getValueTranslator().addValueToMessage(msg, value);
        } else {
            throw new ServiceException(String.format("%s not found in cache", key));
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : ServiceException(com.adaptris.core.ServiceException) ServiceException(com.adaptris.core.ServiceException) Cache(com.adaptris.core.cache.Cache)

Example 19 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class RemoveFromCacheService method doService.

/**
 * Retrieves the item from the cache, stores it against the message and then removes it from the cache
 *
 * @see com.adaptris.core.services.cache.RetrieveFromCacheService#doService(com.adaptris.core.AdaptrisMessage)
 */
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        Cache cache = retrieveCache();
        for (CacheEntryEvaluator ceg : getCacheEntryEvaluators()) {
            String key = ceg.getKey(msg);
            if (isEmpty(key)) {
                log.warn("{} generated null values for the key, nothing to do", ceg.friendlyName());
                continue;
            }
            addCacheValueToMessage(msg, key, ceg.valueTranslator(), !exceptionIfNotFound());
            cache.remove(key);
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : ServiceException(com.adaptris.core.ServiceException) Cache(com.adaptris.core.cache.Cache)

Example 20 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class RemoveKeyFromCache method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        Cache cache = retrieveCache();
        cache.remove(msg.resolve(getKey()));
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : ServiceException(com.adaptris.core.ServiceException) Cache(com.adaptris.core.cache.Cache)

Aggregations

Cache (com.adaptris.core.cache.Cache)36 Test (org.junit.Test)27 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)23 ServiceException (com.adaptris.core.ServiceException)15 MetadataElement (com.adaptris.core.MetadataElement)14 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)6 MetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator)5 StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)5 CacheConnection (com.adaptris.core.services.cache.CacheConnection)4 CoreException (com.adaptris.core.CoreException)2 StaticCacheValueTranslator (com.adaptris.core.services.cache.translators.StaticCacheValueTranslator)2 DefectiveMessageFactory (com.adaptris.core.stubs.DefectiveMessageFactory)2 UnresolvedMetadataException (com.adaptris.core.UnresolvedMetadataException)1 CacheExpiry (com.adaptris.core.cache.CacheExpiry)1 Expiry (com.adaptris.core.cache.CacheExpiry.Expiry)1 CacheProvider (com.adaptris.core.cache.CacheProvider)1 TimeInterval (com.adaptris.util.TimeInterval)1 Serializable (java.io.Serializable)1 URL (java.net.URL)1 Queue (javax.jms.Queue)1