How can Apache Camel be used to monitor file changes?

Janx picture Janx · Nov 20, 2013 · Viewed 13.5k times · Source

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?

Answer

Claus Ibsen picture Claus Ibsen · Nov 20, 2013

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