How can Apache Camel be used to monitor file changes?

advertisements

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't seem to find a way to configure this behavior.

A uri like:

file:/some/directory

will consume the files in the provided directory but will delete them.

A uri like:

file:/some/directory?noop=true

consumes each file once when it is added or when the route is started.

It's surprising that there isn't an option along the lines of

consumeOnChange=true

Is there a straightforward way to monitor file changes and not delete the file after consuming?


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: http://camel.apache.org/file2

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