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 2018/08/24 09:46:25 UTC

[camel] branch master updated: CAMEL-12610: Added global cache option on bean component.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 358a5de  CAMEL-12610: Added global cache option on bean component.
358a5de is described below

commit 358a5de18e0cb5239c3e7169bc4b1a9b43dad731
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Aug 24 11:45:50 2018 +0200

    CAMEL-12610: Added global cache option on bean component.
---
 camel-core/src/main/docs/bean-component.adoc       | 11 +++++-
 camel-core/src/main/docs/class-component.adoc      | 11 +++++-
 .../apache/camel/component/bean/BeanComponent.java | 30 ++++++++++++----
 .../processor/BeanCachedProcessorGlobalTest.java   | 42 ++++++++++++++++++++++
 .../springboot/BeanComponentConfiguration.java     | 14 ++++++++
 .../springboot/ClassComponentConfiguration.java    | 14 ++++++++
 6 files changed, 114 insertions(+), 8 deletions(-)

diff --git a/camel-core/src/main/docs/bean-component.adoc b/camel-core/src/main/docs/bean-component.adoc
index 9221e1c..e8f777a 100644
--- a/camel-core/src/main/docs/bean-component.adoc
+++ b/camel-core/src/main/docs/bean-component.adoc
@@ -19,7 +19,16 @@ the Registry
 
 
 // component options: START
-The Bean component has no options.
+The Bean component supports 2 options, which are listed below.
+
+
+
+[width="100%",cols="2,5,^1,2",options="header"]
+|===
+| Name | Description | Default | Type
+| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. |  | Boolean
+| *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean
+|===
 // component options: END
 
 
diff --git a/camel-core/src/main/docs/class-component.adoc b/camel-core/src/main/docs/class-component.adoc
index e44fef7..cece0d1 100644
--- a/camel-core/src/main/docs/class-component.adoc
+++ b/camel-core/src/main/docs/class-component.adoc
@@ -22,7 +22,16 @@ bean.
 
 
 // component options: START
-The Class component has no options.
+The Class component supports 2 options, which are listed below.
+
+
+
+[width="100%",cols="2,5,^1,2",options="header"]
+|===
+| Name | Description | Default | Type
+| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. |  | Boolean
+| *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean
+|===
 // component options: END
 
 
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index ba7cbaf..46d95ef 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -20,6 +20,7 @@ import java.util.Map;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.spi.Metadata;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.LRUCache;
 import org.apache.camel.util.LRUCacheFactory;
