You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2017/03/15 15:29:43 UTC

camel git commit: CAMEL-11018: camel-ehcache: make it easy to create temporary caches

Repository: camel
Updated Branches:
  refs/heads/master c98e63d20 -> 294c169f6


CAMEL-11018: camel-ehcache: make it easy to create temporary caches


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/294c169f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/294c169f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/294c169f

Branch: refs/heads/master
Commit: 294c169f6d66225574ff3491d2d2dd53979cc82f
Parents: c98e63d
Author: lburgazzoli <lb...@gmail.com>
Authored: Wed Mar 15 16:27:12 2017 +0100
Committer: lburgazzoli <lb...@gmail.com>
Committed: Wed Mar 15 16:29:16 2017 +0100

----------------------------------------------------------------------
 .../component/ehcache/EhcacheComponent.java     |  9 +++---
 .../component/ehcache/EhcacheEndpoint.java      |  1 -
 .../camel/component/ehcache/EhcacheManager.java | 29 +++++++++++++++--
 .../ehcache/EhcacheConfigurationTest.java       | 33 ++++++++++++++++----
 4 files changed, 57 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/294c169f/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
index dd1e0a6..bccc39e 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
@@ -20,19 +20,18 @@ import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
-import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.impl.DefaultComponent;
 
 /**
- * Represents the component that manages {@link EhcacheEndpoint}.
+ * Represents the component that manages {@link DefaultComponent}.
  */
