How can Apache Camel be used to monitor file changes?

后端 未结 5 1780
孤街浪徒
孤街浪徒 2020-12-10 02:09

I would like to monitor all of the files in a given directory for changes, ie an updated timestamp. This use case seems natural for Camel using the file component, but I can

5条回答
  •  旧时难觅i
    2020-12-10 02:35

    I faced the same problem i.e. wanted to copy updated files also (along with new files). Below is my configuration,

    public static void main(String[] a) throws Exception {
    
        CamelContext cc = new DefaultCamelContext();
    
        cc.addRoutes(createRouteBuilder());
    
        cc.start();
    
        Thread.sleep(10 * 60 * 1000);
    
        cc.stop();
    }
    
    
    protected static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("file://D:/Production"
                        + "?idempotent=true"
                        + "&idempotentKey=${file:name}-${file:size}"
                        + "&include=.*.log"
                        + "&noop=true"
                        + "&readLock=changed")
    
                .to("file://D:/LogRepository");
            }
        };
    }
    

    My testing steps:

    1. Run the program and it copies few .log files from D:/Production to D:/LogRepository and then continues to poll D:/Production directory
    2. I opened a already copied log say A.log from D:/Production (since noop=true nothing is moved) and edited it with some editor tool. This doubled the file size and save it.

    At this point I think Camel is supposed to copy that particular file again since its size is modified and in my route definition I used "idempotent=true&idempotentKey=${file:name}-${file:size}&readLock=changed". But camel ignores the file. When I use TRACE for logging it says "Skipping as file is already in progress...", but I did not find any lock file in D:/Production directory when I editted and saved the file.

    I also checked that camel still ignores the file if I replace A.log (with same name but bigger size) in D:/Production directory from outside.

    But I found, everything is working as expected if I remove noop=true option.

    Am I missing something?

提交回复
热议问题