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 2019/01/17 10:01:25 UTC

[camel] branch master updated: CAMEL-13074: FileWatcherReloadStrategy - Use file component instead of JDK FileWatcher

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new b69ff3a  CAMEL-13074: FileWatcherReloadStrategy - Use file component instead of JDK FileWatcher
b69ff3a is described below

commit b69ff3ac7b32ce382549b5860da7dceb262cf9b3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 17 11:00:48 2019 +0100

    CAMEL-13074: FileWatcherReloadStrategy - Use file component instead of JDK FileWatcher
---
 .../camel/impl/FileWatcherReloadStrategy.java      | 28 ++++++++++------------
 .../camel/impl/FileWatcherReloadStrategyTest.java  |  1 +
 2 files changed, 13 insertions(+), 16 deletions(-)

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 7cbfcc9..e8370b5 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
@@ -18,7 +18,6 @@ package org.apache.camel.impl;
 
 import java.io.File;
 import java.io.InputStream;
-import java.util.Locale;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Endpoint;
@@ -99,8 +98,9 @@ public class FileWatcherReloadStrategy extends ReloadStrategySupport {
         if (dir.exists() && dir.isDirectory()) {
             log.info("Starting ReloadStrategy to watch directory: {}", dir);
 
+            // only include xml files
             endpoint = getCamelContext().getEndpoint("file:" + dir + "?delay=" + delay + "&recursive=" + isRecursive
-                + "&readLock=none&noop=true&idempotentKey=${file:name}-${file:modified}");
+                + "&include=.*xml$&readLock=none&noop=true&idempotentKey=${file:name}-${file:modified}");
             // must wrap in unit of work
             task = new UnitOfWorkProducer(new UpdatedFileProcessor(endpoint));
             consumer = endpoint.createConsumer(task);
@@ -124,27 +124,23 @@ public class FileWatcherReloadStrategy extends ReloadStrategySupport {
         }
     }
 
-    protected class UpdatedFileProcessor extends DefaultProducer {
+    private class UpdatedFileProcessor extends DefaultProducer {
 
-        public UpdatedFileProcessor(Endpoint endpoint) {
+        private UpdatedFileProcessor(Endpoint endpoint) {
             super(endpoint);
         }
 
         @Override
         public void process(Exchange exchange) throws Exception {
             String name = exchange.getMessage().getHeader(Exchange.FILE_NAME, String.class);
-            log.trace("Modified/Created file: {}", name);
-
-            // must be an .xml file
-            if (name.toLowerCase(Locale.US).endsWith(".xml")) {
-                log.debug("Modified/Created XML file: {}", name);
-                try {
-                    InputStream is = exchange.getMessage().getMandatoryBody(InputStream.class);
-                    onReloadXml(getCamelContext(), name, is);
-                    IOHelper.close(is);
-                } catch (Exception e) {
-                    log.warn("Error reloading routes from file: " + name + " due " + e.getMessage() + ". This exception is ignored.", e);
-                }
+            log.debug("Modified/Created file: {}", name);
+
+            try {
+                InputStream is = exchange.getMessage().getMandatoryBody(InputStream.class);
+                onReloadXml(getCamelContext(), name, is);
+                IOHelper.close(is);
+            } catch (Exception e) {
+                log.warn("Error reloading routes from file: " + name + " due " + e.getMessage() + ". This exception is ignored.", e);
             }
         }
     }
diff --git a/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java b/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
index cf8c261..0f657d3 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
@@ -156,6 +156,7 @@ public class FileWatcherReloadStrategyTest extends ContextTestSupport {
 
         // create an xml file with some routes
         log.info("Copying file to target/dummy");
+        Thread.sleep(100);
         FileUtil.copyFile(new File("src/test/resources/org/apache/camel/model/barRoute.xml"), new File("target/dummy/barRoute.xml"));
 
         // wait for that file to be processed