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

davsclaus pushed a commit to branch 
CAMEL-28097-add-route-definition-route-configurations
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f8e845d7da1d1cb6de8372b730a0680543c957b8
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Dec 3 22:22:41 2026 +0100

    CAMEL-28097: camel-core - routes added with addRouteDefinition get the 
route configurations
    
    A route added with ModelCamelContext.addRouteDefinition(s) that was not
    prepared yet (also JMX ManagedRoute.updateRouteFromXml) was prepared with
    RouteDefinitionHelper.prepareRoute, which does not merge the route
    configurations. Prepare it with a RoutesDefinition instead, as RouteBuilder
    and route templates do, so it gets the global or the named route
    configurations. Already prepared routes are unchanged.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../org/apache/camel/impl/DefaultCamelContext.java |   9 +-
 .../RoutesConfigurationAddRouteDefinitionTest.java | 149 +++++++++++++++++++++
 ...teUpdateRouteFromXmlRouteConfigurationTest.java | 117 ++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |  14 ++
 4 files changed, 287 insertions(+), 2 deletions(-)

diff --git 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 388310c8f09b..bd5bdffac775 100644
--- 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -53,6 +53,7 @@ import org.apache.camel.model.RouteConfigurationDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RouteDefinitionHelper;
 import org.apache.camel.model.RouteTemplateDefinition;
+import org.apache.camel.model.RoutesDefinition;
 import org.apache.camel.model.TemplatedRouteDefinition;
 import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.rest.RestDefinition;
