This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 95a4c947381b CAMEL-27603: camel-file-watch - events parsing,
non-recursive watch of the base directory only, pollThreads docs, metadata gaps
(#30988)
95a4c947381b is described below
commit 95a4c947381b650554e3a96fe3e0c508487424d6
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 27 22:18:10 2026 +0530
CAMEL-27603: camel-file-watch - events parsing, non-recursive watch of the
base directory only, pollThreads docs, metadata gaps (#30988)
1. The events option is split on commas and each value is trimmed and
upper-cased, so "CREATE, MODIFY" and "create" work. An unknown value
fails with a message that names it and lists the accepted values.
2. pollThreads: each thread runs watchAsync() on the same DirectoryWatcher,
which registers the watched directories again (and hashes their files
again with file hashing on). directory-watcher has no public way to run
an extra
event loop without registering again, and calling watchAsync once would
drop the extra threads that some users set for OVERFLOW. So the code is
unchanged, and the description of the option (endpoint and component)
now says what more threads cost and when they can help.
3. recursive=false now gives the DirectoryWatcher a FileTreeVisitor that
visits only the base directory and the entries directly in it, so the
subdirectories are not registered and their files are not hashed.
directory-watcher also uses the visitor to hash the tree at start and,
with the watch service of the file system that the consumer uses on
Linux and Windows, to report a created directory. So the visitor passes
a subdirectory of the base directory to onFile, never to onDirectory:
it is hashed (a deleted subdirectory gets its DELETE event with file
hashing on) and a created one gets its CREATE event, but it is not
registered. A subdirectory that is gone before the visitor looks at it
is no error. The filter that drops events from deeper levels stays, for
the watch services that always watch the whole tree (macOS, Windows).
4. The no-argument constructor of FileWatchEndpoint left
concurrentConsumers,
pollThreads, queueSize and useFileHashing at 0, 0, 0 and false. The
fields
now start with the documented defaults 1, 1, Integer.MAX_VALUE and true,
like the component.
5. FileWatchConstants has a MESSAGE_TIMESTAMP constant with metadata for
the CamelMessageTimestamp header that the consumer sets, so the header
list of the doc page and the catalog show it.
6. The events option has enums = "CREATE,MODIFY,DELETE" and multiEnum, so
tooling can offer and validate the values.
The component JSON, its catalog copy and the endpoint and component DSL are
regenerated. FileWatchConsumerOptionsTest covers points 1, 3 and 4.
FileWatchNonRecursiveDirectoryTest runs directory-watcher with the
watch service of the file system and the visitor of the consumer, as on
Linux, and checks the CREATE and DELETE events of a subdirectory.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/catalog/components/file-watch.json | 11 +-
.../camel/component/file/watch/file-watch.json | 11 +-
.../component/file/watch/FileWatchComponent.java | 6 +-
.../component/file/watch/FileWatchConstants.java | 3 +
.../component/file/watch/FileWatchConsumer.java | 68 ++++++++++-
.../component/file/watch/FileWatchEndpoint.java | 25 +++--
.../file/watch/FileWatchConsumerOptionsTest.java | 124 +++++++++++++++++++++
.../watch/FileWatchNonRecursiveDirectoryTest.java | 116 +++++++++++++++++++
.../dsl/FileWatchComponentBuilderFactory.java | 8 +-
.../dsl/FileWatchEndpointBuilderFactory.java | 42 ++++++-
10 files changed, 384 insertions(+), 30 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/file-watch.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/file-watch.json
index 9ef62fc56de1..b76b18477909 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/file-watch.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/file-watch.json
@@ -29,7 +29,7 @@
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
"concurrentConsumers": { "index": 3, "kind": "property", "displayName":
"Concurrent Consumers", "group": "advanced", "label": "advanced", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 1, "description": "The number of
threads that take the events from the queue and route them. Increase it when
the route is slower than the events arrive. With more than one thread, the
events can be routed in another or [...]
"fileHasher": { "index": 4, "kind": "property", "displayName": "File
Hasher", "group": "advanced", "label": "advanced", "required": false, "type":
"object", "javaType": "io.methvin.watcher.hashing.FileHasher", "deprecated":
false, "autowired": false, "secret": false, "defaultValue":
"#murmur3FFileHasher", "description": "The
io.methvin.watcher.hashing.FileHasher that hashes each file to drop duplicate
events, such as a MODIFY event of a file whose hash did not change. The default
#mu [...]
- "pollThreads": { "index": 5, "kind": "property", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events." },
+ "pollThreads": { "index": 5, "kind": "property", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events. [...]
"queueSize": { "index": 6, "kind": "property", "displayName": "Queue
Size", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 2147483647, "description": "The maximum number
of events in the queue between the pollThreads threads and the
concurrentConsumers threads. When the queue is full, new events are dropped and
a warning is logged once. A value of 0 or less me [...]
},
"headers": {
@@ -42,7 +42,8 @@
"CamelFileRelativePath": { "index": 6, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
path relative to the directory of the path option, the same value as the
CamelFileName header.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_RELATIVE_PATH" },
"CamelFileNameConsumed": { "index": 7, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
path relative to the directory of the path option, the same value as the
CamelFileName header.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_NAME_CONSUMED" },
"CamelFileParent": { "index": 8, "kind": "header", "displayName": "",
"group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
absolute path of the directory that holds the file or directory of the event,
with \/ as separator.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_PARENT" },
- "CamelFileLastModified": { "index": 9, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
last modified time of the file or directory, in milliseconds since the epoch.
Not set when the file no longer exists, such as for a DELETE event.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_LAST_MODIFIED" }
+ "CamelFileLastModified": { "index": 9, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
last modified time of the file or directory, in milliseconds since the epoch.
Not set when the file no longer exists, such as for a DELETE event.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_LAST_MODIFIED" },
+ "CamelMessageTimestamp": { "index": 10, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
time the consumer received the event, in milliseconds since the epoch.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#MESSAGE_TIMESTAMP" }
},
"bodyTypes": {
"consumerOut": { "carriers": [ "file" ], "javaType": "java.io.File",
"description": "The java.io.File of the file or directory of the event, also
for DELETE, with the event in the CamelFileEventType header. The consumer does
not read it: it converts to text, bytes or a stream by reading the file, which
fails after a DELETE" }
@@ -51,15 +52,15 @@
"path": { "index": 0, "kind": "path", "displayName": "Path", "group":
"consumer", "label": "", "required": true, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"description": "The directory to watch, as an absolute path or a path relative
to the working directory of the JVM. The consumer fails to start when the path
is not a directory, or when it does not exist and autoCreate is false." },
"antInclude": { "index": 1, "kind": "parameter", "displayName": "Ant
Include", "group": "consumer", "label": "", "required": false, "type":
"string", "javaType": "java.lang.String", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "**", "description": "An Ant-style
pattern, such as **\/*.txt, that the path of the file or directory relative to
the path option must match, with \/ as separator and no leading \/. The events
of the paths that do not match are drop [...]
"autoCreate": { "index": 2, "kind": "parameter", "displayName": "Auto
Create", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether the consumer
creates the directory, and its missing parent directories, when it does not
exist at start. If false, the consumer fails to start when the directory does
not exist." },
- "events": { "index": 3, "kind": "parameter", "displayName": "Events",
"group": "consumer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "CREATE,MODIFY,DELETE", "description": "The
types of the events to receive, as a comma-separated list of CREATE, MODIFY and
DELETE in upper case and without spaces. The events of the other types are
dropped." },
- "recursive": { "index": 4, "kind": "parameter", "displayName":
"Recursive", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to receive the
events of the files and directories in all subdirectories, including the
subdirectories created later. If false, only the events of the entries directly
in the directory are received; the whole dire [...]
+ "events": { "index": 3, "kind": "parameter", "displayName": "Events",
"group": "consumer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "CREATE", "MODIFY", "DELETE" ],
"multiEnum": true, "deprecated": false, "autowired": false, "secret": false,
"defaultValue": "CREATE,MODIFY,DELETE", "description": "The types of the events
to receive, as a comma-separated list of CREATE, MODIFY and DELETE, in any case
and with or without spaces around the [...]
+ "recursive": { "index": 4, "kind": "parameter", "displayName":
"Recursive", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to receive the
events of the files and directories in all subdirectories, including the
subdirectories created later. If false, only the events of the entries directly
in the directory are received, and the files [...]
"useFileHashing": { "index": 5, "kind": "parameter", "displayName": "Use
File Hashing", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to hash the
files with the fileHasher to drop duplicate events, as some platforms and JDKs
report a change more than once. If false, no file is hashed, and the route can
receive the same event more tha [...]
"bridgeErrorHandler": { "index": 6, "kind": "parameter", "displayName":
"Bridge Error Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Allows for bridging the consumer to the
Camel routing Error Handler, which mean any exceptions (if possible) occurred
while the Camel consumer is trying to pickup incoming [...]
"exceptionHandler": { "index": 7, "kind": "parameter", "displayName":
"Exception Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.",
"deprecated": false, "autowired": false, "secret": false, "description": "To
let the consumer use a custom ExceptionHandler. Notice if the option
bridgeErrorHandler is enabled then this option is not in use. By def [...]
"exchangePattern": { "index": 8, "kind": "parameter", "displayName":
"Exchange Pattern", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "enum", "javaType":
"org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ],
"deprecated": false, "autowired": false, "secret": false, "description": "Sets
the exchange pattern when the consumer creates an exchange." },
"concurrentConsumers": { "index": 9, "kind": "parameter", "displayName":
"Concurrent Consumers", "group": "advanced", "label": "advanced", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 1, "description": "The number of
threads that take the events from the queue and route them. Increase it when
the route is slower than the events arrive. With more than one thread, the
events can be routed in another o [...]
"fileHasher": { "index": 10, "kind": "parameter", "displayName": "File
Hasher", "group": "advanced", "label": "advanced", "required": false, "type":
"object", "javaType": "io.methvin.watcher.hashing.FileHasher", "deprecated":
false, "autowired": false, "secret": false, "defaultValue":
"#murmur3FFileHasher", "description": "The
io.methvin.watcher.hashing.FileHasher that hashes each file to drop duplicate
events, such as a MODIFY event of a file whose hash did not change. The default
# [...]
- "pollThreads": { "index": 11, "kind": "parameter", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events." },
+ "pollThreads": { "index": 11, "kind": "parameter", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped event [...]
"queueSize": { "index": 12, "kind": "parameter", "displayName": "Queue
Size", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 2147483647, "description": "The maximum number
of events in the queue between the pollThreads threads and the
concurrentConsumers threads. When the queue is full, new events are dropped and
a warning is logged once. A value of 0 or less [...]
}
}
diff --git
a/components/camel-file-watch/src/generated/resources/META-INF/org/apache/camel/component/file/watch/file-watch.json
b/components/camel-file-watch/src/generated/resources/META-INF/org/apache/camel/component/file/watch/file-watch.json
index 9ef62fc56de1..b76b18477909 100644
---
a/components/camel-file-watch/src/generated/resources/META-INF/org/apache/camel/component/file/watch/file-watch.json
+++
b/components/camel-file-watch/src/generated/resources/META-INF/org/apache/camel/component/file/watch/file-watch.json
@@ -29,7 +29,7 @@
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Whether autowiring is enabled. This is used for automatic autowiring options
(the option must be marked as autowired) by looking up in the registry to find
if there is a single instance of matching t [...]
"concurrentConsumers": { "index": 3, "kind": "property", "displayName":
"Concurrent Consumers", "group": "advanced", "label": "advanced", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 1, "description": "The number of
threads that take the events from the queue and route them. Increase it when
the route is slower than the events arrive. With more than one thread, the
events can be routed in another or [...]
"fileHasher": { "index": 4, "kind": "property", "displayName": "File
Hasher", "group": "advanced", "label": "advanced", "required": false, "type":
"object", "javaType": "io.methvin.watcher.hashing.FileHasher", "deprecated":
false, "autowired": false, "secret": false, "defaultValue":
"#murmur3FFileHasher", "description": "The
io.methvin.watcher.hashing.FileHasher that hashes each file to drop duplicate
events, such as a MODIFY event of a file whose hash did not change. The default
#mu [...]
- "pollThreads": { "index": 5, "kind": "property", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events." },
+ "pollThreads": { "index": 5, "kind": "property", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events. [...]
"queueSize": { "index": 6, "kind": "property", "displayName": "Queue
Size", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 2147483647, "description": "The maximum number
of events in the queue between the pollThreads threads and the
concurrentConsumers threads. When the queue is full, new events are dropped and
a warning is logged once. A value of 0 or less me [...]
},
"headers": {
@@ -42,7 +42,8 @@
"CamelFileRelativePath": { "index": 6, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
path relative to the directory of the path option, the same value as the
CamelFileName header.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_RELATIVE_PATH" },
"CamelFileNameConsumed": { "index": 7, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
path relative to the directory of the path option, the same value as the
CamelFileName header.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_NAME_CONSUMED" },
"CamelFileParent": { "index": 8, "kind": "header", "displayName": "",
"group": "consumer", "label": "", "required": false, "javaType": "String",
"deprecated": false, "autowired": false, "secret": false, "description": "The
absolute path of the directory that holds the file or directory of the event,
with \/ as separator.", "constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_PARENT" },
- "CamelFileLastModified": { "index": 9, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
last modified time of the file or directory, in milliseconds since the epoch.
Not set when the file no longer exists, such as for a DELETE event.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_LAST_MODIFIED" }
+ "CamelFileLastModified": { "index": 9, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
last modified time of the file or directory, in milliseconds since the epoch.
Not set when the file no longer exists, such as for a DELETE event.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#FILE_LAST_MODIFIED" },
+ "CamelMessageTimestamp": { "index": 10, "kind": "header", "displayName":
"", "group": "consumer", "label": "", "required": false, "javaType": "long",
"deprecated": false, "autowired": false, "secret": false, "description": "The
time the consumer received the event, in milliseconds since the epoch.",
"constantName":
"org.apache.camel.component.file.watch.FileWatchConstants#MESSAGE_TIMESTAMP" }
},
"bodyTypes": {
"consumerOut": { "carriers": [ "file" ], "javaType": "java.io.File",
"description": "The java.io.File of the file or directory of the event, also
for DELETE, with the event in the CamelFileEventType header. The consumer does
not read it: it converts to text, bytes or a stream by reading the file, which
fails after a DELETE" }
@@ -51,15 +52,15 @@
"path": { "index": 0, "kind": "path", "displayName": "Path", "group":
"consumer", "label": "", "required": true, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"description": "The directory to watch, as an absolute path or a path relative
to the working directory of the JVM. The consumer fails to start when the path
is not a directory, or when it does not exist and autoCreate is false." },
"antInclude": { "index": 1, "kind": "parameter", "displayName": "Ant
Include", "group": "consumer", "label": "", "required": false, "type":
"string", "javaType": "java.lang.String", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "**", "description": "An Ant-style
pattern, such as **\/*.txt, that the path of the file or directory relative to
the path option must match, with \/ as separator and no leading \/. The events
of the paths that do not match are drop [...]
"autoCreate": { "index": 2, "kind": "parameter", "displayName": "Auto
Create", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether the consumer
creates the directory, and its missing parent directories, when it does not
exist at start. If false, the consumer fails to start when the directory does
not exist." },
- "events": { "index": 3, "kind": "parameter", "displayName": "Events",
"group": "consumer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "CREATE,MODIFY,DELETE", "description": "The
types of the events to receive, as a comma-separated list of CREATE, MODIFY and
DELETE in upper case and without spaces. The events of the other types are
dropped." },
- "recursive": { "index": 4, "kind": "parameter", "displayName":
"Recursive", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to receive the
events of the files and directories in all subdirectories, including the
subdirectories created later. If false, only the events of the entries directly
in the directory are received; the whole dire [...]
+ "events": { "index": 3, "kind": "parameter", "displayName": "Events",
"group": "consumer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "CREATE", "MODIFY", "DELETE" ],
"multiEnum": true, "deprecated": false, "autowired": false, "secret": false,
"defaultValue": "CREATE,MODIFY,DELETE", "description": "The types of the events
to receive, as a comma-separated list of CREATE, MODIFY and DELETE, in any case
and with or without spaces around the [...]
+ "recursive": { "index": 4, "kind": "parameter", "displayName":
"Recursive", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to receive the
events of the files and directories in all subdirectories, including the
subdirectories created later. If false, only the events of the entries directly
in the directory are received, and the files [...]
"useFileHashing": { "index": 5, "kind": "parameter", "displayName": "Use
File Hashing", "group": "consumer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Whether to hash the
files with the fileHasher to drop duplicate events, as some platforms and JDKs
report a change more than once. If false, no file is hashed, and the route can
receive the same event more tha [...]
"bridgeErrorHandler": { "index": 6, "kind": "parameter", "displayName":
"Bridge Error Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Allows for bridging the consumer to the
Camel routing Error Handler, which mean any exceptions (if possible) occurred
while the Camel consumer is trying to pickup incoming [...]
"exceptionHandler": { "index": 7, "kind": "parameter", "displayName":
"Exception Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.",
"deprecated": false, "autowired": false, "secret": false, "description": "To
let the consumer use a custom ExceptionHandler. Notice if the option
bridgeErrorHandler is enabled then this option is not in use. By def [...]
"exchangePattern": { "index": 8, "kind": "parameter", "displayName":
"Exchange Pattern", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "enum", "javaType":
"org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ],
"deprecated": false, "autowired": false, "secret": false, "description": "Sets
the exchange pattern when the consumer creates an exchange." },
"concurrentConsumers": { "index": 9, "kind": "parameter", "displayName":
"Concurrent Consumers", "group": "advanced", "label": "advanced", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": 1, "description": "The number of
threads that take the events from the queue and route them. Increase it when
the route is slower than the events arrive. With more than one thread, the
events can be routed in another o [...]
"fileHasher": { "index": 10, "kind": "parameter", "displayName": "File
Hasher", "group": "advanced", "label": "advanced", "required": false, "type":
"object", "javaType": "io.methvin.watcher.hashing.FileHasher", "deprecated":
false, "autowired": false, "secret": false, "defaultValue":
"#murmur3FFileHasher", "description": "The
io.methvin.watcher.hashing.FileHasher that hashes each file to drop duplicate
events, such as a MODIFY event of a file whose hash did not change. The default
# [...]
- "pollThreads": { "index": 11, "kind": "parameter", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped events." },
+ "pollThreads": { "index": 11, "kind": "parameter", "displayName": "Poll
Threads", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 1, "description": "The number of threads that
read the events from the watch service of the directory and put them in the
queue. Increase it when the log warns that an OVERFLOW occurred, which means
that the file system dropped event [...]
"queueSize": { "index": 12, "kind": "parameter", "displayName": "Queue
Size", "group": "advanced", "label": "advanced", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 2147483647, "description": "The maximum number
of events in the queue between the pollThreads threads and the
concurrentConsumers threads. When the queue is full, new events are dropped and
a warning is logged once. A value of 0 or less [...]
}
}
diff --git
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchComponent.java
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchComponent.java
index e156f1a7f16b..2521fd87e5ea 100644
---
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchComponent.java
+++
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchComponent.java
@@ -48,7 +48,11 @@ public class FileWatchComponent extends DefaultComponent {
@Metadata(label = "advanced",
description = "The number of threads that read the events from
the watch service of the directory and put them in"
+ " the queue. Increase it when the log warns that
an OVERFLOW occurred, which means that the file"
- + " system dropped events.",
+ + " system dropped events. All the threads read
the same watch service, and each one registers the"
+ + " watched directories again when the consumer
starts (and hashes their files again when"
+ + " useFileHashing is enabled), so on a large tree
a higher value makes the start slower. More"
+ + " threads can only help when the events come
from several directories at the same time, as the"
+ + " events of one directory are read by one thread
at a time.",
defaultValue = "1")
private int pollThreads = 1;
diff --git
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConstants.java
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConstants.java
index 89e97d10e951..8125813dcf3f 100644
---
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConstants.java
+++
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConstants.java
@@ -55,6 +55,9 @@ public final class FileWatchConstants {
+ " the file no longer exists, such as for a
DELETE event.",
javaType = "long")
public static final String FILE_LAST_MODIFIED =
Exchange.FILE_LAST_MODIFIED;
+ @Metadata(description = "The time the consumer received the event, in
milliseconds since the epoch.",
+ javaType = "long")
+ public static final String MESSAGE_TIMESTAMP = Exchange.MESSAGE_TIMESTAMP;
private FileWatchConstants() {
// Utility class
diff --git
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConsumer.java
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConsumer.java
index 089c67066a64..9ad1fd5c4faa 100644
---
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConsumer.java
+++
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchConsumer.java
@@ -18,10 +18,13 @@ package org.apache.camel.component.file.watch;
import java.io.File;
import java.io.IOException;
+import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.Arrays;
import java.util.HashSet;
+import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
@@ -31,6 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import io.methvin.watcher.DirectoryChangeEvent;
import io.methvin.watcher.DirectoryChangeListener;
import io.methvin.watcher.DirectoryWatcher;
+import io.methvin.watcher.visitor.FileTreeVisitor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
@@ -75,8 +79,18 @@ public class FileWatchConsumer extends DefaultConsumer {
super.doInit();
for (String event : getEndpoint().getEvents().split(",")) {
- FileEventEnum fe = FileEventEnum.valueOf(event);
- events.add(fe);
+ String name = event.trim();
+ if (name.isEmpty()) {
+ continue;
+ }
+ try {
+
events.add(FileEventEnum.valueOf(name.toUpperCase(Locale.ROOT)));
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(
+ "Unknown event '" + name + "' in the events option,
the accepted values are "
+ +
Arrays.toString(FileEventEnum.values()),
+ e);
+ }
}
}
@@ -112,6 +126,11 @@ public class FileWatchConsumer extends DefaultConsumer {
watcherBuilder.watchService(this.baseDirectory.getFileSystem().newWatchService());
}
+ if (!getEndpoint().isRecursive()) {
+ // only the base directory is watched (and its files hashed), not
the whole tree below it
+ watcherBuilder.fileTreeVisitor(new
BaseDirectoryVisitor(baseDirectory));
+ }
+
watcherBuilder.fileHashing(getEndpoint().isUseFileHashing());
if (getEndpoint().getFileHasher() != null &&
getEndpoint().isUseFileHashing()) {
watcherBuilder.fileHasher(getEndpoint().getFileHasher());
@@ -180,7 +199,7 @@ public class FileWatchConsumer extends DefaultConsumer {
if (lastModified != null) {
message.setHeader(FileWatchConstants.FILE_LAST_MODIFIED,
lastModified);
}
- message.setHeader(Exchange.MESSAGE_TIMESTAMP, event.getEventDate());
+ message.setHeader(FileWatchConstants.MESSAGE_TIMESTAMP,
event.getEventDate());
return exchange;
}
@@ -253,6 +272,49 @@ public class FileWatchConsumer extends DefaultConsumer {
return false;
}
+ /**
+ * Visits only the base directory and the entries directly in it, so that
a non-recursive watch does not register
+ * (or hash the files of) a subdirectory, also not one created later.
directory-watcher calls the visitor to hash
+ * the tree at start, to register the directories, and (except with the
watch service of macOS) to report a created
+ * directory. So a subdirectory directly in the base directory is passed
to onFile, never to onDirectory: it is
+ * hashed, which gives the DELETE event of a deleted subdirectory, and a
created one is reported as CREATE, but it
+ * is not registered. The events from deeper levels that a platform
reports anyway (the watch services of macOS and
+ * Windows watch the whole tree) are dropped by {@link
#matchFilters(FileEvent)}.
+ */
+ static final class BaseDirectoryVisitor implements FileTreeVisitor {
+ private final Path baseDirectory;
+
+ BaseDirectoryVisitor(Path baseDirectory) {
+ this.baseDirectory = baseDirectory;
+ }
+
+ @Override
+ public void recursiveVisitFiles(Path file, Callback onDirectory,
Callback onFile) throws IOException {
+ if (isBaseDirectory(file)) {
+ onDirectory.call(file);
+ try (DirectoryStream<Path> entries =
Files.newDirectoryStream(file)) {
+ for (Path entry : entries) {
+ onFile.call(entry);
+ }
+ }
+ } else if (file.getParent() != null &&
isBaseDirectory(file.getParent())) {
+ onFile.call(file);
+ }
+ }
+
+ private boolean isBaseDirectory(Path path) {
+ if (path.equals(baseDirectory)) {
+ return true;
+ }
+ try {
+ return Files.isSameFile(path, baseDirectory);
+ } catch (IOException e) {
+ // such as a directory that was removed right after it was
created
+ return false;
+ }
+ }
+ }
+
class FileWatchDirectoryChangeListener implements DirectoryChangeListener {
@Override
public void onEvent(DirectoryChangeEvent directoryChangeEvent) {
diff --git
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchEndpoint.java
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchEndpoint.java
index 4bdaf5c63a45..b49b5df3a579 100644
---
a/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchEndpoint.java
+++
b/components/camel-file-watch/src/main/java/org/apache/camel/component/file/watch/FileWatchEndpoint.java
@@ -48,9 +48,10 @@ public class FileWatchEndpoint extends DefaultEndpoint
implements MultipleConsum
+ " autoCreate is false.")
@Metadata(required = true)
private String path;
- @UriParam(description = "The types of the events to receive, as a
comma-separated list of CREATE, MODIFY and DELETE in"
- + " upper case and without spaces. The events of
the other types are dropped.",
- defaultValue = "CREATE,MODIFY,DELETE")
+ @UriParam(description = "The types of the events to receive, as a
comma-separated list of CREATE, MODIFY and DELETE, in"
+ + " any case and with or without spaces around the
values. An unknown value fails the start of"
+ + " the consumer. The events of the other types
are dropped.",
+ defaultValue = "CREATE,MODIFY,DELETE", enums =
"CREATE,MODIFY,DELETE", multiEnum = true)
private String events = "CREATE,MODIFY,DELETE";
@UriParam(description = "Whether the consumer creates the directory, and
its missing parent directories, when it does not"
+ " exist at start. If false, the consumer fails
to start when the directory does not exist.",
@@ -58,7 +59,9 @@ public class FileWatchEndpoint extends DefaultEndpoint
implements MultipleConsum
private boolean autoCreate = true;
@UriParam(description = "Whether to receive the events of the files and
directories in all subdirectories, including the"
+ " subdirectories created later. If false, only
the events of the entries directly in the"
- + " directory are received; the whole directory
tree is still watched.",
+ + " directory are received, and the files of the
subdirectories are not hashed. The subdirectories"
+ + " are not watched either, except where the watch
service always watches the whole directory"
+ + " tree, such as on macOS.",
defaultValue = "true")
private boolean recursive = true;
@UriParam(label = "advanced",
@@ -66,13 +69,17 @@ public class FileWatchEndpoint extends DefaultEndpoint
implements MultipleConsum
+ " route is slower than the events arrive. With
more than one thread, the events can be routed in"
+ " another order than they occurred.",
defaultValue = "1")
- private int concurrentConsumers;
+ private int concurrentConsumers = 1;
@UriParam(label = "advanced",
description = "The number of threads that read the events from
the watch service of the directory and put them in"
+ " the queue. Increase it when the log warns that
an OVERFLOW occurred, which means that the file"
- + " system dropped events.",
+ + " system dropped events. All the threads read
the same watch service, and each one registers the"
+ + " watched directories again when the consumer
starts (and hashes their files again when"
+ + " useFileHashing is enabled), so on a large tree
a higher value makes the start slower. More"
+ + " threads can only help when the events come
from several directories at the same time, as the"
+ + " events of one directory are read by one thread
at a time.",
defaultValue = "1")
- private int pollThreads;
+ private int pollThreads = 1;
@UriParam(description = "An Ant-style pattern, such as **/*.txt, that the
path of the file or directory relative to the"
+ " path option must match, with / as separator
and no leading /. The events of the paths that do"
+ " not match are dropped, and an empty pattern
matches all paths.",
@@ -83,7 +90,7 @@ public class FileWatchEndpoint extends DefaultEndpoint
implements MultipleConsum
+ " concurrentConsumers threads. When the queue is
full, new events are dropped and a warning is"
+ " logged once. A value of 0 or less means no
limit.",
defaultValue = "" + Integer.MAX_VALUE)
- private int queueSize;
+ private int queueSize = Integer.MAX_VALUE;
@UriParam(label = "advanced",
description = "The io.methvin.watcher.hashing.FileHasher that
hashes each file to drop duplicate events, such as"
+ " a MODIFY event of a file whose hash did not
change. The default #murmur3FFileHasher hashes the"
@@ -96,7 +103,7 @@ public class FileWatchEndpoint extends DefaultEndpoint
implements MultipleConsum
+ " report a change more than once. If false, no
file is hashed, and the route can receive the same"
+ " event more than once.",
defaultValue = "true")
- private boolean useFileHashing;
+ private boolean useFileHashing = true;
public FileWatchEndpoint() {
}
diff --git
a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchConsumerOptionsTest.java
b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchConsumerOptionsTest.java
new file mode 100644
index 000000000000..8f76d2978e0c
--- /dev/null
+++
b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchConsumerOptionsTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.watch;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import io.methvin.watcher.hashing.FileHash;
+import io.methvin.watcher.hashing.FileHasher;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The events option, the files a non-recursive watch hashes, and the defaults
of an endpoint made without the
+ * component.
+ */
+class FileWatchConsumerOptionsTest {
+
+ @TempDir
+ Path dir;
+
+ @Test
+ void eventsWithSpacesAndLowerCaseAreAccepted() throws Exception {
+ try (CamelContext context = new DefaultCamelContext()) {
+ FileWatchEndpoint endpoint = context.getEndpoint("file-watch:" +
dir, FileWatchEndpoint.class);
+ endpoint.setEvents(" create , Modify");
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(endpoint).to("mock:events");
+ }
+ });
+ context.start();
+
+ MockEndpoint mock = context.getEndpoint("mock:events",
MockEndpoint.class);
+ mock.expectedMinimumMessageCount(1);
+
mock.message(0).header(FileWatchConstants.EVENT_TYPE_HEADER).isEqualTo("CREATE");
+ Files.writeString(dir.resolve("created.txt"), "camel");
+
+ mock.assertIsSatisfied();
+ }
+ }
+
+ @Test
+ void anUnknownEventNamesTheAcceptedValues() throws Exception {
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("file-watch:" + dir +
"?events=CREATE,MOVE").to("mock:events");
+ }
+ });
+
+ Exception e = assertThrows(Exception.class, context::start);
+ Throwable cause = e;
+ while (!(cause instanceof IllegalArgumentException) &&
cause.getCause() != null) {
+ cause = cause.getCause();
+ }
+ assertTrue(cause.getMessage().contains("'MOVE'") &&
cause.getMessage().contains("[CREATE, DELETE, MODIFY]"),
+ cause.getMessage());
+ }
+ }
+
+ @Test
+ void aNonRecursiveWatchHashesOnlyTheFilesOfTheBaseDirectory() throws
Exception {
+ Path top = Files.writeString(dir.resolve("top.txt"), "camel");
+ Path inner =
Files.writeString(Files.createDirectories(dir.resolve("sub")).resolve("inner.txt"),
"camel");
+ Set<Path> hashed = ConcurrentHashMap.newKeySet();
+ FileHasher hasher = path -> {
+ hashed.add(path.toRealPath());
+ return FileHash.fromLong(1L);
+ };
+
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.getRegistry().bind("recordingHasher", hasher);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("file-watch:" + dir +
"?recursive=false&fileHasher=#recordingHasher").to("mock:events");
+ }
+ });
+ // the consumer registers the directories and hashes their files
when it starts
+ context.start();
+
+ assertTrue(hashed.contains(top.toRealPath()), "hashed: " + hashed);
+ assertFalse(hashed.contains(inner.toRealPath()), "a file of a
subdirectory was hashed: " + hashed);
+ }
+ }
+
+ @Test
+ void anEndpointMadeWithoutTheComponentHasTheDocumentedDefaults() {
+ FileWatchEndpoint endpoint = new FileWatchEndpoint();
+
+ assertEquals(1, endpoint.getConcurrentConsumers());
+ assertEquals(1, endpoint.getPollThreads());
+ assertEquals(Integer.MAX_VALUE, endpoint.getQueueSize());
+ assertTrue(endpoint.isUseFileHashing());
+ }
+}
diff --git
a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchNonRecursiveDirectoryTest.java
b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchNonRecursiveDirectoryTest.java
new file mode 100644
index 000000000000..02730346720f
--- /dev/null
+++
b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchNonRecursiveDirectoryTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.watch;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import io.methvin.watcher.DirectoryChangeEvent;
+import io.methvin.watcher.DirectoryWatcher;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * A non-recursive watch reports a subdirectory created in, or deleted from,
the base directory. The tests run
+ * directory-watcher the way the consumer does on Linux and Windows: with a
watch service of the file system, which
+ * reports a created directory through the file tree visitor. On macOS the
consumer leaves the watch service to
+ * directory-watcher, which reports a created directory without the visitor,
so a route test there does not cover this.
+ */
+class FileWatchNonRecursiveDirectoryTest {
+
+ @TempDir
+ Path dir;
+
+ @Test
+ void aCreatedSubdirectoryIsReported() throws Exception {
+ BlockingQueue<DirectoryChangeEvent> events = new
LinkedBlockingQueue<>();
+ DirectoryWatcher watcher = startWatcher(events);
+ try {
+ Path created = Files.createDirectory(dir.resolve("created"));
+
+ awaitEvent(events, DirectoryChangeEvent.EventType.CREATE, created);
+ } finally {
+ watcher.close();
+ }
+ }
+
+ @Test
+ void aDeletedSubdirectoryIsReported() throws Exception {
+ Path existing = Files.createDirectory(dir.resolve("existing"));
+ BlockingQueue<DirectoryChangeEvent> events = new
LinkedBlockingQueue<>();
+ DirectoryWatcher watcher = startWatcher(events);
+ try {
+ // with file hashing, a DELETE is only reported for a path that
was hashed when the watch started
+ Files.delete(existing);
+
+ awaitEvent(events, DirectoryChangeEvent.EventType.DELETE,
existing);
+ } finally {
+ watcher.close();
+ }
+ }
+
+ @Test
+ void aSubdirectoryRemovedRightAfterItWasCreatedIsNoError() throws
Exception {
+ Path gone = dir.resolve("gone");
+ List<Path> directories = new ArrayList<>();
+ List<Path> files = new ArrayList<>();
+
+ // what directory-watcher does for the CREATE event of a directory
that no longer exists
+ new
FileWatchConsumer.BaseDirectoryVisitor(dir).recursiveVisitFiles(gone,
directories::add, files::add);
+
+ assertEquals(List.of(), directories);
+ assertEquals(List.of(gone), files);
+ }
+
+ private DirectoryWatcher startWatcher(BlockingQueue<DirectoryChangeEvent>
events) throws Exception {
+ DirectoryWatcher watcher = DirectoryWatcher.builder()
+ .path(dir)
+ .watchService(dir.getFileSystem().newWatchService())
+ .fileTreeVisitor(new
FileWatchConsumer.BaseDirectoryVisitor(dir))
+ .fileHashing(true)
+ .listener(events::add)
+ .build();
+ // registers the base directory and hashes its entries before it
returns
+ watcher.watchAsync();
+ return watcher;
+ }
+
+ private static void awaitEvent(BlockingQueue<DirectoryChangeEvent> events,
DirectoryChangeEvent.EventType type, Path path)
+ throws InterruptedException {
+ List<DirectoryChangeEvent> others = new ArrayList<>();
+ // the polling watch service of the JDK on macOS checks the directory
every few seconds
+ long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
+ while (System.nanoTime() < deadline) {
+ DirectoryChangeEvent event = events.poll(100,
TimeUnit.MILLISECONDS);
+ if (event != null) {
+ if (event.eventType() == type && event.path().equals(path)) {
+ return;
+ }
+ others.add(event);
+ }
+ }
+ fail("No " + type + " event for " + path + ", other events: " +
others);
+ }
+}
diff --git
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/FileWatchComponentBuilderFactory.java
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/FileWatchComponentBuilderFactory.java
index 10dea4e1ad3b..ebf42545ae10 100644
---
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/FileWatchComponentBuilderFactory.java
+++
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/FileWatchComponentBuilderFactory.java
@@ -171,7 +171,13 @@ public interface FileWatchComponentBuilderFactory {
* The number of threads that read the events from the watch service of
* the directory and put them in the queue. Increase it when the log
* warns that an OVERFLOW occurred, which means that the file system
- * dropped events.
+ * dropped events. All the threads read the same watch service, and
each
+ * one registers the watched directories again when the consumer starts
+ * (and hashes their files again when useFileHashing is enabled), so on
+ * a large tree a higher value makes the start slower. More threads can
+ * only help when the events come from several directories at the same
+ * time, as the events of one directory are read by one thread at a
+ * time.
*
* The option is a: <code>int</code> type.
*
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FileWatchEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FileWatchEndpointBuilderFactory.java
index 53bdfded6f68..b80df0c2ee17 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FileWatchEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/FileWatchEndpointBuilderFactory.java
@@ -99,8 +99,9 @@ public interface FileWatchEndpointBuilderFactory {
}
/**
* The types of the events to receive, as a comma-separated list of
- * CREATE, MODIFY and DELETE in upper case and without spaces. The
- * events of the other types are dropped.
+ * CREATE, MODIFY and DELETE, in any case and with or without spaces
+ * around the values. An unknown value fails the start of the consumer.
+ * The events of the other types are dropped.
*
* The option is a: <code>java.lang.String</code> type.
*
@@ -118,7 +119,9 @@ public interface FileWatchEndpointBuilderFactory {
* Whether to receive the events of the files and directories in all
* subdirectories, including the subdirectories created later. If
false,
* only the events of the entries directly in the directory are
- * received; the whole directory tree is still watched.
+ * received, and the files of the subdirectories are not hashed. The
+ * subdirectories are not watched either, except where the watch
service
+ * always watches the whole directory tree, such as on macOS.
*
* The option is a: <code>boolean</code> type.
*
@@ -136,7 +139,9 @@ public interface FileWatchEndpointBuilderFactory {
* Whether to receive the events of the files and directories in all
* subdirectories, including the subdirectories created later. If
false,
* only the events of the entries directly in the directory are
- * received; the whole directory tree is still watched.
+ * received, and the files of the subdirectories are not hashed. The
+ * subdirectories are not watched either, except where the watch
service
+ * always watches the whole directory tree, such as on macOS.
*
* The option will be converted to a <code>boolean</code> type.
*
@@ -399,7 +404,13 @@ public interface FileWatchEndpointBuilderFactory {
* The number of threads that read the events from the watch service of
* the directory and put them in the queue. Increase it when the log
* warns that an OVERFLOW occurred, which means that the file system
- * dropped events.
+ * dropped events. All the threads read the same watch service, and
each
+ * one registers the watched directories again when the consumer starts
+ * (and hashes their files again when useFileHashing is enabled), so on
+ * a large tree a higher value makes the start slower. More threads can
+ * only help when the events come from several directories at the same
+ * time, as the events of one directory are read by one thread at a
+ * time.
*
* The option is a: <code>int</code> type.
*
@@ -417,7 +428,13 @@ public interface FileWatchEndpointBuilderFactory {
* The number of threads that read the events from the watch service of
* the directory and put them in the queue. Increase it when the log
* warns that an OVERFLOW occurred, which means that the file system
- * dropped events.
+ * dropped events. All the threads read the same watch service, and
each
+ * one registers the watched directories again when the consumer starts
+ * (and hashes their files again when useFileHashing is enabled), so on
+ * a large tree a higher value makes the start slower. More threads can
+ * only help when the events come from several directories at the same
+ * time, as the events of one directory are read by one thread at a
+ * time.
*
* The option will be converted to a <code>int</code> type.
*
@@ -673,6 +690,19 @@ public interface FileWatchEndpointBuilderFactory {
public String fileLastModified() {
return "CamelFileLastModified";
}
+ /**
+ * The time the consumer received the event, in milliseconds since the
+ * epoch.
+ *
+ * The option is a: {@code long} type.
+ *
+ * Group: consumer
+ *
+ * @return the name of the header {@code MessageTimestamp}.
+ */
+ public String messageTimestamp() {
+ return "CamelMessageTimestamp";
+ }
}
static FileWatchEndpointBuilder endpointBuilder(String componentName,
String path) {
class FileWatchEndpointBuilderImpl extends AbstractEndpointBuilder
implements FileWatchEndpointBuilder, AdvancedFileWatchEndpointBuilder {