Search in sources :

Example 1 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_NoExpiryMetadata.

@Test
public void testDoService_NoExpiryMetadata() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests().withExpiration(new TimeInterval(1L, TimeUnit.SECONDS));
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // msg.addMetadata("expiry", "a"); // empty expiry should not expire.
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 2 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_AbsoluteExpiry_Millis.

@Test
public void testDoService_AbsoluteExpiry_Millis() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // Expire in 2 seconds or so.
        long expiry = System.currentTimeMillis() + ThreadLocalRandom.current().nextInt(1000) + 1000;
        msg.addMetadata("expiry", String.valueOf(expiry));
        System.err.println(getName() + ":" + msg.getMetadataValue("expiry"));
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 3 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_Expiry_Unsupported.

@Test
public void testDoService_Expiry_Unsupported() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests().withExpiration(new TimeInterval(1L, TimeUnit.SECONDS));
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // unparseable expiry should not expire.
        msg.addMetadata("expiry", "a");
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 4 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class GetValueFromCacheTest method testDoService.

@Test
public void testDoService() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    Cache cache = createCacheInstanceForTests();
    GetValueFromCache service = new GetValueFromCache().withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        cache.put(msg.getUniqueId(), "Goodbye Cruel World");
        service.doService(msg);
        assertNotEquals("Hello World", msg.getContent());
        assertEquals("Goodbye Cruel World", msg.getContent());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 5 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class BasicCacheExampleGenerator method configureRetrieveService.

private static <T extends RetrieveFromCacheService> T configureRetrieveService(T service) {
    CacheEntryEvaluator eval1 = new CacheEntryEvaluator();
    CacheEntryEvaluator eval2 = new CacheEntryEvaluator();
    CacheEntryEvaluator eval3 = new CacheEntryEvaluator();
    eval1.setKeyTranslator(new MetadataCacheValueTranslator("A_MetadataKey_Whose_Value_Is_The_Cache_key"));
    eval1.setValueTranslator(new MetadataCacheValueTranslator("A_MetadataKey_Which_Will_Contain_What_We_Find_in_The_Cache"));
    eval2.setKeyTranslator(new MetadataCacheValueTranslator("MetadataKey_Whose_Value_Is_The_Cache_Key_And_This_Key_Contains_A_Payload"));
    eval2.setValueTranslator(new StringPayloadCacheTranslator());
    eval3.setKeyTranslator(new MetadataCacheValueTranslator("JMSCorrelationID"));
    eval3.setValueTranslator(new JmsReplyToCacheValueTranslator());
    service.setCacheEntryEvaluators(new ArrayList(Arrays.asList(new CacheEntryEvaluator[] { eval1, eval2, eval3 })));
    return service;
}
Also used : ObjectMetadataCacheValueTranslator(com.adaptris.core.services.cache.translators.ObjectMetadataCacheValueTranslator) MetadataCacheValueTranslator(com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator) JmsReplyToCacheValueTranslator(com.adaptris.core.services.cache.translators.JmsReplyToCacheValueTranslator) ArrayList(java.util.ArrayList) CacheEntryEvaluator(com.adaptris.core.services.cache.CacheEntryEvaluator) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)

Aggregations

StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)12 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)10 Test (org.junit.Test)10 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)7 Cache (com.adaptris.core.cache.Cache)5 MyCacheEventListener (com.adaptris.core.cache.MyCacheEventListener)5 ServiceException (com.adaptris.core.ServiceException)3 CacheEntryEvaluator (com.adaptris.core.services.cache.CacheEntryEvaluator)2 JmsReplyToCacheValueTranslator (com.adaptris.core.services.cache.translators.JmsReplyToCacheValueTranslator)2 MetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator)2 ObjectMetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.ObjectMetadataCacheValueTranslator)2 TimeInterval (com.adaptris.util.TimeInterval)2 ArrayList (java.util.ArrayList)2 AddToCacheService (com.adaptris.core.services.cache.AddToCacheService)1 XpathCacheValueTranslator (com.adaptris.core.services.cache.translators.XpathCacheValueTranslator)1 DefectiveMessageFactory (com.adaptris.core.stubs.DefectiveMessageFactory)1 Date (java.util.Date)1