You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/03/20 16:08:47 UTC
svn commit: r1458871 - in /camel/trunk/components/camel-cache/src:
main/java/org/apache/camel/component/cache/CacheProducer.java
test/java/org/apache/camel/component/cache/CacheProducerTest.java
Author: davsclaus
Date: Wed Mar 20 15:08:47 2013
New Revision: 1458871
URL: http://svn.apache.org/r1458871
Log:
CAMEL-6189: Fixed camel-cache cache check. Thanks to Henrik Flintenborg Sorensen for the patch.
Modified:
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java
camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java
Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java?rev=1458871&r1=1458870&r2=1458871&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java Wed Mar 20 15:08:47 2013
@@ -101,7 +101,8 @@ public class CacheProducer extends Defau
}
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_CHECK)) {
LOG.debug("Querying an element with key {} from the Cache", key);
- if (cache.isKeyInCache(key)) {
+ Element element = cache.getQuiet(key); // getQuiet checks for element expiry
+ if (element != null) {
exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
} else {
exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
Modified: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java?rev=1458871&r1=1458870&r2=1458871&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java (original)
+++ camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java Wed Mar 20 15:08:47 2013
@@ -23,6 +23,8 @@ import java.util.concurrent.TimeUnit;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Element;
+import net.sf.ehcache.ElementEvictionData;
+
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
@@ -593,4 +595,110 @@ public class CacheProducerTest extends B
cacheExceptionEndpoint.assertIsSatisfied();
}
+ @Test
+ public void testCheckDataFromCache() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ public void configure() {
+ onException(CacheException.class).
+ handled(true).
+ to("log:LOGGER").
+ to("mock:CacheProducerTest.cacheException");
+
+ from("direct:a").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).
+ setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).
+ setBody(constant("Test body")).
+ to("cache://TestCache1").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_URL_CHECK)).
+ setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).
+ to("cache://TestCache1").
+ choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull()).
+ to("mock:CacheProducerTest.result").end();
+ }
+ });
+
+ resultEndpoint.expectedMessageCount(1);
+ cacheExceptionEndpoint.expectedMessageCount(0);
+ resultEndpoint.expectedBodiesReceived("Test body");
+ context.start();
+ LOG.debug("------------Beginning CacheProducer Check An Element Exists Test---------------");
+ sendOriginalFile();
+ resultEndpoint.assertIsSatisfied();
+ cacheExceptionEndpoint.assertIsSatisfied();
+ }
+
+ @Test
+ public void testCheckDataFromCacheNegativeTest() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ public void configure() {
+ onException(CacheException.class).
+ handled(true).
+ to("log:LOGGER").
+ to("mock:CacheProducerTest.cacheException");
+
+ from("direct:a").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).
+ setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).
+ setBody(constant("Test body")).
+ to("cache://TestCache1").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_URL_CHECK)).
+ setHeader(CacheConstants.CACHE_KEY, constant("foo")).
+ to("cache://TestCache1").
+ choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull()).
+ to("mock:CacheProducerTest.result").end();
+ }
+ });
+
+ resultEndpoint.expectedMessageCount(0);
+ cacheExceptionEndpoint.expectedMessageCount(0);
+ context.start();
+ LOG.debug("------------Beginning CacheProducer Check An Element Does Not Exist Test---------------");
+ sendOriginalFile();
+ resultEndpoint.assertIsSatisfied();
+ cacheExceptionEndpoint.assertIsSatisfied();
+ }
+
+ @Test
+ public void testCheckExpiredDataFromCache() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ public void configure() {
+ onException(CacheException.class).
+ handled(true).
+ to("log:LOGGER").
+ to("mock:CacheProducerTest.cacheException");
+
+ from("direct:a").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).
+ setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).
+ setBody(constant("Test body")).
+ to("cache://TestCache1?timeToLiveSeconds=1");
+
+ from("direct:b").
+ setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_URL_CHECK)).
+ setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).
+ to("cache://TestCache1").
+ choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()).
+ to("mock:CacheProducerTest.result").end();
+ }
+ });
+
+ // Put an element to the cache
+ resultEndpoint.expectedMessageCount(1);
+ cacheExceptionEndpoint.expectedMessageCount(0);
+ context.start();
+ LOG.debug("------------Beginning CacheProducer Check An Element Does Not Exist After Expiry Test---------------");
+ sendOriginalFile();
+
+ // Alter the cache element so it appears "expired" (without having to wait for it to happen)
+ Cache testCache = cache.getCacheManagerFactory().getInstance().getCache("TestCache1");
+ Element element = testCache.getQuiet("Ralph_Waldo_Emerson");
+ element.getElementEvictionData().setCreationTime(System.currentTimeMillis() - 10000);
+ testCache.putQuiet(element);
+
+ // Check that the element is not found when expired
+ template.sendBody("direct:b", "dummy");
+ resultEndpoint.assertIsSatisfied();
+ cacheExceptionEndpoint.assertIsSatisfied();
+ }
+
}