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 2016/12/20 14:36:03 UTC

[2/2] camel git commit: CAMEL-10599: Add ReloadStrategy to allow watching for file changes and reload routes on the fly.

CAMEL-10599: Add ReloadStrategy to allow watching for file changes and reload routes on the fly.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3680b4c7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3680b4c7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3680b4c7

Branch: refs/heads/master
Commit: 3680b4c73d540af36bfbedec66685fb889e839a7
Parents: 4df2239
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 20 15:30:15 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Dec 20 15:33:19 2016 +0100

----------------------------------------------------------------------
 .../camel/support/ReloadStrategySupport.java    | 38 +++++++++++++------
 .../src/main/resources/camel/myroutes.xml       | 39 ++++++++++++++++++++
 2 files changed, 65 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3680b4c7/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java b/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
index 3513cbd..8290a92 100644
--- a/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
+++ b/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
@@ -18,6 +18,7 @@ package org.apache.camel.support;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -107,8 +108,8 @@ public abstract class ReloadStrategySupport extends ServiceSupport implements Re
         // find all <route> which are the routes
         NodeList list = dom.getElementsByTagName("route");
 
-        // collect which routes are updated
-        CollectionStringBuffer csb = new CollectionStringBuffer(",");
+        // collect which routes are updated/skpped
+        List<RouteDefinition> routes = new ArrayList<>();
 
         if (list != null && list.getLength() > 0) {
             for (int i = 0; i < list.getLength(); i++) {
@@ -131,14 +132,10 @@ public abstract class ReloadStrategySupport extends ServiceSupport implements Re
                 }
 
                 try {
-                    RoutesDefinition routes = ModelHelper.loadRoutesDefinition(camelContext, node);
-                    if (!routes.getRoutes().isEmpty()) {
-                        // collect route ids and force assign ids if not in use
-                        for (RouteDefinition route : routes.getRoutes()) {
-                            String id = route.idOrCreate(camelContext.getNodeIdFactory());
-                            csb.append(id);
-                        }
-                        camelContext.addRouteDefinitions(routes.getRoutes());
+                    // load from XML -> JAXB model and store as routes to be updated
+                    RoutesDefinition loaded = ModelHelper.loadRoutesDefinition(camelContext, node);
+                    if (!loaded.getRoutes().isEmpty()) {
+                        routes.addAll(loaded.getRoutes());
                     }
                 } catch (Exception e) {
                     failed++;
@@ -147,8 +144,25 @@ public abstract class ReloadStrategySupport extends ServiceSupport implements Re
             }
         }
 
-        if (!csb.isEmpty()) {
-            log.info("Reloaded routes: [{}] from XML resource: {}", csb, name);
+        if (!routes.isEmpty()) {
+            try {
+                CollectionStringBuffer csb = new CollectionStringBuffer(",");
+                // collect route ids and force assign ids if not in use
+                for (RouteDefinition route : routes) {
+                    String id = route.idOrCreate(camelContext.getNodeIdFactory());
+                    csb.append(id);
+                }
+                log.debug("Reloading routes: [{}] from XML resource: {}", csb, name);
+
+                // update the routes (add will remove and shutdown first)
+                camelContext.addRouteDefinitions(routes);
+
+                log.info("Reloaded routes: [{}] from XML resource: {}", csb, name);
+            } catch (Exception e) {
+                failed++;
+                throw ObjectHelper.wrapRuntimeCamelException(e);
+            }
+
         }
 
         // update cache

http://git-wip-us.apache.org/repos/asf/camel/blob/3680b4c7/examples/camel-example-spring-boot-reload/src/main/resources/camel/myroutes.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-reload/src/main/resources/camel/myroutes.xml b/examples/camel-example-spring-boot-reload/src/main/resources/camel/myroutes.xml
new file mode 100644
index 0000000..035887b
--- /dev/null
+++ b/examples/camel-example-spring-boot-reload/src/main/resources/camel/myroutes.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+
+<!-- notice Camel will only update the routes that has been changed, so you can edit either either route or both
+     and save the file, and Camel will update only what is required -->
+
+<routes xmlns="http://camel.apache.org/schema/spring">
+
+  <route id="timer">
+    <from uri="timer:foo"/>
+    <to uri="direct:foo"/>
+    <!-- you can try changing me -->
+    <log message="You said: ${body}"/>
+  </route>
+
+  <route id="foo">
+    <from uri="direct:foo"/>
+    <transform>
+      <!-- and I can be changed too -->
+      <constant>Hello World</constant>
+    </transform>
+  </route>
+
+</routes>