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/09/10 17:16:21 UTC

[2/6] git commit: CAMEL-6725: Added cache option to beanRef / in the DSL

CAMEL-6725: Added cache option to beanRef / <bean> in the DSL


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

Branch: refs/heads/master
Commit: c3078ad79d6fbd36427a3eaa4b3aafcc63914fd2
Parents: f25ee35
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Sep 10 17:13:37 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 10 17:13:37 2013 +0200

----------------------------------------------------------------------
 .../org/apache/camel/model/BeanDefinition.java  | 30 +++++++++++++++--
 .../apache/camel/model/ProcessorDefinition.java | 35 ++++++++++++++++++++
 .../bean/BeanExplicitMethodAmbiguousTest.java   |  6 ++--
 3 files changed, 66 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c3078ad7/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java b/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
index 22bd468..0211bbc 100644
--- a/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
@@ -48,6 +48,8 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
     private String method;
     @XmlAttribute
     private String beanType;
+    @XmlAttribute
+    private Boolean cache;
     @XmlTransient
     private Class<?> beanClass;
     @XmlTransient
@@ -130,6 +132,14 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
         this.beanClass = beanType;
     }
 
+    public Boolean getCache() {
+        return cache;
+    }
+
+    public void setCache(Boolean cache) {
+        this.cache = cache;
+    }
+
     // Fluent API
     //-------------------------------------------------------------------------
     /**
@@ -184,14 +194,30 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
         return this;
     }
 
+    /**
+     * Caches the bean lookup, to avoid lookup up bean on every usage.
+     *
+     * @return the builder
+     */
+    @Deprecated
+    public BeanDefinition cache() {
+        setCache(true);
+        return this;
+    }
+
     @Override
-    public Processor createProcessor(RouteContext routeContext) {
+    public Processor createProcessor(RouteContext routeContext) throws Exception {
         BeanProcessor answer;
         Class<?> clazz = bean != null ? bean.getClass() : null;
         BeanHolder beanHolder;
 
         if (ObjectHelper.isNotEmpty(ref)) {
-            beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
+            if (cache != null && cache) {
+                // cache the registry lookup which avoids repeat lookup in the registry
+                beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
+            } else {
+                beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
+            }
             // bean holder will check if the bean exists
             bean = beanHolder.getBean();
             answer = new BeanProcessor(beanHolder);

http://git-wip-us.apache.org/repos/asf/camel/blob/c3078ad7/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 9998e51..7d5699c 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -2580,6 +2580,41 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
 
     /**
      * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
+     * Adds a bean which is invoked which could be a final destination, or could be a transformation in a pipeline
+     *
+     * @param ref  reference to a bean to lookup in the registry
+     * @param 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.
+     * @return the builder
+     */
+    @SuppressWarnings("unchecked")
+    public Type beanRef(String ref, boolean cache) {
+        BeanDefinition answer = new BeanDefinition(ref);
+        answer.setCache(cache);
+        addOutput(answer);
+        return (Type) this;
+    }
+
+    /**
+     * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
+     * Adds a bean which is invoked which could be a final destination, or could be a transformation in a pipeline
+     *
+     * @param ref  reference to a bean to lookup in the registry
+     * @param method  the method name to invoke on the bean (can be used to avoid ambiguity)
+     * @param 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.
+     * @return the builder
+     */
+    @SuppressWarnings("unchecked")
+    public Type beanRef(String ref, String method, boolean cache) {
+        BeanDefinition answer = new BeanDefinition(ref, method);
+        answer.setCache(cache);
+        addOutput(answer);
+        return (Type) this;
+    }
+
+    /**
+     * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
      * Adds a processor which sets the body on the IN message
      *
      * @return a expression builder clause to set the body

http://git-wip-us.apache.org/repos/asf/camel/blob/c3078ad7/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
index 15c8778..ae0f9cf 100644
--- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
@@ -64,11 +64,11 @@ public class BeanExplicitMethodAmbiguousTest extends ContextTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:hello").beanRef("dummy", "hello");
+                from("direct:hello").beanRef("dummy", "hello", true);
 
-                from("direct:bye").beanRef("dummy");
+                from("direct:bye").beanRef("dummy", true);
                 
-                from("direct:foo").beanRef("dummy", "bar");
+                from("direct:foo").beanRef("dummy", "bar", true);
             }
         };
     }