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 718427c367d3 CAMEL-28096: camel-core - a lookup of a route
configuration no longer makes global route configurations non-global (#31334)
718427c367d3 is described below
commit 718427c367d3e533dbe55e5180945095780d369d
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Dec 4 13:10:47 2026 +0100
CAMEL-28096: camel-core - a lookup of a route configuration no longer makes
global route configurations non-global (#31334)
DefaultModel.getRouteConfigurationDefinition called idOrCreate on every
route
configuration while searching, which gave global (id-less) configurations a
generated id. Routes added afterwards without routeConfigurationId no longer
got them, removeRouteConfiguration of a global configuration removed
nothing,
and every route reload (updateRouteConfigurationsToCamelContext) leaked a
copy.
Compare the ids without assigning one, and remove a route configuration by
identity, or else the one with the same id (for a global configuration, the
global one from the same resource).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../java/org/apache/camel/impl/DefaultModel.java | 42 ++++-
.../main/java/org/apache/camel/model/Model.java | 8 +-
.../model/RoutesConfigurationGlobalUpdateTest.java | 183 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc | 13 ++
4 files changed, 237 insertions(+), 9 deletions(-)
diff --git
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
index 1f84b2491194..93b0b4bcedcd 100644
---
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
+++
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -176,14 +176,15 @@ public class DefaultModel implements Model {
public RouteConfigurationDefinition getRouteConfigurationDefinition(String
id) {
lock.lock();
try {
+ // compare the ids as they are: do not assign an id (idOrCreate)
as that would turn a global
+ // route configuration (no id) into a named configuration, which
routes without
+ // routeConfigurationId no longer use. A null id finds the first
global route configuration.
for (RouteConfigurationDefinition def : routesConfigurations) {
- if
(def.idOrCreate(camelContext.getCamelContextExtension().getContextPlugin(NodeIdFactory.class))
- .equals(id)) {
+ if (ObjectHelper.equal(id, def.getId())) {
return def;
}
}
- // you can have a global route configuration that has no ID
assigned
- return routesConfigurations.stream().filter(c -> c.getId() ==
null).findFirst().orElse(null);
+ return null;
} finally {
lock.unlock();
}
@@ -191,9 +192,36 @@ public class DefaultModel implements Model {
@Override
public void removeRouteConfiguration(RouteConfigurationDefinition
routeConfigurationDefinition) throws Exception {
- RouteConfigurationDefinition toBeRemoved =
getRouteConfigurationDefinition(
- routeConfigurationDefinition.getId());
- this.routesConfigurations.remove(toBeRemoved);
+ if (routeConfigurationDefinition == null) {
+ return;
+ }
+ lock.lock();
+ try {
+ // remove the same instance
+ if (routesConfigurations.removeIf(def -> def ==
routeConfigurationDefinition)) {
+ return;
+ }
+ // otherwise remove the configuration it replaces: the one with
the same id, or for a global
+ // route configuration (no id) the global one from the same
resource
+ String id = routeConfigurationDefinition.getId();
+ String location = resourceLocation(routeConfigurationDefinition);
+ for (RouteConfigurationDefinition def : routesConfigurations) {
+ boolean match = id != null
+ ? id.equals(def.getId())
+ : def.getId() == null && ObjectHelper.equal(location,
resourceLocation(def));
+ if (match) {
+ routesConfigurations.remove(def);
+ return;
+ }
+ }
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ private static String resourceLocation(RouteConfigurationDefinition def) {
+ Resource resource = def.getResource();
+ return resource != null ? resource.getLocation() : null;
}
@Override
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
index bfd2482bb683..25a053bf8506 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
@@ -79,7 +79,10 @@ public interface Model {
List<RouteConfigurationDefinition> getRouteConfigurationDefinitions();
/**
- * Removes a route configuration from the context
+ * Removes a route configuration from the context.
+ * <p/>
+ * Removes the given instance, or else the route configuration it
replaces: the one with the same id, or for a
+ * global route configuration (without an id) the global route
configuration from the same resource.
*
* @param routeConfigurationDefinition route configuration to remove
* @throws Exception if the route configuration could
not be removed for whatever reason
@@ -89,7 +92,8 @@ public interface Model {
/**
* Gets the route configuration definition with the given id
*
- * @param id id of the route configuration
+ * @param id id of the route configuration, or <code>null</code> for the
first global route configuration (without
+ * an id)
* @return the route configuration definition or <code>null</code> if
not found
*/
RouteConfigurationDefinition getRouteConfigurationDefinition(String id);
diff --git
a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationGlobalUpdateTest.java
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationGlobalUpdateTest.java
new file mode 100644
index 000000000000..83fad6e242c4
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationGlobalUpdateTest.java
@@ -0,0 +1,183 @@
+/*
+ * 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.model;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.support.ResourceHelper;
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Global route configurations (without an id) must stay global when route
configurations are looked up, updated and
+ * removed.
+ */
+public class RoutesConfigurationGlobalUpdateTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ private static RouteConfigurationBuilder configuration(String id, String
uri) {
+ return configuration(id, uri, null);
+ }
+
+ private static RouteConfigurationBuilder configuration(String id, String
uri, Resource resource) {
+ return new RouteConfigurationBuilder() {
+ @Override
+ public void configuration() {
+ RouteConfigurationDefinition rc = id != null ?
routeConfiguration(id) : routeConfiguration();
+ rc.setResource(resource);
+
rc.onException(IllegalArgumentException.class).handled(true).to(uri);
+ }
+ };
+ }
+
+ private static RouteBuilder failingRoute(String routeId, String
routeConfigurationId) {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ RouteDefinition route = from("direct:" +
routeId).routeId(routeId);
+ if (routeConfigurationId != null) {
+ route.routeConfigurationId(routeConfigurationId);
+ }
+ route.throwException(new IllegalArgumentException("Forced"));
+ }
+ };
+ }
+
+ @Test
+ public void testLookupDoesNotChangeGlobalConfiguration() throws Exception {
+ context.addRoutes(configuration(null, "mock:global"));
+ context.start();
+
+ assertNull(context.getRouteConfigurationDefinition("doesNotExist"));
+ RouteConfigurationDefinition global =
context.getRouteConfigurationDefinitions().get(0);
+ assertNull(global.getId(), "A lookup must not assign an id to a global
route configuration");
+ assertSame(global, context.getRouteConfigurationDefinition(null));
+
+ // a route added after the lookup still uses the global route
configuration
+ context.addRoutes(failingRoute("a", null));
+ getMockEndpoint("mock:global").expectedBodiesReceived("Hello");
+ template.sendBody("direct:a", "Hello");
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testRemoveGlobalConfiguration() throws Exception {
+ context.addRoutes(configuration(null, "mock:global"));
+ context.start();
+
+ RouteConfigurationDefinition global =
context.getRouteConfigurationDefinitions().get(0);
+ context.removeRouteConfiguration(global);
+ assertTrue(context.getRouteConfigurationDefinitions().isEmpty());
+
+ // a route added afterwards has no route configuration
+ context.addRoutes(failingRoute("a", null));
+ getMockEndpoint("mock:global").expectedMessageCount(0);
+ Exchange out = template.send("direct:a", e ->
e.getMessage().setBody("Hello"));
+ assertInstanceOf(IllegalArgumentException.class, out.getException());
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testRemoveGlobalConfigurationByAnotherInstance() throws
Exception {
+ context.addRoutes(configuration(null, "mock:global"));
+ context.addRoutes(configuration("myConfig", "mock:myConfig"));
+ context.start();
+
+ // remove by a global route configuration that is not in the model (as
a reload does)
+ context.removeRouteConfiguration(new RouteConfigurationDefinition());
+ assertEquals(1, context.getRouteConfigurationDefinitions().size());
+ assertEquals("myConfig",
context.getRouteConfigurationDefinitions().get(0).getId());
+ }
+
+ @Test
+ public void testUpdateGlobalConfiguration() throws Exception {
+ context.addRoutes(configuration(null, "mock:global"));
+ context.start();
+
+ configuration(null,
"mock:global2").updateRouteConfigurationsToCamelContext(context);
+ assertEquals(1, context.getRouteConfigurationDefinitions().size(),
"The global configuration is replaced");
+ assertNull(context.getRouteConfigurationDefinitions().get(0).getId());
+
+ configuration(null,
"mock:global3").updateRouteConfigurationsToCamelContext(context);
+ assertEquals(1, context.getRouteConfigurationDefinitions().size(),
"The global configuration is replaced");
+
+ context.addRoutes(failingRoute("a", null));
+ getMockEndpoint("mock:global").expectedMessageCount(0);
+ getMockEndpoint("mock:global2").expectedMessageCount(0);
+ getMockEndpoint("mock:global3").expectedBodiesReceived("Hello");
+ template.sendBody("direct:a", "Hello");
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testUpdateGlobalConfigurationOfSameResource() throws Exception
{
+ Resource a = ResourceHelper.fromString("mem:a.yaml", "");
+ Resource b = ResourceHelper.fromString("mem:b.yaml", "");
+ context.addRoutes(configuration(null, "mock:a", a));
+ context.addRoutes(configuration(null, "mock:b", b));
+ context.start();
+
+ // only the global configuration of resource a is replaced
+ configuration(null, "mock:a2",
a).updateRouteConfigurationsToCamelContext(context);
+ assertEquals(2, context.getRouteConfigurationDefinitions().size());
+ RouteConfigurationDefinition first =
context.getRouteConfigurationDefinitions().get(0);
+ RouteConfigurationDefinition second =
context.getRouteConfigurationDefinitions().get(1);
+ assertEquals("mem:b.yaml", first.getResource().getLocation());
+ assertEquals("mem:a.yaml", second.getResource().getLocation());
+ assertEquals("mock:a2", ((ToDefinition)
second.getOnExceptions().get(0).getOutputs().get(0)).getUri());
+ }
+
+ @Test
+ public void testUpdateNamedConfigurationKeepsGlobal() throws Exception {
+ context.addRoutes(configuration(null, "mock:global"));
+ context.addRoutes(configuration("myConfig", "mock:myConfig"));
+ context.addRoutes(failingRoute("a", null));
+ context.start();
+
+ // a reload of the file with the named configuration
+ configuration("myConfig",
"mock:myConfig2").updateRouteConfigurationsToCamelContext(context);
+ assertEquals(2, context.getRouteConfigurationDefinitions().size());
+
assertFalse(context.getRouteConfigurationDefinitions().stream().anyMatch(rc ->
rc.getId() != null
+ && !rc.getId().equals("myConfig")),
+ "The global configuration must not get an id");
+
+ // the routes are added again after the reload
+ context.getRouteController().removeAllRoutes();
+ context.addRoutes(failingRoute("a", null));
+ context.addRoutes(failingRoute("b", "myConfig"));
+
+ getMockEndpoint("mock:global").expectedBodiesReceived("Hello a");
+ getMockEndpoint("mock:myConfig").expectedMessageCount(0);
+ getMockEndpoint("mock:myConfig2").expectedBodiesReceived("Hello b");
+ template.sendBody("direct:a", "Hello a");
+ template.sendBody("direct:b", "Hello b");
+ assertMockEndpointsSatisfied();
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index 585a2e1e82f3..518760e0ae02 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -725,3 +725,16 @@ route a second time. The advised route had each
`onException`, `intercept`, `int
configuration ran more than once per step or call. They are now in the advised
route once, and run once, as in the
route without `adviceWith`. A test that expects the higher counts on the mock
endpoints of such an intercept must be
updated.
+
+=== camel-core - global route configurations are replaced and removed
+
+A global route configuration (one without an id) now stays global when route
configurations are looked up by id
+(`getRouteConfigurationDefinition`). Before, the lookup gave it a generated id
such as `routesConfiguration1`, so
+routes added later without a `routeConfigurationId` no longer got it. This
also happened on every route reload of a
+file with a route configuration.
+
+As a result, reloading a file with a global route configuration now replaces
the old one, and
+`removeRouteConfiguration` of a global route configuration removes it. Before,
the old copy stayed in the list under a
+generated id, and still applied to routes with `routeConfigurationId="*"` or a
pattern.
+`getRouteConfigurationDefinition` of an id that does not exist returns `null`,
and `getRouteConfigurationDefinition(null)`
+returns the first global route configuration.