How can Apache Camel be used to monitor file changes?

I don't think Camel supports that specific feature but with the existent options you can come up with a similar solution of monitoring a directory.

What you need to do is set a small delay value to check the directory and maintain a repository of the already read files. Depending on how you configure the repository (by size, by filename, by a mix of them...) this solution would be able to provide you information about news files and modified files. As a caveat it would be consuming the files in the directory very often.

Maybe you could use other solutions different from Camel like Apache Commons VFS2 (I wrote a explanation about how to use it for this scenario: WatchService locks some files?


Setting noop to true will result in Camel setting idempotent=true as well, despite the fact that idempotent is false by default.

Simplest solution to monitor files would be:

.from("file:path?noop=true&idempotent=false&delay=60s")

This will monitor changes to all files in the given directory every one minute.

This can be found in the Camel documentation at: http://camel.apache.org/file2.html.


You can do this by setting up the idempotentKey to tell Camel how a file is considered changed. For example if the file size changes, or its timestamp changes etc.

See more details at the Camel file documentation at: https://camel.apache.org/components/latest/file-component.html

See the section Avoiding reading the same file more than once (idempotent consumer). And read about idempotent and idempotentKey.

So something alike

from("file:/somedir?noop=true&idempotentKey=${file:name}-${file:size}")

Or

from("file:/somedir?noop=true&idempotentKey=${file:name}-${file:modified}")

You can read here about the various ${file:xxx} tokens you can use: http://camel.apache.org/file-language.html