Pratiyush Kumar Singh
Pratiyush Kumar Singh

Reputation: 2007

How to poll last modified file out of multiple files and send to target endpoint in apache camel?

Here we have 4 files with different timestamp. We require to pick only latest one (first file with timestamp 18/08/2016 using Apache camel).

demo

How this can be implemented? I couldn't find much resource on this topic.

Upvotes: 3

Views: 1389

Answers (4)

Claus Ibsen
Claus Ibsen

Reputation: 55540

You can sort the files by timestamp, and then tell Camel to only pickup 1 file.

sortBy=file:modified&eagerMaxMessagesPerPoll=false&maxMessagesPerPoll=1

You would need to turn of eager max messages also. See more details in the file2 documentation for these options: http://camel.apache.org/file2

If you are consuming from a file directory with

from("file:...")

Then you need also to consider what to do with the file after its processed, should it be deleted / stay as-is (eg noop). For example if you delete the file, then Camel will just pickup the 2nd last modified file on next poll, and so on.

If you need to delete all the files, then I am afraid Camel do not have that out of the box, and you may need to write some logic that delete all those files yourself.

Upvotes: 5

ravthiru
ravthiru

Reputation: 9633

Check Camel's File Language. Looks like file:modified should help you

Upvotes: 0

Souciance Eqdam Rashti
Souciance Eqdam Rashti

Reputation: 3191

You can implement the method provided by Jordi Castilla using a filter in Camel. See doc here: http://camel.apache.org/file2.html See the section on using filter.

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 27003

Seems quite easy using File::lastModified() over a folder and loop into File::listFiles():

public static void main(String[] args) {
    final String folder = "D:\\Users\\tmp";
    final File file = new File(folder);

    long lastModified = Long.MAX_VALUE; 
    for (File f : file.listFiles()) {
        if (f.lastModified() < lastModified) 
            lastModified = f.lastModified();
    }

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    System.out.println("Oldest is " + sdf.format(lastModified));
}

In my tmp folder:

data.csv     08/08/2016
data.json    28/07/2016
index.html   17/06/2016
map.csv      29/07/2016

Output:

Oldest is 06/17/2016 09:53:10

Upvotes: 1

Related Questions