camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] camel git commit: CAMEL-8053 Fixed the issue of Memory leak when adding/removing a lot of routes
Date Thu, 20 Nov 2014 11:28:34 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.13.x 9d911ca4e -> 00c5178af
  refs/heads/camel-2.14.x 6d0b9b11f -> 622649db1


CAMEL-8053 Fixed the issue of Memory leak when adding/removing a lot of routes

Conflicts:
	camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java


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

Branch: refs/heads/camel-2.14.x
Commit: 622649db1ef50e74b36be4310326b3bcd3229597
Parents: 6d0b9b1
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Thu Nov 20 14:02:30 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu Nov 20 19:27:25 2014 +0800

----------------------------------------------------------------------
 .../camel/builder/ErrorHandlerBuilder.java      |  1 +
 .../camel/builder/ErrorHandlerBuilderRef.java   | 12 +++
 .../builder/ErrorHandlerBuilderSupport.java     | 25 ++++++
 .../apache/camel/impl/DefaultCamelContext.java  | 44 +++++-----
 .../builder/ErrorHandlerBuilderRefTest.java     | 87 ++++++++++++++++++++
 5 files changed, 150 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/622649db/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
index 303feb1..3c5a3f1 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
@@ -90,4 +90,5 @@ public interface ErrorHandlerBuilder extends ErrorHandlerFactory {
      * @return a clone of this {@link ErrorHandlerBuilder}
      */
     ErrorHandlerBuilder cloneBuilder();
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/622649db/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java
b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java
index 0cf9d57..4d06b7f 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java
@@ -50,6 +50,18 @@ public class ErrorHandlerBuilderRef extends ErrorHandlerBuilderSupport
{
         }
         super.addErrorHandlers(routeContext, exception);
     }
+    
+    @Override
+    public boolean removeOnExceptionList(String id) {
+        for (RouteContext routeContext : handlers.keySet()) {
+            if (getRouteId(routeContext).equals(id)) {
+                handlers.remove(routeContext);
+                break;
+            }
+        }
+        return super.removeOnExceptionList(id);
+    }
+    
 
     public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws
Exception {
         ErrorHandlerBuilder handler = handlers.get(routeContext);

http://git-wip-us.apache.org/repos/asf/camel/blob/622649db/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
index 747a6dd..365ebc0 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.processor.ErrorHandler;
 import org.apache.camel.processor.ErrorHandlerSupport;
@@ -94,4 +95,28 @@ public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder
         ObjectHelper.notNull(exceptionPolicyStrategy, "ExceptionPolicyStrategy");
         this.exceptionPolicyStrategy = exceptionPolicyStrategy;
     }
