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

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

commit 2672c926db94da574922c85cf6fb5346e82bdc54
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Dec 3 22:43:09 2026 +0100

    CAMEL-28101: camel-core - a route reload removes the route configurations a 
file no longer declares, or of a deleted file
    
    On route reload only the route configurations with the same ids as in the 
new
    version of a file were replaced, so a route configuration removed from a 
file,
    or the route configurations of a deleted file, stayed in the model and the
    routes created afterwards still used them.
    
    Each route configuration now has the resource of the builder it came from. 
The
    update of a route configuration builder removes all route configurations of 
its
    resource before it adds the new ones, and RouteWatcherReloadStrategy 
removes the
    route configurations of a deleted file with the new
    CamelContext.removeRouteConfigurationsFromResources. Running routes are not
    refreshed (CAMEL-25142).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../main/java/org/apache/camel/CamelContext.java   |  16 ++
 .../camel/impl/engine/SimpleCamelContext.java      |   5 +
 .../org/apache/camel/impl/DefaultCamelContext.java |   7 +
 .../java/org/apache/camel/impl/DefaultModel.java   |  16 ++
 .../camel/builder/RouteConfigurationBuilder.java   |  20 ++
 .../RoutesConfigurationFromResourcesTest.java      | 120 ++++++++++++
 .../camel/support/RouteWatcherReloadStrategy.java  |  10 +-
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |  13 ++
 .../EndpointRouteConfigurationBuilder.java         |  23 +++
 .../yaml/RouteReloadRouteConfigurationTest.java    | 202 +++++++++++++++++++++
 10 files changed, 430 insertions(+), 2 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java 
b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
index 6b39e2971ad3..bc1ed85e662f 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
@@ -861,6 +861,22 @@ public interface CamelContext extends 
CamelContextLifecycle, RuntimeConfiguratio
      */
     void removeRouteTemplatesFromResources(String pattern) throws Exception;
 
+    /**
+     * Removes the route configurations that were loaded from a resource whose 
location matches the pattern, such as a
+     * route file, and keeps those without a resource, such as the route 
configurations of a Java
+     * {@code RouteConfigurationBuilder} added to the context. A route reload 
removes with this the route configurations
+     * of a file that was deleted, or that no longer declares them.
+     * <p/>
+     * The routes that use a removed route configuration keep it until they 
are created again.
+     *
+     * @param  pattern   pattern for the location of the resource, such as * 
for all, file:src/main/resources/cfg.yaml
+     *                   for the route configurations of that file, or 
file:src/main/resources/* for those of the files
+     *                   in that folder
+     * @throws Exception is thrown if error during removing route 
configurations
+     * @since            4.25
+     */
+    void removeRouteConfigurationsFromResources(String pattern) throws 
Exception;
+
     /**
      * Adds the given route policy factory
      *
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
index 3c291d3ef3da..ad533aa2e369 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
@@ -797,6 +797,11 @@ public class SimpleCamelContext extends 
AbstractCamelContext {
         throw new UnsupportedOperationException();
     }
 
+    @Override
+    public void removeRouteConfigurationsFromResources(String pattern) throws 
Exception {
+        throw new UnsupportedOperationException();
+    }
+
     @Override
     public List<NamedRoute> getNamedRouteDefinitions() {
         throw new UnsupportedOperationException();
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..0f2d034d62a1 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
@@ -450,6 +450,13 @@ public class DefaultCamelContext extends 
SimpleCamelContext implements ModelCame
         }
     }
 
+    @Override
+    public void removeRouteConfigurationsFromResources(String pattern) throws 
Exception {
+        if (!isLockModel()) {
+            model.removeRouteConfigurationsFromResources(pattern);
+        }
+    }
+
     @Override
     public List<NamedRoute> getNamedRouteDefinitions() {
         return new ArrayList<>(model.getRouteDefinitions());
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..ad01d18b0125 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
@@ -167,6 +167,22 @@ public class DefaultModel implements Model {
         }
     }
 
+    /**
+     * Removes the route configurations loaded from a resource whose location 
matches the pattern (by identity, so a
+     * global route configuration without an id is removed too).
+     *
+     * @see 
org.apache.camel.CamelContext#removeRouteConfigurationsFromResources(String)
+     */
+    void removeRouteConfigurationsFromResources(String pattern) {
+        lock.lock();
+        try {
+            routesConfigurations.removeIf(def -> def.getResource() != null
+                    && 
PatternHelper.matchPattern(def.getResource().getLocation(), pattern));
+        } finally {
+            lock.unlock();
+        }
+    }
+
     @Override
     public List<RouteConfigurationDefinition> 
