Search in sources :

Example 11 with Cache

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

the class RetrieveFromCacheServiceTest method testDoService_KeyNotFound.

@Test
public void testDoService_KeyNotFound() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", new ArrayList<MetadataElement>());
    Cache cache = createCacheInstanceForTests();
    RetrieveFromCacheService service = createService();
    CacheEntryEvaluator eval = new CacheEntryEvaluator();
    eval.setErrorOnEmptyKey(false);
    service.addCacheEntryEvaluator(eval);
    try {
        service.setConnection(new CacheConnection(cache));
        start(service);
        service.doService(msg);
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 12 with Cache

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

the class RetrieveFromCacheServiceTest method testDoService_NullValueTranslator.

@Test
public void testDoService_NullValueTranslator() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) }));
    Cache cache = createCacheInstanceForTests();
    RetrieveFromCacheService service = createServiceForTests();
    CacheEntryEvaluator eval = service.getCacheEntryEvaluators().get(0);
    // we know that StaticCacheValueTranslator should throw an exception if
    // we try to apply a value into the message.
    eval.setValueTranslator(new StaticCacheValueTranslator("dummy value"));
    try {
        service.setConnection(new CacheConnection(cache));
        start(service);
        // Now add the TARGET DATA to the cache.
        cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE);
        service.doService(msg);
        fail("Expected a ServiceException");
    } catch (ServiceException expected) {
    } finally {
        stop(service);
    }
}
Also used : ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) StaticCacheValueTranslator(com.adaptris.core.services.cache.translators.StaticCacheValueTranslator) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 13 with Cache

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

the class RetrieveFromCacheServiceTest method testDoService_NoExceptionIfNotFound_NotFound.

@Test
public void testDoService_NoExceptionIfNotFound_NotFound() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) }));
    Cache cache = createCacheInstanceForTests();
    RetrieveFromCacheService service = createServiceForTests();
    service.setExceptionIfNotFound(false);
    try {
        service.setConnection(new CacheConnection(cache));
        start(service);
        service.doService(msg);
        assertFalse(msg.headersContainsKey(TARGET_METADATA_KEY));
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 14 with Cache

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

the class RetrieveFromCacheServiceTest method testDoService.

@Test
public void testDoService() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) }));
    Cache cache = createCacheInstanceForTests();
    RetrieveFromCacheService service = createServiceForTests();
    try {
        service.setConnection(new CacheConnection(cache));
        start(service);
        // Now add the TARGET DATA to the cache.
        cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE);
        service.doService(msg);
        assertTrue(msg.headersContainsKey(TARGET_METADATA_KEY));
        assertEquals(LOOKED_UP_VALUE, msg.getMetadataValue(TARGET_METADATA_KEY));
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 15 with Cache

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

the class AddToCacheService method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        Cache cache = retrieveCache();
        for (CacheEntryEvaluator ceg : getCacheEntryEvaluators()) {
            String key = ceg.getKey(msg);
            Object value = ceg.getValue(msg);
            if (key == null || value == null) {
                log.warn("{} generated null values for either the key or value, not storing in cache", ceg.friendlyName());
                continue;
            }
            if (enforceSerializable() && !(value instanceof Serializable)) {
                throw new ServiceException("Cache value " + value + " should be Serializable, but is of type " + value.getClass());
            }
            cache.put(key, value);
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : Serializable(java.io.Serializable) ServiceException(com.adaptris.core.ServiceException) 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