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 2020/11/05 18:34:16 UTC

[camel] branch master updated: camel-core - Optimize to use single shared instance for error handlers in the routing engine.

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 45f1c64  camel-core - Optimize to use single shared instance for error handlers in the routing engine.
45f1c64 is described below

commit 45f1c64505a7e547923fa945f6eb0cfd57ef6a48
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Nov 5 19:24:38 2020 +0100

    camel-core - Optimize to use single shared instance for error handlers in the routing engine.
---
 .../errorhandler/DefaultExceptionPolicyStrategy.java        | 13 ++++++++-----
 .../camel/processor/errorhandler/ErrorHandlerSupport.java   | 10 ++--------
 2 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/DefaultExceptionPolicyStrategy.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/DefaultExceptionPolicyStrategy.java
index a8ab043..69c03467 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/DefaultExceptionPolicyStrategy.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/DefaultExceptionPolicyStrategy.java
@@ -53,6 +53,9 @@ import org.slf4j.LoggerFactory;
  */
 public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
 
+    // thread safe so we can use a shared instance
+    public static DefaultExceptionPolicyStrategy INSTANCE = new DefaultExceptionPolicyStrategy();
+
     private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionPolicyStrategy.class);
 
     @Override
@@ -96,7 +99,7 @@ public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
         }
     }
 
-    private void initRouteAndContextScopedExceptionPolicies(
+    private static void initRouteAndContextScopedExceptionPolicies(
             Set<ExceptionPolicyKey> exceptionPolicies,
             Set<ExceptionPolicyKey> routeScoped,
             Set<ExceptionPolicyKey> contextScoped) {
@@ -111,7 +114,7 @@ public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
         }
     }
 
-    private boolean findMatchedExceptionPolicy(
+    private static boolean findMatchedExceptionPolicy(
             Iterable<ExceptionPolicyKey> exceptionPolicies,
             Exchange exchange, Throwable exception,
             Map<Integer, ExceptionPolicyKey> candidates) {
@@ -201,7 +204,7 @@ public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
      * @param  exception      the thrown exception
      * @return                <tt>true</tt> if the to current exception class is a candidate, <tt>false</tt> to skip it.
      */
-    protected boolean filter(ExceptionPolicyKey type, Class<?> exceptionClass, Throwable exception) {
+    protected static boolean filter(ExceptionPolicyKey type, Class<?> exceptionClass, Throwable exception) {
         // must be instance of check to ensure that the exceptionClass is one type of the thrown exception
         return exceptionClass.isInstance(exception);
     }
@@ -219,7 +222,7 @@ public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
      * @param  exchange   the current {@link Exchange}
      * @return            <tt>true</tt> if matched, <tt>false</tt> otherwise.
      */
-    protected boolean matchesWhen(ExceptionPolicyKey definition, Exchange exchange) {
+    protected static boolean matchesWhen(ExceptionPolicyKey definition, Exchange exchange) {
         if (definition.getWhen() == null) {
             // if no predicate then it's always a match
             return true;
@@ -236,7 +239,7 @@ public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
      * @param  exception the exception
      * @return           the list to iterate
      */
-    protected Iterable<Throwable> createExceptionIterable(Throwable exception) {
+    protected static Iterable<Throwable> createExceptionIterable(Throwable exception) {
         return ObjectHelper.createExceptionIterable(exception);
     }
 
diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ErrorHandlerSupport.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ErrorHandlerSupport.java
index 172cc44..424934c 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ErrorHandlerSupport.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ErrorHandlerSupport.java
@@ -30,7 +30,8 @@ import org.apache.camel.support.ChildServiceSupport;
 public abstract class ErrorHandlerSupport extends ChildServiceSupport implements ErrorHandler {
 
     protected final Map<ExceptionPolicyKey, ExceptionPolicy> exceptionPolicies = new LinkedHashMap<>();
-    protected ExceptionPolicyStrategy exceptionPolicy = createDefaultExceptionPolicyStrategy();
+    // optimize to use a shared instance
+    protected ExceptionPolicyStrategy exceptionPolicy = DefaultExceptionPolicyStrategy.INSTANCE;
 
     public void addErrorHandler(Processor errorHandler) {
         addChildService(errorHandler);
@@ -67,13 +68,6 @@ public abstract class ErrorHandlerSupport extends ChildServiceSupport implements
     }
 
     /**
-     * Creates the default exception policy strategy to use.
-     */
-    public static ExceptionPolicyStrategy createDefaultExceptionPolicyStrategy() {
-        return new DefaultExceptionPolicyStrategy();
-    }
-
-    /**
      * Whether this error handler supports transacted exchanges or not.
      */
     public abstract boolean supportTransacted();