-public class EhcacheComponent extends UriEndpointComponent {
+public class EhcacheComponent extends DefaultComponent {
     
     public EhcacheComponent() {
-        super(EhcacheEndpoint.class);
     }
 
     public EhcacheComponent(CamelContext context) {
-        super(context, EhcacheEndpoint.class);
+        super(context);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/294c169f/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
index 9f091d7..ac7bd7f 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
@@ -84,5 +84,4 @@ public class EhcacheEndpoint extends DefaultEndpoint {
     EhcacheConfiguration getConfiguration() {
         return configuration;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/294c169f/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
----------------------------------------------------------------------
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
index 091af93..be56d06 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
@@ -17,11 +17,16 @@
 package org.apache.camel.component.ehcache;
 
 import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.apache.camel.Service;
 import org.apache.camel.util.ObjectHelper;
 import org.ehcache.Cache;
 import org.ehcache.CacheManager;
+import org.ehcache.UserManagedCache;
+import org.ehcache.config.CacheConfiguration;
+import org.ehcache.config.builders.UserManagedCacheBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -30,6 +35,8 @@ public class EhcacheManager implements Service {
 
     private final EhcacheConfiguration configuration;
     private final CacheManager cacheManager;
+    private final ConcurrentMap<String, UserManagedCache<?, ?>> userCaches;
+
     private final boolean managed;
 
     public EhcacheManager(EhcacheConfiguration configuration) throws IOException {
@@ -46,6 +53,7 @@ public class EhcacheManager implements Service {
 
     private EhcacheManager(CacheManager cacheManager, boolean managed, EhcacheConfiguration configuration) {
         this.cacheManager = cacheManager;
+        this.userCaches = new ConcurrentHashMap<>();
         this.managed = managed;
         this.configuration = configuration;
 
@@ -53,23 +61,38 @@ public class EhcacheManager implements Service {
     }
 
     @Override
-    public void start() throws Exception {
+    public synchronized void start() throws Exception {
         if (managed) {
             cacheManager.init();
         }
     }
 
     @Override
-    public void stop() throws Exception {
+    public synchronized void stop() throws Exception {
         if (managed) {
             cacheManager.close();
         }
+
+        // Clean up any User managed cache
+        userCaches.values().forEach(UserManagedCache::close);
     }
 
+    @SuppressWarnings("unchecked")
     public <K, V> Cache<K, V> getCache(String name, Class<K> keyType, Class<V> valueType) throws Exception {
         Cache<K, V> cache = cacheManager.getCache(name, keyType, valueType);
         if (cache == null && configuration != null && configuration.isCreateCacheIfNotExist()) {
-            cache = cacheManager.createCache(name, configuration.getMandatoryConfiguration());
+            CacheConfiguration<K, V> cacheConfiguration = configuration.getConfiguration();
+
+            if (cacheConfiguration != null) {
+                cache = cacheManager.createCache(name, cacheConfiguration);
+            } else {
+                // If a cache configuration is not provided, create a User Managed
+                // Cache
+                cache = (Cache<K, V>)userCaches.computeIfAbsent(
+                    name,
+                    key -> UserManagedCacheBuilder.newUserManagedCacheBuilder(keyType, valueType).build(true)
+                );
+            }
         }
 
         return cache;

http://git-wip-us.apache.org/repos/asf/camel/blob/294c169f/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java
index 8a77fff..b275869 100644
--- a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java
+++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java
@@ -22,6 +22,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.ehcache.Cache;
+import org.ehcache.UserManagedCache;
 import org.ehcache.config.ResourcePools;
 import org.ehcache.config.ResourceType;
 import org.ehcache.config.SizedResourcePool;
@@ -32,10 +33,12 @@ import org.ehcache.config.units.MemoryUnit;
 import org.junit.Test;
 
 public class EhcacheConfigurationTest extends CamelTestSupport {
-    @EndpointInject(uri = "ehcache://myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration")
-    private EhcacheEndpoint ehcacheConf;
-    @EndpointInject(uri = "ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml")
+    @EndpointInject(uri = "ehcache:myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration")
+    private EhcacheEndpoint ehcacheProgrammaticConf;
+    @EndpointInject(uri = "ehcache:myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml")
     private EhcacheEndpoint ehcacheFileConf;
+    @EndpointInject(uri = "ehcache:myUserCacheConf")
+    private EhcacheEndpoint ehcacheUserConf;
 
     @Override
     protected JndiRegistry createRegistry() throws Exception {
@@ -60,7 +63,7 @@ public class EhcacheConfigurationTest extends CamelTestSupport {
 
     @Test
     public void testProgrammaticConfiguration() throws Exception {
-        Cache<String, String> cache = getCache(ehcacheConf, "myProgrammaticCacheConf");
+        Cache<String, String> cache = getCache(ehcacheProgrammaticConf, "myProgrammaticCacheConf");
         ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools();
 
         SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP);
@@ -85,6 +88,21 @@ public class EhcacheConfigurationTest extends CamelTestSupport {
         assertEquals(EntryUnit.ENTRIES, h.getUnit());
     }
 
+    @Test
+    public void testUserConfiguration() throws Exception {
+        fluentTemplate()
+            .withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_PUT)
+            .withHeader(EhcacheConstants.KEY, "user-key")
+            .withBody("user-val")
+            .to("direct:ehcacheUserConf")
+            .send();
+
+        Cache<Object, Object> cache = ehcacheUserConf.getManager().getCache("myUserCacheConf", Object.class, Object.class);
+
+        assertTrue(cache instanceof UserManagedCache);
+        assertEquals("user-val", cache.get("user-key"));
+    }
+
     protected Cache<String, String> getCache(EhcacheEndpoint endpoint, String cacheName) throws Exception {
         return endpoint.getManager().getCache(cacheName, String.class, String.class);
     }
@@ -97,9 +115,12 @@ public class EhcacheConfigurationTest extends CamelTestSupport {
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-                from("direct://start")
-                    //.to(ehcacheConf)
+                from("direct:ehcacheProgrammaticConf")
+                    .to(ehcacheProgrammaticConf);
+                from("direct:ehcacheFileConf")
                     .to(ehcacheFileConf);
+                from("direct:ehcacheUserConf")
+                    .to(ehcacheUserConf);
             }
         };
     }