+    
+    /**
+     * Remove the OnExceptionList by look up the route id from the ErrorHandlerBuilder internal
map
+     * @param id the route id
+     * @return true if the route context is found and removed
+     */
+    public boolean removeOnExceptionList(String id) {
+        for (RouteContext routeContext : onExceptions.keySet()) {
+            if (getRouteId(routeContext).equals(id)) {
+                onExceptions.remove(routeContext);
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    protected String getRouteId(RouteContext routeContext) {
+        CamelContext context = routeContext.getCamelContext();
+        if (context != null) {
+            return routeContext.getRoute().idOrCreate(context.getNodeIdFactory());
+        } else {
+            return routeContext.getRoute().getId();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/622649db/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index ce4ab89..c0f8679 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -70,6 +70,7 @@ import org.apache.camel.SuspendableService;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.VetoCamelContextStartException;
 import org.apache.camel.builder.ErrorHandlerBuilder;
+import org.apache.camel.builder.ErrorHandlerBuilderSupport;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.impl.converter.BaseTypeConverterRegistry;
 import org.apache.camel.impl.converter.DefaultTypeConverter;
@@ -148,7 +149,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Represents the context used to configure routes and the policies to use.
  *
- * @version 
+ * @version
  */
 @SuppressWarnings("deprecation")
 public class DefaultCamelContext extends ServiceSupport implements ModelCamelContext, SuspendableService
{
@@ -565,7 +566,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         if (endpointType.isInstance(endpoint)) {
             return endpointType.cast(endpoint);
         } else {
-            throw new IllegalArgumentException("The endpoint is not of type: " + endpointType

+            throw new IllegalArgumentException("The endpoint is not of type: " + endpointType
                 + " but is: " + endpoint.getClass().getCanonicalName());
         }
     }
@@ -861,10 +862,10 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
                 startRouteService(routeService, false);
             }
             return completed;
-        } 
+        }
         return false;
     }
-    
+
     public synchronized void stopRoute(String routeId) throws Exception {
         RouteService routeService = routeServices.get(routeId);
         if (routeService != null) {
@@ -914,10 +915,15 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
             getShutdownStrategy().shutdown(this, routes, timeout, timeUnit);
             // must stop route service as well (and remove the routes from management)
             stopRouteService(routeService, true);
-        } 
+        }
     }
 
     public synchronized boolean removeRoute(String routeId) throws Exception {
+        // remove the route from ErrorHandlerBuilder if possible
+        if (getErrorHandlerBuilder() instanceof ErrorHandlerBuilderSupport) {
+            ErrorHandlerBuilderSupport builder = (ErrorHandlerBuilderSupport)getErrorHandlerBuilder();
+            builder.removeOnExceptionList(routeId);
+        }
         RouteService routeService = routeServices.get(routeId);
         if (routeService != null) {
             if (getRouteStatus(routeId).isStopped()) {
@@ -1220,20 +1226,20 @@ public class DefaultCamelContext extends ServiceSupport implements
ModelCamelCon
 
         return answer;
     }
-    
+
     public String getPropertyPrefixToken() {
         PropertiesComponent pc = getPropertiesComponent();
-        
+
         if (pc != null) {
             return pc.getPrefixToken();
         } else {
             return null;
         }
     }
-    
+
     public String getPropertySuffixToken() {
         PropertiesComponent pc = getPropertiesComponent();
-        
+
         if (pc != null) {
             return pc.getSuffixToken();
         } else {
@@ -1272,20 +1278,20 @@ public class DefaultCamelContext extends ServiceSupport implements
ModelCamelCon
                     throw new IllegalArgumentException("PropertiesComponent with name properties
must be defined"
                             + " in CamelContext to support property placeholders.");
                 }
-                
+
             // Component available, use actual tokens
             } else if (pc != null && text.contains(pc.getPrefixToken())) {
                 // the parser will throw exception if property key was not found
                 String answer = pc.parseUri(text);
                 log.debug("Resolved text: {} -> {}", text, answer);
-                return answer; 
+                return answer;
             }
         }
 
         // return original text as is
         return text;
     }
-    
+
     // Properties
     // -----------------------------------------------------------------------
 
@@ -1833,7 +1839,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         if (getDelayer() != null && getDelayer() > 0) {
             log.info("Delayer is enabled with: {} ms. on CamelContext: {}", getDelayer(),
getName());
         }
-        
+
         // register debugger
         if (getDebugger() != null) {
             log.info("Debugger: {} is enabled on CamelContext: {}", getDebugger(), getName());
@@ -2166,7 +2172,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
 
         service.start();
     }
-    
+
     private void startServices(Collection<?> services) throws Exception {
         for (Object element : services) {
             if (element instanceof Service) {
@@ -2252,7 +2258,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         routeService.setRemovingRoutes(removingRoutes);
         stopRouteService(routeService);
     }
-    
+
     protected void logRouteState(Route route, String state) {
         if (log.isInfoEnabled()) {
             if (route.getConsumer() != null) {
@@ -2262,7 +2268,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
             }
         }
     }
-    
+
     protected synchronized void stopRouteService(RouteService routeService) throws Exception
{
         routeService.stop();
         for (Route route : routeService.getRoutes()) {
@@ -2421,7 +2427,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
                     throw new FailedToStartRouteException(routeService.getId(),
                         "Multiple consumers for the same endpoint is not allowed: " + endpoint);
                 }
-                
+
                 // check for multiple consumer violations with existing routes which
                 // have already been started, or is currently starting
                 List<Endpoint> existingEndpoints = new ArrayList<Endpoint>();
@@ -2629,7 +2635,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
     protected boolean shouldStartRoutes() {
         return isStarted() && !isStarting();
     }
-    
+
     /**
      * Gets the properties component in use.
      * Returns {@code null} if no properties component is in use.
@@ -2939,7 +2945,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
     public void setDebugger(Debugger debugger) {
         this.debugger = debugger;
     }
-    
+
     public UuidGenerator getUuidGenerator() {
         return uuidGenerator;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/622649db/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerBuilderRefTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerBuilderRefTest.java
b/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerBuilderRefTest.java
new file mode 100644
index 0000000..047242c
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerBuilderRefTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.builder;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.impl.JndiRegistry;
+
+public class ErrorHandlerBuilderRefTest extends ContextTestSupport {
+    ErrorHandlerBuilderRef errorHandlerBuilderRef = new ErrorHandlerBuilderRef("ref");
+    
+    @Override
+    public boolean isUseRouteBuilder() {
+        return true;
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        registry.bind("ref", new DefaultErrorHandlerBuilder());
+        return registry;
+    }
+    
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.setErrorHandlerBuilder(errorHandlerBuilderRef);
+        return context;
+    }
+    
+    public void testErrorHandlerBuilderRef() throws Exception {
+        String uuid = UUID.randomUUID().toString();
+        context.addRoutes(new TempRouteBuilder(uuid));
+        checkObjectSize(2);
+        context.stopRoute(uuid);
+        context.removeRoute(uuid);
+        checkObjectSize(1);
+    }
+    
+    private void checkObjectSize(int size) throws Exception {
+        assertEquals("Get a wrong size of Route", size, context.getRoutes().size());
+        Field field = ErrorHandlerBuilderRef.class.getDeclaredField("handlers");
+        field.setAccessible(true);
+        assertEquals("Get a wrong size of ErrorHandler", size, ((Map<?, ?>) field.get(errorHandlerBuilderRef)).size());
+    }
+    
+    private static class TempRouteBuilder extends RouteBuilder {
+
+        final String routeId;
+
+        public TempRouteBuilder(String routeId) {
+            this.routeId = routeId;
+        }
+
+        @Override
+        public void configure() throws Exception {
+            from("direct:foo").routeId(routeId).to("mock:foo");
+        }
+    }
+    
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("mock:result");
+            }
+        };
+    }
+
+}


Mime
View raw message