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

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


The following commit(s) were added to refs/heads/main by this push:
     new 23590accdc99 CAMEL-26893: camel-core - Gather the endpoints in use 
only when a route is removed (#30255)
23590accdc99 is described below

commit 23590accdc9908bee413f8429d1406f476b7a948
Author: Ayush Kulshreshtha <[email protected]>
AuthorDate: Tue Nov 17 12:35:27 2026 +0530

    CAMEL-26893: camel-core - Gather the endpoints in use only when a route is 
removed (#30255)
    
    removeRoute() gathered the endpoints of every route before it looked
    up the route to remove, and threw the map away when the id was unknown
    or the route was still started. Adding a route with an explicit id
    calls removeRoute() for that id, so adding n routes with ids walked the
    endpoints of all routes already in the context each time: O(n^2).
    
    Gather the endpoints only when a stopped route is actually removed,
    still before it is shut down.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../camel/impl/engine/AbstractCamelContext.java    |  17 ++--
 .../camel/impl/RouteRemoveGatherEndpointsTest.java | 113 +++++++++++++++++++++
 2 files changed, 122 insertions(+), 8 deletions(-)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index b6b092313e57..bc45c3fcfd7c 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -1648,17 +1648,18 @@ public abstract class AbstractCamelContext extends 
BaseService
         try {
             DefaultRouteError.reset(this, routeId);
 
-            // gather a map of all the endpoints in use by the routes, so we 
can
-            // known if a given endpoints is in use
-            // by one or more routes, when we remove the route
-            Map<String, Set<Endpoint>> endpointsInUse = new HashMap<>();
-            for (Map.Entry<String, RouteService> entry : 
routeServices.entrySet()) {
-                endpointsInUse.put(entry.getKey(), 
entry.getValue().gatherEndpoints());
-            }
-
             RouteService routeService = routeServices.get(routeId);
             if (routeService != null) {
                 if (getRouteStatus(routeId).isStopped()) {
+                    // gather a map of all the endpoints in use by the routes, 
so we can
+                    // know if a given endpoint is in use by one or more 
routes, when we
+                    // remove the route. This walks every route, so it is only 
done when a
+                    // route is actually removed: adding a route with an 
explicit id calls
+                    // this method too, for an id that is usually not in use.
+                    Map<String, Set<Endpoint>> endpointsInUse = new 
HashMap<>();
+                    for (Map.Entry<String, RouteService> entry : 
routeServices.entrySet()) {
+                        endpointsInUse.put(entry.getKey(), 
entry.getValue().gatherEndpoints());
+                    }
                     try {
                         doRemove(routeId, loggingLevel, routeService, 
endpointsInUse);
                     } catch (Exception e) {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/RouteRemoveGatherEndpointsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/RouteRemoveGatherEndpointsTest.java
new file mode 100644
index 000000000000..164d9e9ef70a
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/RouteRemoveGatherEndpointsTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.impl;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.EndpointAware;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Service;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Removing a route walks the endpoints of every route in the context. That 
must only happen when a route is actually
+ * removed, not when the route id is unknown or the route is still started, as 
when routes with explicit ids are added.
+ */
+public class RouteRemoveGatherEndpointsTest extends ContextTestSupport {
+
+    private final CountingEndpointAware counting = new CountingEndpointAware();
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        super.setUp();
+        // ignore what starting the context did
+        counting.calls.set(0);
+    }
+
+    @Test
+    public void testRemoveUnknownOrStartedRouteDoesNotGatherEndpoints() throws 
Exception {
+        assertFalse(context.removeRoute("unknown"));
+        assertEquals(0, counting.calls.get(), "endpoints gathered for an 
unknown route id");
+
+        assertFalse(context.removeRoute("foo"));
+        assertEquals(0, counting.calls.get(), "endpoints gathered for a 
started route");
+
+        context.getRouteController().stopRoute("foo");
+        assertTrue(context.removeRoute("foo"));
+        assertTrue(counting.calls.get() > 0, "endpoints not gathered when the 
route was removed");
+        assertEquals(0, context.getRoutes().size());
+    }
+
+    @Test
+    public void testAddRouteWithIdDoesNotGatherEndpointsOfOtherRoutes() throws 
Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:bar").routeId("bar").to("mock:bar");
+            }
+        });
+
+        assertEquals(2, context.getRoutes().size());
+        assertEquals(0, counting.calls.get(), "endpoints of the existing route 
gathered when adding a route");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("direct:foo").routeId("foo").process(counting).to("mock:foo");
+            }
+        };
+    }
+
+    private static final class CountingEndpointAware implements Processor, 
EndpointAware, Service {
+
+        private final AtomicInteger calls = new AtomicInteger();
+
+        @Override
+        public Endpoint getEndpoint() {
+            calls.incrementAndGet();
+            return null;
+        }
+
+        @Override
+        public void process(Exchange exchange) {
+            // noop
+        }
+
+        @Override
+        public void start() {
+            // noop
+        }
+
+        @Override
+        public void stop() {
+            // noop
+        }
+    }
+}

Reply via email to