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/19 15:35:19 UTC

[7/8] 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/76218fb1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/76218fb1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/76218fb1

Branch: refs/heads/master
Commit: 76218fb1c4876dcfad484ea1dbf062799eeb768a
Parents: b868b5f
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Dec 19 15:42:45 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Dec 19 16:34:46 2016 +0100

----------------------------------------------------------------------
 .../camel/impl/FileWatcherReloadStrategy.java   | 46 ++++++++------
 .../ManagedFileWatcherReloadStrategyTest.java   | 65 ++++++++++++++++++++
 2 files changed, 92 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/76218fb1/camel-core/src/main/java/org/apache/camel/impl/FileWatcherReloadStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/FileWatcherReloadStrategy.java b/camel-core/src/main/java/org/apache/camel/impl/FileWatcherReloadStrategy.java
index e80f048..20573b7 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/FileWatcherReloadStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/FileWatcherReloadStrategy.java
@@ -27,13 +27,13 @@ import java.util.Locale;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 
-import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
-import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
-
 import org.apache.camel.support.ReloadStrategySupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 
+import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
+import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
+
 /**
  * A file based {@link org.apache.camel.spi.ReloadStrategy} which watches a file folder
  * for modified files and reload on file changes.
@@ -45,8 +45,7 @@ import org.apache.camel.util.ObjectHelper;
  */
 public class FileWatcherReloadStrategy extends ReloadStrategySupport {
 
-    private Path folder;
-    private WatchService watcher;
+    private String folder;
     private ExecutorService executorService;
 
     public FileWatcherReloadStrategy() {
@@ -56,12 +55,9 @@ public class FileWatcherReloadStrategy extends ReloadStrategySupport {
         setFolder(directory);
     }
 
-    public Path getFolder() {
-        return folder;
-    }
 
     public void setFolder(String folder) {
-        this.folder = new File(folder).toPath();
+        this.folder = folder;
     }
 
     @Override
@@ -71,17 +67,21 @@ public class FileWatcherReloadStrategy extends ReloadStrategySupport {
             return;
         }
 
-        log.info("Starting ReloadStrategy to watch directory: {}", folder);
-        try {
-            this.watcher = folder.getFileSystem().newWatchService();
-            // we cannot support deleting files as we don't know which routes that would be
-            folder.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
-        } catch (IOException e) {
-            throw ObjectHelper.wrapRuntimeCamelException(e);
+        File dir = new File(folder);
+        if (dir.exists() && dir.isDirectory()) {
+            log.info("Starting ReloadStrategy to watch directory: {}", dir);
+            try {
+                Path path = dir.toPath();
+                WatchService watcher = path.getFileSystem().newWatchService();
+                // we cannot support deleting files as we don't know which routes that would be
+                path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
+
+                executorService = getCamelContext().getExecutorServiceManager().newSingleThreadExecutor(this, "FileWatcherReloadStrategy");
+                executorService.submit(new WatchFileChangesTask(watcher, path));
+            } catch (IOException e) {
+                throw ObjectHelper.wrapRuntimeCamelException(e);
+            }
         }
-
-        executorService = getCamelContext().getExecutorServiceManager().newSingleThreadExecutor(this, "FileWatcherReloadStrategy");
-        executorService.submit(new WatchFileChangesTask());
     }
 
 
@@ -98,6 +98,14 @@ public class FileWatcherReloadStrategy extends ReloadStrategySupport {
      */
     protected class WatchFileChangesTask implements Runnable {
 
+        private final WatchService watcher;
+        private final Path folder;
+
+        public WatchFileChangesTask(WatchService watcher, Path folder) {
+            this.watcher = watcher;
+            this.folder = folder;
+        }
+
         public void run() {
             log.debug("ReloadStrategy is starting watching folder: {}", folder);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76218fb1/camel-core/src/test/java/org/apache/camel/management/ManagedFileWatcherReloadStrategyTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedFileWatcherReloadStrategyTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedFileWatcherReloadStrategyTest.java
new file mode 100644
index 0000000..76e7aa5
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/management/ManagedFileWatcherReloadStrategyTest.java
@@ -0,0 +1,65 @@
+/**
+ * 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.builder.RouteBuilder;
+import org.apache.camel.impl.FileWatcherReloadStrategy;
+
+/**
+ * @version 
+ */
+public class ManagedFileWatcherReloadStrategyTest extends ManagementTestSupport {
+
+    public void testReloadStrategy() throws Exception {
+        // JMX tests dont work well on AIX CI servers (hangs them)
+        if (isPlatform("aix")) {
+            return;
+        }
+
+        MBeanServer mbeanServer = getMBeanServer();
+
+        ObjectName on = ObjectName.getInstance("org.apache.camel:context=camel-1,type=services,name=FileWatcherReloadStrategy");
+        assertTrue(mbeanServer.isRegistered(on));
+
+        String strategy = (String) mbeanServer.getAttribute(on, "Strategy");
+        assertEquals("FileWatcherReloadStrategy", strategy);
+
+        Integer reload = (Integer) mbeanServer.getAttribute(on, "ReloadCounter");
+        assertEquals(0, reload.intValue());
+
+        Integer failed = (Integer) mbeanServer.getAttribute(on, "FailedCounter");
+        assertEquals(0, failed.intValue());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // add reload strategy
+                context.setReloadStrategy(new FileWatcherReloadStrategy("target/dummy"));
+
+                from("direct:start")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}