getRouteConfigurationDefinitions() {
         return routesConfigurations;
diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteConfigurationBuilder.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteConfigurationBuilder.java
index 61e2f59daeeb..2b9143d5b630 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteConfigurationBuilder.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteConfigurationBuilder.java
@@ -88,6 +88,12 @@ public abstract class RouteConfigurationBuilder extends 
RouteBuilder implements
             configuration();
         }
         List<RouteConfigurationDefinition> list = 
getRouteConfigurationCollection().getRouteConfigurations();
+        // the resource of this builder (such as a route file) on each route 
configuration, so they can be removed by it
+        assignResource(list);
+        if (getResource() != null && getResource().getLocation() != null) {
+            // remove the route configurations of this resource, also those it 
no longer declares
+            
context.removeRouteConfigurationsFromResources(getResource().getLocation());
+        }
         if (!list.isEmpty()) {
             // remove existing before updating
             for (RouteConfigurationDefinition def : list) {
@@ -97,6 +103,19 @@ public abstract class RouteConfigurationBuilder extends 
RouteBuilder implements
         }
     }
 
+    /**
+     * Sets the resource of this builder on the route configurations that have 
none.
+     */
+    private void assignResource(List<RouteConfigurationDefinition> list) {
+        if (getResource() != null) {
+            for (RouteConfigurationDefinition def : list) {
+                if (def.getResource() == null) {
+                    def.setResource(getResource());
+                }
+            }
+        }
+    }
+
     @Override
     protected void initializeCamelContext(CamelContext camelContext) {
         super.initializeCamelContext(camelContext);
@@ -112,6 +131,7 @@ public abstract class RouteConfigurationBuilder extends 
RouteBuilder implements
         if (getResource() != null) {
             getRouteConfigurationCollection().setResource(getResource());
         }
+        
assignResource(getRouteConfigurationCollection().getRouteConfigurations());
         camelContext.getCamelContextExtension().getContextPlugin(Model.class)
                 
.addRouteConfigurations(getRouteConfigurationCollection().getRouteConfigurations());
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationFromResourcesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationFromResourcesTest.java
new file mode 100644
index 000000000000..364f7fc92dcf
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationFromResourcesTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.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.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * CAMEL-28101: route configurations are removed by the resource they were 
loaded from.
+ */
+public class RoutesConfigurationFromResourcesTest extends ContextTestSupport {
+
+    private final Resource a = ResourceHelper.fromString("mem:a.yaml", "");
+    private final Resource b = ResourceHelper.fromString("mem:b.yaml", "");
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    /**
+     * A builder loaded from the resource (as a routes loader does), with the 
given route configurations: null for a
+     * global one.
+     */
+    private static RouteConfigurationBuilder builder(Resource resource, 
String... ids) {
+        RouteConfigurationBuilder builder = new RouteConfigurationBuilder() {
+            @Override
+            public void configuration() {
+                for (String id : ids) {
+                    (id != null ? routeConfiguration(id) : 
routeConfiguration())
+                            
.onException(IllegalArgumentException.class).handled(true).to("mock:" + id);
+                }
+            }
+        };
+        builder.setResource(resource);
+        return builder;
+    }
+
+    private static RouteConfigurationBuilder javaBuilder(String id) {
+        return new RouteConfigurationBuilder() {
+            @Override
+            public void configuration() {
+                
routeConfiguration(id).onException(IllegalArgumentException.class).handled(true).to("mock:"
 + id);
+            }
+        };
+    }
+
+    private List<String> ids() {
+        return 
context.getRouteConfigurationDefinitions().stream().map(RouteConfigurationDefinition::getId).toList();
+    }
+
+    @Test
+    public void testConfigurationsGetTheResourceOfTheBuilder() throws 
Exception {
+        context.addRoutes(builder(a, null, "a1"));
+        context.addRoutes(javaBuilder("java"));
+
+        List<RouteConfigurationDefinition> list = 
context.getRouteConfigurationDefinitions();
+        assertEquals(3, list.size());
+        assertSame(a, list.get(0).getResource());
+        assertSame(a, list.get(1).getResource());
+        assertNull(list.get(2).getResource());
+    }
+
+    @Test
+    public void testRemoveRouteConfigurationsFromResources() throws Exception {
+        context.addRoutes(builder(a, null, "a1"));
+        context.addRoutes(builder(b, null, "b1"));
+        context.addRoutes(javaBuilder("java"));
+        context.start();
+        assertEquals(5, context.getRouteConfigurationDefinitions().size());
+
+        // the global route configuration of a is removed too, and not the one 
of b
+        context.removeRouteConfigurationsFromResources("mem:a.yaml");
+        assertEquals(List.of("b1", "java"), ids().stream().filter(id -> id != 
null).toList());
+        assertEquals(3, ids().size());
+        assertSame(b, 
context.getRouteConfigurationDefinitions().get(0).getResource());
+
+        // all those from a resource, not those added from Java
+        context.removeRouteConfigurationsFromResources("*");
+        assertEquals(List.of("java"), ids());
+    }
+
+    @Test
+    public void 
testUpdateRemovesTheConfigurationsTheResourceNoLongerDeclares() throws 
Exception {
+        context.addRoutes(builder(a, null, "a1", "a2"));
+        context.addRoutes(builder(b, "b1"));
+        context.start();
+
+        // a now declares a2 only
+        builder(a, "a2").updateRouteConfigurationsToCamelContext(context);
+        assertEquals(List.of("b1", "a2"), ids());
+
+        // a declares no route configuration at all
+        builder(a).updateRouteConfigurationsToCamelContext(context);
+        assertEquals(List.of("b1"), ids());
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
index d65d46f19d30..7157b48f8090 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
@@ -632,8 +632,14 @@ public class RouteWatcherReloadStrategy extends 
FileWatcherResourceReloadStrateg
 
             if (resources != null) {
                 for (Resource resource : resources) {
-                    if (Files.exists(Paths.get(resource.getURI())) && 
!equalResourceLocation(sources, resource)) {
-                        sources.add(resource);
+                    if (Files.exists(Paths.get(resource.getURI()))) {
+                        if (!equalResourceLocation(sources, resource)) {
+                            sources.add(resource);
+                        }
+                    } else if (resource.getLocation() != null) {
+                        // the file is deleted, so nothing loads it again: 
remove its route configurations, which the
+                        // routes created from now on would otherwise still 
use (CAMEL-28101)
+                        
getCamelContext().removeRouteConfigurationsFromResources(resource.getLocation());
                     }
                 }
             }
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..40eb9b46e86b 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,16 @@ 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 - route reload removes the route configurations a file no 
longer declares
+
+When the routes are reloaded (`camel.main.routesReloadEnabled`, or 
`RoutesLoader.updateRoutes`), the route
+configurations of a reloaded file are replaced by those the file declares now. 
A route configuration removed from
+the file, or the route configurations of a deleted file, are removed. Before, 
they stayed and the routes created after
+the reload still used them. The routes that keep running are not created 
again, so they keep the route configuration
+they were created with until their own file is reloaded (with the default 
`removeAllRoutes=true` all routes are created
+again).
+
+Each route configuration now has the resource it was loaded from, also those 
of XML and Java files.
+The new method `CamelContext.removeRouteConfigurationsFromResources(pattern)` 
removes the route configurations of the
+resources whose location matches the pattern.
diff --git 
a/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
 
b/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
index 5cde309d1113..59ff9c049992 100644
--- 
a/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
+++ 
b/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
@@ -88,6 +88,12 @@ public abstract class EndpointRouteConfigurationBuilder 
extends EndpointRouteBui
             configuration();
         }
         List<RouteConfigurationDefinition> list = 
getRouteConfigurationCollection().getRouteConfigurations();
+        // the resource of this builder (such as a route file) on each route 
configuration, so they can be removed by it
+        assignResource(list);
+        if (getResource() != null && getResource().getLocation() != null) {
+            // remove the route configurations of this resource, also those it 
no longer declares
+            
context.removeRouteConfigurationsFromResources(getResource().getLocation());
+        }
         if (!list.isEmpty()) {
             // remove existing before updating
             for (RouteConfigurationDefinition def : list) {
@@ -104,8 +110,25 @@ public abstract class EndpointRouteConfigurationBuilder 
extends EndpointRouteBui
             throw new IllegalArgumentException("CamelContext has not been 
injected!");
         }
         getRouteConfigurationCollection().setCamelContext(camelContext);
+        if (getResource() != null) {
+            getRouteConfigurationCollection().setResource(getResource());
+        }
+        
assignResource(getRouteConfigurationCollection().getRouteConfigurations());
         camelContext.getCamelContextExtension().getContextPlugin(Model.class)
                 
.addRouteConfigurations(getRouteConfigurationCollection().getRouteConfigurations());
     }
 
+    /**
+     * Sets the resource of this builder on the route configurations that have 
none.
+     */
+    private void assignResource(List<RouteConfigurationDefinition> list) {
+        if (getResource() != null) {
+            for (RouteConfigurationDefinition def : list) {
+                if (def.getResource() == null) {
+                    def.setResource(getResource());
+                }
+            }
+        }
+    }
+
 }
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRouteConfigurationTest.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRouteConfigurationTest.java
new file mode 100644
index 000000000000..7109e453521b
--- /dev/null
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRouteConfigurationTest.java
@@ -0,0 +1,202 @@
+/*
+ * 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.dsl.yaml;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.dsl.yaml.support.YamlTestSupport;
+import org.apache.camel.model.RouteConfigurationDefinition;
+import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.support.RouteWatcherReloadStrategy;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * CAMEL-28101: a route configuration that was removed from a file, or whose 
file was deleted, is removed from the model
+ * when the routes are reloaded, so the routes created afterwards no longer 
use it.
+ */
+class RouteReloadRouteConfigurationTest extends YamlTestSupport {
+
+    /** Subclass to expose protected lifecycle methods for testing without a 
live file watcher. */
+    static class TestableStrategy extends RouteWatcherReloadStrategy {
+        TestableStrategy(String dir) {
+            super(dir);
+        }
+
+        @Override
+        public void doStart() throws Exception {
+            super.doStart();
+        }
+
+        @Override
+        public void doStop() throws Exception {
+            super.doStop();
+        }
+    }
+
+    @TempDir
+    Path dir;
+
+    private Path cfg;
+    private Path routes;
+    private TestableStrategy strategy;
+
+    @Override
+    public void doSetup() throws Exception {
+        cfg = dir.resolve("cfg.camel.yaml");
+        routes = dir.resolve("routes.camel.yaml");
+        Files.writeString(cfg, configuration(null, "global") + 
configuration("other", "other"));
+        Files.writeString(routes, """
+                - route:
+                    id: r1
+                    from:
+                      uri: direct:r1
+                      steps:
+                        - throwException:
+                            exceptionType: java.lang.IllegalArgumentException
+                            message: Forced
+                - route:
+                    id: r2
+                    routeConfigurationId: other
+                    from:
+                      uri: direct:r2
+                      steps:
+                        - throwException:
+                            exceptionType: java.lang.IllegalArgumentException
+                            message: Forced
+                """);
+        context.start();
+        loadRoutes(ResourceHelper.resolveResource(context, "file:" + cfg),
+                ResourceHelper.resolveResource(context, "file:" + routes));
+        assertThat(send("direct:r1")).isEqualTo("global");
+        assertThat(send("direct:r2")).isEqualTo("other");
+
+        strategy = new TestableStrategy(dir.toString());
+        strategy.setCamelContext(context);
+        strategy.setPattern("*.yaml");
+        // the strategy is not started (no file watcher in the test): its 
reload callback is driven by hand
+        strategy.doStart();
+    }
+
+    @Override
+    public void doCleanup() throws Exception {
+        if (strategy != null) {
+            strategy.doStop();
+        }
+    }
+
+    @Test
+    void reloadOfTheSameFileKeepsItsConfigurationsOnce() throws Exception {
+        reload(cfg);
+
+        assertThat(strategy.getFailedCounter()).isZero();
+        assertThat(configurationIds()).containsExactlyInAnyOrder(null, 
"other");
+        assertThat(send("direct:r1")).isEqualTo("global");
+        assertThat(send("direct:r2")).isEqualTo("other");
+    }
+
+    @Test
+    void configurationRemovedFromFile() throws Exception {
+        Files.writeString(cfg, configuration("other", "other"));
+        reload(cfg);
+
+        assertThat(strategy.getFailedCounter()).isZero();
+        assertThat(configurationIds()).containsExactly("other");
+        // the routes are created again (removeAllRoutes=true), without the 
global configuration
+        assertThat(send("direct:r1")).isEqualTo("FAILED");
+        assertThat(send("direct:r2")).isEqualTo("other");
+    }
+
+    @Test
+    void configurationFileDeleted() throws Exception {
+        Files.delete(cfg);
+        reload(cfg);
+
+        assertThat(strategy.getFailedCounter()).isZero();
+        assertThat(configurationIds()).isEmpty();
+        assertThat(send("direct:r1")).isEqualTo("FAILED");
+        assertThat(send("direct:r2")).isEqualTo("FAILED");
+    }
+
+    @Test
+    void configurationRemovedFromFileWithoutRemoveAllRoutes() throws Exception 
{
+        strategy.setRemoveAllRoutes(false);
+        Files.writeString(cfg, configuration("other", "other"));
+        reload(cfg);
+
+        assertThat(strategy.getFailedCounter()).isZero();
+        assertThat(configurationIds()).containsExactly("other");
+        // the running routes are not created again, so they keep the 
configuration they were created with
+        assertThat(send("direct:r1")).isEqualTo("global");
+
+        // until their file is reloaded
+        reload(routes);
+        assertThat(send("direct:r1")).isEqualTo("FAILED");
+        assertThat(send("direct:r2")).isEqualTo("other");
+    }
+
+    @Test
+    void configurationFileDeletedWithoutRemoveAllRoutes() throws Exception {
+        strategy.setRemoveAllRoutes(false);
+        Files.delete(cfg);
+        reload(cfg);
+
+        assertThat(strategy.getFailedCounter()).isZero();
+        assertThat(configurationIds()).isEmpty();
+
+        reload(routes);
+        assertThat(send("direct:r1")).isEqualTo("FAILED");
+        assertThat(send("direct:r2")).isEqualTo("FAILED");
+    }
+
+    private void reload(Path file) throws Exception {
+        strategy.getResourceReload().onReload(file.toString(), 
ResourceHelper.resolveResource(context, "file:" + file));
+    }
+
+    private String[] configurationIds() {
+        return 
context.getRouteConfigurationDefinitions().stream().map(RouteConfigurationDefinition::getId)
+                .toArray(String[]::new);
+    }
+
+    /**
+     * @return the body set by the onException that handled the exchange, or 
FAILED when it was not handled
+     */
+    private String send(String uri) {
+        Exchange out = context.createProducerTemplate().request(uri, e -> 
e.getMessage().setBody("Hello"));
+        return out.isFailed() ? "FAILED" : 
out.getMessage().getBody(String.class);
+    }
+
+    private static String configuration(String id, String body) {
+        return """
+                - routeConfiguration:
+                    %s
+                    onException:
+                      - onException:
+                          exception:
+                            - java.lang.IllegalArgumentException
+                          handled:
+                            constant: "true"
+                          steps:
+                            - setBody:
+                                constant: "%s"
+                """.formatted(id != null ? "id: " + id : "description: 
global", body);
+    }
+}

Reply via email to