camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1431891 - in /camel/trunk/components/camel-cache/src: main/java/org/apache/camel/component/cache/ test/java/org/apache/camel/component/cache/
Date Fri, 11 Jan 2013 05:14:31 GMT
Author: ningjiang
Date: Fri Jan 11 05:14:31 2013
New Revision: 1431891

URL: http://svn.apache.org/viewvc?rev=1431891&view=rev
Log:
CAMEL-5950 fixed the issue that Cache producer is not thread safe

Modified:
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConsumer.java
    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/CacheRegistryRefTest.java

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConsumer.java?rev=1431891&r1=1431890&r2=1431891&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConsumer.java
(original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConsumer.java
Fri Jan 11 05:14:31 2013
@@ -16,23 +16,35 @@
  */
 package org.apache.camel.component.cache;
 
+import net.sf.ehcache.Ehcache;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class CacheConsumer extends DefaultConsumer {
+    private static final transient Logger LOG = LoggerFactory.getLogger(CacheConsumer.class);
 
-    private CacheConfiguration config;
+    private CacheEventListener cacheEventListener;
+    
+    private Ehcache cache;
 
     public CacheConsumer(Endpoint endpoint, Processor processor, CacheConfiguration config)
{
         super(endpoint, processor);
-        this.config = config;
     }
 
     @Override
     protected void doStart() throws Exception {
         super.doStart();
         createConsumerCacheConnection();
+        LOG.debug("initialize the cache");
+    }
+    
+    @Override
+    protected void doStop() throws Exception {
+        // unregisty the listenser when the consumer is stopped
+        cache.getCacheEventNotificationService().unregisterListener(cacheEventListener);
     }
 
     @Override
@@ -41,11 +53,10 @@ public class CacheConsumer extends Defau
     }
 
     protected void createConsumerCacheConnection() {
-        CacheEventListener cacheEventListener = new CacheEventListener();
+        cacheEventListener = new CacheEventListener();
         cacheEventListener.setCacheConsumer(this);
-
-        config.getEventListenerRegistry().addCacheEventListener(cacheEventListener);
-
-        getEndpoint().initializeCache();
+        cache = getEndpoint().initializeCache();
+        // registry the CacheEventListener directly 
+        cache.getCacheEventNotificationService().registerListener(cacheEventListener);
     }
 }

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=1431891&r1=1431890&r2=1431891&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
Fri Jan 11 05:14:31 2013
@@ -43,6 +43,7 @@ public class CacheProducer extends Defau
     @Override
     protected void doStart() throws Exception {
         super.doStart();
+        cache = getEndpoint().initializeCache();
     }
 
     @Override
@@ -52,9 +53,6 @@ public class CacheProducer extends Defau
 
     public void process(Exchange exchange) throws Exception {
         LOG.trace("Cache Name: {}", config.getCacheName());
-
-        cache = getEndpoint().initializeCache();
-
         Map<String, Object> headers = exchange.getIn().getHeaders();
         String key = (headers.containsKey(CacheConstants.CACHE_KEY))
                 ? exchange.getIn().getHeader(CacheConstants.CACHE_KEY, String.class)

Modified: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheRegistryRefTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheRegistryRefTest.java?rev=1431891&r1=1431890&r2=1431891&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheRegistryRefTest.java
(original)
+++ camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheRegistryRefTest.java
Fri Jan 11 05:14:31 2013
@@ -56,9 +56,9 @@ public class CacheRegistryRefTest extend
 
     @Override
     public void setUp() throws Exception {
-        super.setUp();
         eventListenerRegistry.addCacheEventListener(new TestCacheEventListener());
         loaderRegistry.addCacheLoader(new TestLoader());
+        super.setUp();
     }
 
     @Override
@@ -90,7 +90,7 @@ public class CacheRegistryRefTest extend
                 in.setBody("Hello World");
             }
         });
-
+        
         CacheManager cm = cacheEndpoint.getCacheManagerFactory().getInstance();
         Cache cache = cm.getCache(cacheEndpoint.getConfig().getCacheName());
         Set<CacheEventListener> ehcacheEventListners = cache.getCacheEventNotificationService().getCacheEventListeners();
@@ -106,6 +106,7 @@ public class CacheRegistryRefTest extend
 
         //Expecting 2 listeners- one added by us: TestCacheEventListener and
         //one added by ehcache by cfg file.
+        System.out.println("EventListenser is " + ehcacheEventListners.iterator().next());
         assertEquals("Number of registered listeners", 2, ehcacheEventListners.size());
 
         assertEquals("Number of registered loaders", 1, cacheLoaders.size());



Mime
View raw message