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/05/08 10:59:28 UTC

[1/2] git commit: CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine DESSAIGNE for the patch.

Updated Branches:
  refs/heads/camel-2.10.x c6ec31990 -> fe19edcb1
  refs/heads/camel-2.11.x 15aa6f0a7 -> 178294d85


CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine DESSAIGNE for the patch.


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

Branch: refs/heads/camel-2.11.x
Commit: 178294d85fbbe61a8a9c9921251035b234347f55
Parents: 15aa6f0
Author: Claus Ibsen <da...@apache.org>
Authored: Wed May 8 10:52:57 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed May 8 10:53:22 2013 +0200

----------------------------------------------------------------------
 .../camel/language/groovy/GroovyExpression.java    |   54 ++++++++------
 .../camel/language/groovy/GroovyLanguage.java      |   15 ++++-
 2 files changed, 45 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/178294d8/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
index 3f41f9e..616e3ee 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
@@ -16,14 +16,14 @@
  */
 package org.apache.camel.language.groovy;
 
-import java.util.AbstractMap;
-import java.util.Collections;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
 import groovy.lang.Script;
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.ExpressionSupport;
 import org.apache.camel.util.ExchangeHelper;
 
@@ -47,30 +47,38 @@ public class GroovyExpression extends ExpressionSupport {
     }
 
     public <T> T evaluate(Exchange exchange, Class<T> type) {
-        // use application classloader if given
-        ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
-        GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
-
-        // need to re-parse script due thread-safe with binding
-        Script script = shell.parse(text);
-        configure(exchange, script.getBinding());
-        Object value = script.evaluate(text);
+        Script script = instantiateScript(exchange);
+        script.setBinding(createBinding(exchange));
+        Object value = script.run();
 
         return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 
-    private void configure(Exchange exchange, final Binding binding) {
-        ExchangeHelper.populateVariableMap(exchange, new AbstractMap<String, Object>() {
-            @Override
-            public Object put(String key, Object value) {
-                binding.setProperty(key, value);
-                return null;
-            }
+    @SuppressWarnings("unchecked")
+    private Script instantiateScript(Exchange exchange) {
+        // Get the script from the cache, or create a new instance
+        GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
+        Class<Script> scriptClass = language.getScriptFromCache(text);
+        if (scriptClass == null) {
+            ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
+            GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
+            scriptClass = shell.getClassLoader().parseClass(text);
+            language.addScriptToCache(text, scriptClass);
+        }
+
+        // New instance of the script
+        try {
+            return scriptClass.newInstance();
+        } catch (InstantiationException e) {
+            throw new RuntimeCamelException(e);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
 
-            @Override
-            public Set<Entry<String, Object>> entrySet() {
-                return Collections.emptySet();
-            }
-        });
+    private Binding createBinding(Exchange exchange) {
+        Map<String, Object> variables = new HashMap<String, Object>();
+        ExchangeHelper.populateVariableMap(exchange, variables);
+        return new Binding(variables);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/178294d8/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
index a26a0f5..a82cd0f 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
@@ -16,13 +16,18 @@
  */
 package org.apache.camel.language.groovy;
 
+import groovy.lang.Script;
 import org.apache.camel.support.LanguageSupport;
+import org.apache.camel.util.LRUSoftCache;
 
 /**
- * @version 
+ * @version
  */
 public class GroovyLanguage extends LanguageSupport {
 
+    // Cache used to stores the compiled scripts (aka their classes)
+    private final LRUSoftCache<String, Class<Script>> scriptCache = new LRUSoftCache<String, Class<Script>>(1000);
+
     public static GroovyExpression groovy(String expression) {
         return new GroovyLanguage().createExpression(expression);
     }
@@ -36,4 +41,12 @@ public class GroovyLanguage extends LanguageSupport {
         return new GroovyExpression(expression);
     }
 
+    Class<Script> getScriptFromCache(String script) {
+        return scriptCache.get(script);
+    }
+
+    void addScriptToCache(String script, Class<Script> scriptClass) {
+        scriptCache.put(script, scriptClass);
+    }
+
 }


[2/2] git commit: CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine DESSAIGNE for the patch.

Posted by da...@apache.org.
CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine DESSAIGNE for the patch.


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

Branch: refs/heads/camel-2.10.x
Commit: fe19edcb1988b6ecb664eb657c45eff7fc5cbf0d
Parents: c6ec319
Author: Claus Ibsen <da...@apache.org>
Authored: Wed May 8 10:52:57 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed May 8 10:59:21 2013 +0200

----------------------------------------------------------------------
 .../camel/language/groovy/GroovyExpression.java    |   54 ++++++++------
 .../camel/language/groovy/GroovyLanguage.java      |   17 ++++-
 2 files changed, 47 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/fe19edcb/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
index 3f41f9e..616e3ee 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
@@ -16,14 +16,14 @@
  */
 package org.apache.camel.language.groovy;
 
-import java.util.AbstractMap;
-import java.util.Collections;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
 import groovy.lang.Script;
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.ExpressionSupport;
 import org.apache.camel.util.ExchangeHelper;
 
@@ -47,30 +47,38 @@ public class GroovyExpression extends ExpressionSupport {
     }
 
     public <T> T evaluate(Exchange exchange, Class<T> type) {
-        // use application classloader if given
-        ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
-        GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
-
-        // need to re-parse script due thread-safe with binding
-        Script script = shell.parse(text);
-        configure(exchange, script.getBinding());
-        Object value = script.evaluate(text);
+        Script script = instantiateScript(exchange);
+        script.setBinding(createBinding(exchange));
+        Object value = script.run();
 
         return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 
-    private void configure(Exchange exchange, final Binding binding) {
-        ExchangeHelper.populateVariableMap(exchange, new AbstractMap<String, Object>() {
-            @Override
-            public Object put(String key, Object value) {
-                binding.setProperty(key, value);
-                return null;
-            }
+    @SuppressWarnings("unchecked")
+    private Script instantiateScript(Exchange exchange) {
+        // Get the script from the cache, or create a new instance
+        GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
+        Class<Script> scriptClass = language.getScriptFromCache(text);
+        if (scriptClass == null) {
+            ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
+            GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
+            scriptClass = shell.getClassLoader().parseClass(text);
+            language.addScriptToCache(text, scriptClass);
+        }
+
+        // New instance of the script
+        try {
+            return scriptClass.newInstance();
+        } catch (InstantiationException e) {
+            throw new RuntimeCamelException(e);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
 
-            @Override
-            public Set<Entry<String, Object>> entrySet() {
-                return Collections.emptySet();
-            }
-        });
+    private Binding createBinding(Exchange exchange) {
+        Map<String, Object> variables = new HashMap<String, Object>();
+        ExchangeHelper.populateVariableMap(exchange, variables);
+        return new Binding(variables);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/fe19edcb/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
index 241c4fb..73d89a4 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
@@ -18,12 +18,18 @@ package org.apache.camel.language.groovy;
 
 import org.apache.camel.IsSingleton;
 import org.apache.camel.spi.Language;
+import org.apache.camel.util.LRUSoftCache;
+
+import groovy.lang.Script;
 
 /**
- * @version 
+ * @version
  */
 public class GroovyLanguage implements Language, IsSingleton {
 
+    // Cache used to stores the compiled scripts (aka their classes)
+    private final LRUSoftCache<String, Class<Script>> scriptCache = new LRUSoftCache<String, Class<Script>>(1000);
+
     public static GroovyExpression groovy(String expression) {
         return new GroovyLanguage().createExpression(expression);
     }
@@ -39,4 +45,13 @@ public class GroovyLanguage implements Language, IsSingleton {
     public boolean isSingleton() {
         return true;
     }
+
+    Class<Script> getScriptFromCache(String script) {
+        return scriptCache.get(script);
+    }
+
+    void addScriptToCache(String script, Class<Script> scriptClass) {
+        scriptCache.put(script, scriptClass);
+    }
+
 }