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 2022/10/20 09:10:36 UTC

[camel] branch camel-3.14.x updated: CAMEL-18421: camel-core - Adding route dynamic leak bootstraps

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

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


The following commit(s) were added to refs/heads/camel-3.14.x by this push:
     new 543342e28e2 CAMEL-18421: camel-core - Adding route dynamic leak bootstraps
543342e28e2 is described below

commit 543342e28e27aa5941bf8beec366049240117e73
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 23 19:08:03 2022 +0200

    CAMEL-18421: camel-core - Adding route dynamic leak bootstraps
---
 .../main/java/org/apache/camel/reifier/RouteReifier.java    | 10 ++++++----
 .../org/apache/camel/builder/AddRoutesAtRuntimeTest.java    | 13 +++++++++++++
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
index b13b98b2c5a..827d5bdcc1d 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java
@@ -31,6 +31,7 @@ import org.apache.camel.FailedToCreateRouteException;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.ServiceStatus;
 import org.apache.camel.ShutdownRoute;
 import org.apache.camel.ShutdownRunningTask;
 import org.apache.camel.StartupStep;
@@ -343,10 +344,11 @@ public class RouteReifier extends ProcessorReifier<RouteDefinition> {
                 builder, null);
         prepareErrorHandlerAware(route, errorHandler);
 
-        camelContext.adapt(ExtendedCamelContext.class).addBootstrap(() -> {
-            // okay route has been created from the model, then the model is no longer needed and we can de-reference
-            route.clearRouteModel();
-        });
+        // only during startup phase
+        if (camelContext.getStatus().ordinal() < ServiceStatus.Started.ordinal()) {
+            // okay route has been created from the model, then the model is no longer needed, and we can de-reference
+            camelContext.adapt(ExtendedCamelContext.class).addBootstrap(route::clearRouteModel);
+        }
 
         return route;
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java
index a591030fe0f..e28606849e9 100644
--- a/core/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java
@@ -16,8 +16,14 @@
  */
 package org.apache.camel.builder;
 
+import java.lang.reflect.Field;
+import java.util.List;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.impl.engine.AbstractCamelContext;
+import org.apache.camel.util.ReflectionHelper;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -40,11 +46,18 @@ public class AddRoutesAtRuntimeTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
         assertEquals(2, context.getRoutes().size());
 
+        // use reflection to test that we do not leak bootstraps when dynamic adding routes
+        Field f = AbstractCamelContext.class.getDeclaredField("bootstraps");
+        Assertions.assertEquals(0, ((List) ReflectionHelper.getField(f, context)).size());
+
         getMockEndpoint("mock:bar").expectedMessageCount(1);
         context.addRoutes(new MyDynamcRouteBuilder(context, "direct:bar", "mock:bar"));
         template.sendBody("direct:bar", "Hi Camel");
         assertMockEndpointsSatisfied();
         assertEquals(3, context.getRoutes().size());
+
+        // use reflection to test that we do not leak bootstraps when dynamic adding routes
+        Assertions.assertEquals(0, ((List) ReflectionHelper.getField(f, context)).size());
     }
 
     @Override