WalkerDev
WalkerDev

Reputation: 1322

How can I tell Camel to only copy a file if the Last Modified date is past a certain time?

I'm wondering if this is possible to achieve with Apache Camel. What I would like to do, is have Camel look at a directory of files, and only copy the ones whose "Last Modified" date is more recent than a certain date. For example, only copy files that were modified AFTER February 7, 2014. Basically I want to update a variable for the "Last Run Date" every time Camel runs, and then check if the files were modified after the Last Run.

I would like to use the actual timestamp on the file, not anything provided by Camel... it is my understanding that there is a deprecated method in Camel that used to stamp files when Camel looked at them, and then that would let you know whether they have been processed already or not. But this functionality is deprecated so I need an alternative.

Apache recommends moving or deleting the file after processing to know whether it has been processed, but this is not an option for me. Any ideas? Thanks in advance.

SOLVED (2014-02-10):

import java.util.Date;

import org.apache.camel.builder.RouteBuilder;

public class TestRoute extends RouteBuilder {

    static final long A_DAY = 86400000;

    @Override
    public void configure() throws Exception {

        Date yesterday = new Date(System.currentTimeMillis() - A_DAY);

        from("file://C:\\TestOutputFolder?noop=true").
            filter(header("CamelFileLastModified").isGreaterThan(yesterday)).
            to("file://C:\\TestInputFolder");

    }

}

No XML configuration required. Thanks for the answers below.

Upvotes: 5

Views: 7444

Answers (2)

SergeyB
SergeyB

Reputation: 9868

Take a look at Camel's File Language. Looks like file:modified might be what you are looking for.

example: filterFile=${file:modified} < ${date:now-24h}

Upvotes: 2

Claus Ibsen
Claus Ibsen

Reputation: 55540

Yes you can implement a filter and then return true|false if you want to include the file or not. In that logic you can check the file modification and see if the file is more than X days old etc.

See the Camel file docs at

And look for the filter option, eg where you implement org.apache.camel.component.file.GenericFileFilter interface.

Upvotes: 3

Related Questions