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 2021/12/01 19:22:31 UTC

[camel] 02/02: CAMEL-17260: camel-core - Reload routes - Should handle non changed routes

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

commit 13428f71c968d1e0b3c76bd4cbca1457a6a9fde9
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 1 20:21:42 2021 +0100

    CAMEL-17260: camel-core - Reload routes - Should handle non changed routes
---
 .../java/org/apache/camel/spi/RoutesLoader.java    | 14 +++++++++++++
 .../support/FileWatcherResourceReloadStrategy.java |  4 ++--
 .../camel/support/RouteWatcherReloadStrategy.java  | 23 +++++++++++++++++++---
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  2 +-
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
index b161cf2..69d2cfe 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
@@ -84,6 +84,20 @@ public interface RoutesLoader extends CamelContextAware {
      * @return           route ids for the routes that was loaded or updated.
      */
     default Set<String> updateRoutes(Resource... resources) throws Exception {
+        return updateRoutes(Arrays.asList(resources));
+    }
+
+    /**
+     * Loads or updates existing {@link RoutesBuilder} from the give list of {@link Resource} into the current
+     * {@link org.apache.camel.CamelContext}.
+     *
+     * If a route is loaded with a route id for an existing route, then the existing route is stopped and remove, so it
+     * can be updated.
+     *
+     * @param  resources the resources to be loaded or updated.
+     * @return           route ids for the routes that was loaded or updated.
+     */
+    default Set<String> updateRoutes(Collection<Resource> resources) throws Exception {
         Set<String> answer = new LinkedHashSet<>();
         Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
         for (RoutesBuilder builder : builders) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
index 2881d04..87506f3 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
@@ -265,8 +265,8 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
                     for (WatchEvent<?> event : key.pollEvents()) {
                         WatchEvent<Path> we = (WatchEvent<Path>) event;
                         Path path = we.context();
-                        File file = pathToReload.resolve(path).toAbsolutePath().toFile();
-                        String name = FileUtil.compactPath(file.getAbsolutePath());
+                        File file = pathToReload.resolve(path).toFile();
+                        String name = FileUtil.compactPath(file.getPath());
                         LOG.debug("Detected Modified/Created file: {}", name);
                         boolean accept = fileFilter == null || fileFilter.accept(file);
                         if (accept) {
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 60a1afc..bf0db4c 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
@@ -132,17 +132,31 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
     }
 
     protected void onRouteReload(String name, Resource resource) {
+        // remember all existing resources
+        List<Resource> sources = new ArrayList<>();
+
         try {
             // should all existing routes be stopped and removed first?
             if (removeAllRoutes) {
+                // remember all the sources of the current routes (except the updated)
+                getCamelContext().getRoutes().forEach(r -> {
+                    Resource rs = r.getSourceResource();
+                    if (rs != null && !rs.getLocation().equals(resource.getLocation())) {
+                        sources.add(rs);
+                    }
+                });
                 // first stop and remove all routes
                 getCamelContext().getRouteController().removeAllRoutes();
                 // remove left-over route templates and endpoints, so we can start on a fresh
                 getCamelContext().removeRouteTemplates("*");
                 getCamelContext().getEndpointRegistry().clear();
             }
+
+            sources.add(resource);
+
+            // reload those other routes that was stopped and removed as we want to keep running those
             Set<String> ids
-                    = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(resource);
+                    = getCamelContext().adapt(ExtendedCamelContext.class).getRoutesLoader().updateRoutes(sources);
             if (!ids.isEmpty()) {
                 List<String> lines = new ArrayList<>();
                 int total = 0;
@@ -153,10 +167,12 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                     if (ServiceStatus.Started.name().equals(status)) {
                         started++;
                     }
+                    Route route = getCamelContext().getRoute(id);
                     // use basic endpoint uri to not log verbose details or potential sensitive data
-                    String uri = getCamelContext().getRoute(id).getEndpoint().getEndpointBaseUri();
+                    String uri = route.getEndpoint().getEndpointBaseUri();
                     uri = URISupport.sanitizeUri(uri);
-                    lines.add(String.format("    %s %s (%s)", status, id, uri));
+                    String loc = route.getSourceResource() != null ? route.getSourceResource().getLocation() : "";
+                    lines.add(String.format("    %s %s (%s) (source: %s)", status, id, uri, loc));
                 }
                 LOG.info(String.format("Routes reloaded summary (total:%s started:%s)", total, started));
                 // if we are default/verbose then log each route line
@@ -167,6 +183,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                     }
                 }
             }
+
             // fire events for routes reloaded
             for (String id : ids) {
                 Route route = getCamelContext().getRoute(id);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 9c90b9f..c324f7d 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -174,7 +174,7 @@ class Run implements Callable<Integer> {
                 // check if file exist
                 File inputFile = new File(file.substring(5));
                 if (!inputFile.exists() && !inputFile.isFile()) {
-                    System.err.println("File does not exist: " + files);
+                    System.err.println("File does not exist: " + file);
                     return 1;
                 }
             }