@@ -768,8 +769,12 @@ public class DefaultCamelContext extends 
SimpleCamelContext implements ModelCame
                 if (includedRoute(routeDefinition)) {
                     // must ensure route is prepared, before we can start it
                     if (!routeDefinition.isPrepared()) {
-                        
RouteDefinitionHelper.prepareRoute(getCamelContextReference(), routeDefinition);
-                        routeDefinition.markPrepared();
+                        // prepare the route with the route configurations of 
the CamelContext, as a RouteBuilder does
+                        // (the route keeps its resource, and the route 
collection marks the route as prepared)
+                        RoutesDefinition routes = new RoutesDefinition();
+                        routes.setCamelContext(getCamelContextReference());
+                        routes.setResource(routeDefinition.getResource());
+                        routes.prepareRoute(routeDefinition);
                     }
                     // force the creation of ids on all nodes in the route
                     RouteDefinitionHelper.forceAssignIds(this, 
routeDefinition.getInput());
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationAddRouteDefinitionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationAddRouteDefinitionTest.java
new file mode 100644
index 000000000000..d10a7714be26
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationAddRouteDefinitionTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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 java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.DeadLetterChannelBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A route added with {@link 
ModelCamelContext#addRouteDefinition(RouteDefinition)}, that is not prepared 
yet, gets the
+ * route configurations of the CamelContext (CAMEL-28097).
+ */
+public class RoutesConfigurationAddRouteDefinitionTest extends 
ContextTestSupport {
+
+    @Test
+    public void testAddRouteDefinitionGlobalConfiguration() throws Exception {
+        RouteDefinition route = new RouteDefinition();
+        route.from("direct:added").routeId("added")
+                .throwException(new IllegalArgumentException("Forced"));
+        context.addRouteDefinition(route);
+
+        getMockEndpoint("mock:global").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:named").expectedMessageCount(0);
+
+        template.sendBody("direct:added", "Hello World");
+
+        assertMockEndpointsSatisfied();
+        assertEquals(List.of("<default>"), appliedIds("added"));
+        assertEquals(1, onExceptions("added"));
+    }
+
+    @Test
+    public void testAddRouteDefinitionNamedConfiguration() throws Exception {
+        RouteDefinition route = new RouteDefinition();
+        
route.from("direct:addedNamed").routeId("addedNamed").routeConfigurationId("named")
+                .throwException(new IllegalArgumentException("Forced"));
+        context.addRouteDefinition(route);
+
+        getMockEndpoint("mock:named").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:global").expectedMessageCount(0);
+
+        template.sendBody("direct:addedNamed", "Bye World");
+
+        assertMockEndpointsSatisfied();
+        assertEquals(List.of("named"), appliedIds("addedNamed"));
+        assertEquals(1, onExceptions("addedNamed"));
+    }
+
+    @Test
+    public void testAddRouteDefinitionOwnErrorHandlerWins() throws Exception {
+        RouteDefinition route = new RouteDefinition();
+        
route.from("direct:addedOwn").routeId("addedOwn").routeConfigurationId("dlc")
+                .errorHandler(new DeadLetterChannelBuilder("mock:own"))
+                .throwException(new IllegalArgumentException("Forced"));
+        context.addRouteDefinition(route);
+
+        getMockEndpoint("mock:own").expectedBodiesReceived("Hi World");
+        getMockEndpoint("mock:dlc").expectedMessageCount(0);
+
+        template.sendBody("direct:addedOwn", "Hi World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testAddRouteDefinitionConfigurationErrorHandler() throws 
Exception {
+        RouteDefinition route = new RouteDefinition();
+        
route.from("direct:addedDlc").routeId("addedDlc").routeConfigurationId("dlc")
+                .throwException(new IllegalArgumentException("Forced"));
+        context.addRouteDefinition(route);
+
+        getMockEndpoint("mock:dlc").expectedBodiesReceived("Hi World");
+
+        template.sendBody("direct:addedDlc", "Hi World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testReAddPreparedRouteNotMergedTwice() throws Exception {
+        RouteDefinition route = context.getRouteDefinition("builder");
+        assertEquals(1, onExceptions("builder"));
+
+        // the route from the route builder is prepared already, so adding it 
again does not merge again
+        context.removeRouteDefinition(route);
+        context.addRouteDefinition(route);
+
+        getMockEndpoint("mock:global").expectedBodiesReceived("Hello Again");
+
+        template.sendBody("direct:builder", "Hello Again");
+
+        assertMockEndpointsSatisfied();
+        assertEquals(1, onExceptions("builder"));
+    }
+
+    private List<String> appliedIds(String routeId) {
+        return 
List.copyOf(context.getRouteDefinition(routeId).getAppliedRouteConfigurationIds());
+    }
+
+    private long onExceptions(String routeId) {
+        return context.getRouteDefinition(routeId).getOutputs().stream()
+                .filter(OnExceptionDefinition.class::isInstance)
+                .count();
+    }
+
+    @Override
+    protected RouteBuilder[] createRouteBuilders() {
+        return new RouteBuilder[] {
+                new RouteConfigurationBuilder() {
+                    @Override
+                    public void configuration() {
+                        // global route configuration (no id)
+                        
routeConfiguration().onException(Exception.class).handled(true).to("mock:global");
+                        // a route configuration with an id applies only to 
the routes that select it
+                        
routeConfiguration("named").onException(Exception.class).handled(true).to("mock:named");
+                        
routeConfiguration("dlc").errorHandler(deadLetterChannel("mock:dlc"));
+                    }
+                },
+                new RouteBuilder() {
+                    @Override
+                    public void configure() {
+                        from("direct:builder").routeId("builder")
+                                .throwException(new 
IllegalArgumentException("Forced"));
+                    }
+                }
+        };
+    }
+
+}
diff --git 
a/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlRouteConfigurationTest.java
 
b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlRouteConfigurationTest.java
new file mode 100644
index 000000000000..a5ae54722e2d
--- /dev/null
+++ 
b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlRouteConfigurationTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.management;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import static 
org.apache.camel.management.ManagedRouteUpdateRouteFromXmlTest.getRouteObjectName;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The route updated from XML gets the route configurations, as the route it 
replaces had (CAMEL-28097).
+ */
+@DisabledOnOs(OS.AIX)
+public class ManagedRouteUpdateRouteFromXmlRouteConfigurationTest extends 
ManagementTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        // enable updating route
+        
context.getManagementStrategy().getManagementAgent().setUpdateRouteEnabled(true);
+
+        return context;
+    }
+
+    @Test
+    public void testUpdateRouteFromXmlKeepsGlobalOnException() throws 
Exception {
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = getRouteObjectName(mbeanServer);
+
+        getMockEndpoint("mock:global").expectedBodiesReceived("Hello World");
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        String xml = "<route id=\"myRoute\" 
xmlns=\"http://camel.apache.org/schema/spring\";>"
+                     + "  <from uri=\"direct:start\"/>"
+                     + "  <to uri=\"mock:changed\"/>"
+                     + "  <throwException 
exceptionType=\"java.lang.IllegalArgumentException\" message=\"Changed\"/>"
+                     + "</route>";
+        mbeanServer.invoke(on, "updateRouteFromXml", new Object[] { xml }, new 
String[] { "java.lang.String" });
+        assertEquals(1, context.getRoutes().size());
+
+        resetMocks();
+        getMockEndpoint("mock:changed").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:global").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:named").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testUpdateRouteFromXmlNamedConfiguration() throws Exception {
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = getRouteObjectName(mbeanServer);
+
+        String xml = "<route id=\"myRoute\" routeConfigurationId=\"named\" 
xmlns=\"http://camel.apache.org/schema/spring\";>"
+                     + "  <from uri=\"direct:start\"/>"
+                     + "  <throwException 
exceptionType=\"java.lang.IllegalArgumentException\" message=\"Changed\"/>"
+                     + "</route>";
+        mbeanServer.invoke(on, "updateRouteFromXml", new Object[] { xml }, new 
String[] { "java.lang.String" });
+        assertEquals(1, context.getRoutes().size());
+
+        getMockEndpoint("mock:named").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:global").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder[] createRouteBuilders() {
+        return new RoutesBuilder[] {
+                new RouteConfigurationBuilder() {
+                    @Override
+                    public void configuration() {
+                        // global route configuration (no id)
+                        
routeConfiguration().onException(Exception.class).handled(true).to("mock:global");
+                        
routeConfiguration("named").onException(Exception.class).handled(true).to("mock:named");
+                    }
+                },
+                new RouteBuilder() {
+                    @Override
+                    public void configure() {
+                        from("direct:start").routeId("myRoute")
+                                .throwException(new 
IllegalArgumentException("Forced"));
+                    }
+                }
+        };
+    }
+
+}
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 a3638dcc0c47..2de5592bb3d4 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
@@ -652,3 +652,17 @@ The polling consumer (`pollEnrich`, or `receive` of a 
`ConsumerTemplate`) now cl
 the polled exchange is done, instead of leaving it open. With `pollEnrich` 
this happens when the enriched exchange
 completes. With a `ConsumerTemplate`, call `doneUoW(exchange)` when you are 
done with the received exchange, as for any
 other polled endpoint.
+
+=== camel-core - routes added with addRouteDefinition get the route 
configurations
+
+A route that is added with `ModelCamelContext.addRouteDefinition` or 
`addRouteDefinitions`, as a `RouteDefinition`
+that is not prepared yet, now gets the matching route configurations of the 
`CamelContext`, in the same way as a route
+from a `RouteBuilder`: the global route configurations, or the ones selected 
with its `routeConfigurationId`.
+Before, such a route got no route configuration at all, so for example the 
`onException` of a global route
+configuration did not apply to it. This includes a route that is updated from 
XML with the JMX operation
+`updateRouteFromXml` of `ManagedRoute`: the updated route gets the matching 
route configurations too (select them
+with `routeConfigurationId` in the XML when the route used named route 
configurations).
+An error handler set on the route itself still takes precedence over the one 
of a route configuration.
+
+Routes from a `RouteBuilder`, the YAML and XML DSLs, route templates, Kamelets 
and Spring XML are prepared already, and
+are not affected.

Reply via email to