@@ -35,7 +36,11 @@ public class BeanComponent extends UriEndpointComponent {
     // use an internal soft cache for BeanInfo as they are costly to introspect
     // for example the bean language using OGNL expression runs much faster reusing the BeanInfo from this cache
     @SuppressWarnings("unchecked")
-    private final Map<BeanInfoCacheKey, BeanInfo> cache = LRUCacheFactory.newLRUSoftCache(1000);
+    private final Map<BeanInfoCacheKey, BeanInfo> beanInfoCache = LRUCacheFactory.newLRUSoftCache(1000);
+
+    @Metadata(label = "advanced", description = "If enabled, Camel will cache the result of the first Registry look-up."
+        + " Cache can be enabled if the bean in the Registry is defined as a singleton scope.")
+    private Boolean cache;
 
     public BeanComponent() {
         super(BeanEndpoint.class);
@@ -50,6 +55,7 @@ public class BeanComponent extends UriEndpointComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
         BeanEndpoint endpoint = new BeanEndpoint(uri, this);
         endpoint.setBeanName(remaining);
+        endpoint.setCache(cache);
         setProperties(endpoint, parameters);
 
         // the bean.xxx options is for the bean
@@ -59,19 +65,31 @@ public class BeanComponent extends UriEndpointComponent {
     }
     
     BeanInfo getBeanInfoFromCache(BeanInfoCacheKey key) {
-        return cache.get(key);
+        return beanInfoCache.get(key);
     }
 
     void addBeanInfoToCache(BeanInfoCacheKey key, BeanInfo beanInfo) {
-        cache.put(key, beanInfo);
+        beanInfoCache.put(key, beanInfo);
     }
 
     @Override
     protected void doShutdown() throws Exception {
-        if (LOG.isDebugEnabled() && cache instanceof LRUCache) {
-            LRUCache cache = (LRUCache) this.cache;
+        if (LOG.isDebugEnabled() && beanInfoCache instanceof LRUCache) {
+            LRUCache cache = (LRUCache) this.beanInfoCache;
             LOG.debug("Clearing BeanInfo cache[size={}, hits={}, misses={}, evicted={}]", new Object[]{cache.size(), cache.getHits(), cache.getMisses(), cache.getEvicted()});
         }
-        cache.clear();
+        beanInfoCache.clear();
+    }
+
+    public Boolean getCache() {
+        return cache;
+    }
+
+    /**
+     * If enabled, Camel will cache the result of the first Registry look-up.
+     * Cache can be enabled if the bean in the Registry is defined as a singleton scope.
+     */
+    public void setCache(Boolean cache) {
+        this.cache = cache;
     }
 }
diff --git a/camel-core/src/test/java/org/apache/camel/processor/BeanCachedProcessorGlobalTest.java b/camel-core/src/test/java/org/apache/camel/processor/BeanCachedProcessorGlobalTest.java
new file mode 100644
index 0000000..159deae
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/BeanCachedProcessorGlobalTest.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.bean.BeanComponent;
+
+public class BeanCachedProcessorGlobalTest extends BeanCachedProcessorTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // set global cache on bean
+                BeanComponent bean = getContext().getComponent("bean", BeanComponent.class);
+                bean.setCache(true);
+
+                from("direct:noCache")
+                        .to("bean:something?cache=false");
+                from("direct:cached")
+                        .to("bean:something");
+
+            }
+        };
+    }
+
+}
diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/bean/springboot/BeanComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/bean/springboot/BeanComponentConfiguration.java
index cc8c2fb..40ec635 100644
--- a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/bean/springboot/BeanComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/bean/springboot/BeanComponentConfiguration.java
@@ -37,12 +37,26 @@ public class BeanComponentConfiguration
      */
     private Boolean enabled;
     /**
+     * If enabled, Camel will cache the result of the first Registry look-up.
+     * Cache can be enabled if the bean in the Registry is defined as a
+     * singleton scope.
+     */
+    private Boolean cache;
+    /**
      * Whether the component should resolve property placeholders on itself when
      * starting. Only properties which are of String type can use property
      * placeholders.
      */
     private Boolean resolvePropertyPlaceholders = true;
 
+    public Boolean getCache() {
+        return cache;
+    }
+
+    public void setCache(Boolean cache) {
+        this.cache = cache;
+    }
+
     public Boolean getResolvePropertyPlaceholders() {
         return resolvePropertyPlaceholders;
     }
diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/beanclass/springboot/ClassComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/beanclass/springboot/ClassComponentConfiguration.java
index 7a49145..e660b2f 100644
--- a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/beanclass/springboot/ClassComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/component/beanclass/springboot/ClassComponentConfiguration.java
@@ -37,12 +37,26 @@ public class ClassComponentConfiguration
      */
     private Boolean enabled;
     /**
+     * If enabled, Camel will cache the result of the first Registry look-up.
+     * Cache can be enabled if the bean in the Registry is defined as a
+     * singleton scope.
+     */
+    private Boolean cache;
+    /**
      * Whether the component should resolve property placeholders on itself when
      * starting. Only properties which are of String type can use property
      * placeholders.
      */
     private Boolean resolvePropertyPlaceholders = true;
 
+    public Boolean getCache() {
+        return cache;
+    }
+
+    public void setCache(Boolean cache) {
+        this.cache = cache;
+    }
+
     public Boolean getResolvePropertyPlaceholders() {
         return resolvePropertyPlaceholders;
     }