Camel Components

File Watch

Since Camel 3.0

Only consumer is supported

This component can be used to watch file modification events in the folder. It is based on the project directory-watcher.

URI Options

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The File Watch component supports the following options which are listed below.

Name Description Default Type

bridgeErrorHandler (consumer)

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 messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

useFileHashing (consumer)

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 than once.

true

boolean

autowiredEnabled (advanced)

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 type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

concurrentConsumers (advanced)

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 order than they occurred.

1

int

fileHasher (advanced)

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 content, and so reads every file in the directory tree in full when the consumer starts; #lastModifiedTimeFileHasher uses the last modified time instead, which suits large files. Not used when useFileHashing is false.

#murmur3FFileHasher

FileHasher

pollThreads (advanced)

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. 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.

1

int

queueSize (advanced)

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 means no limit.

2147483647

int

Endpoint Options

The File Watch endpoint is configured using URI syntax:

file-watch:path

With the following path and query parameters:

Path Parameters

Name Description Default Type

path (consumer)

Required 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.

String

Query Parameters

Name Description Default Type

antInclude (consumer)

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.

**

String

autoCreate (consumer)

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.

true

boolean

events (consumer)

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.

Enum values:

  • CREATE

  • MODIFY

  • DELETE

CREATE,MODIFY,DELETE

String

recursive (consumer)

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 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.

true

boolean

useFileHashing (consumer)

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 than once.

true

boolean

bridgeErrorHandler (consumer (advanced))

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 messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

concurrentConsumers (advanced)

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 order than they occurred.

1

int

fileHasher (advanced)

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 content, and so reads every file in the directory tree in full when the consumer starts; #lastModifiedTimeFileHasher uses the last modified time instead, which suits large files. Not used when useFileHashing is false.

#murmur3FFileHasher

FileHasher

pollThreads (advanced)

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. 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.

1

int

queueSize (advanced)

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 means no limit.

2147483647

int

Message Headers

The File Watch component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelFileEventType (consumer)

Constant: EVENT_TYPE_HEADER

The type of the event: CREATE, MODIFY or DELETE.

String

CamelFileNameOnly (consumer)

Constant: FILE_NAME_ONLY

Only the file name (the name with no leading paths).

String

CamelFileAbsolute (consumer)

Constant: FILE_ABSOLUTE

Always true, as the CamelFileAbsolutePath and CamelFilePath headers hold an absolute path.

Boolean

CamelFileAbsolutePath (consumer)

Constant: FILE_ABSOLUTE_PATH

The absolute path of the file or directory of the event, normalized and with / as separator.

String

CamelFilePath (consumer)

Constant: FILE_PATH

The absolute path of the file or directory of the event, the same value as the CamelFileAbsolutePath header.

String

CamelFileName (consumer)

Constant: FILE_NAME

The path of the file or directory of the event relative to the directory of the path option, with / as separator.

String

CamelFileRelativePath (consumer)

Constant: FILE_RELATIVE_PATH

The path relative to the directory of the path option, the same value as the CamelFileName header.

String

CamelFileNameConsumed (consumer)

Constant: FILE_NAME_CONSUMED

The path relative to the directory of the path option, the same value as the CamelFileName header.

String

CamelFileParent (consumer)

Constant: FILE_PARENT

The absolute path of the directory that holds the file or directory of the event, with / as separator.

String

CamelFileLastModified (consumer)

Constant: FILE_LAST_MODIFIED

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.

long

CamelMessageTimestamp (consumer)

Constant: MESSAGE_TIMESTAMP

The time the consumer received the event, in milliseconds since the epoch.

long

Message Body

The message body at each side of the File Watch component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

file (java.io.File)

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.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Examples

Recursive watch all events (file creation, file deletion, file modification):

  • Java

  • XML

  • YAML

from("file-watch://some-directory")
    .log("File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}");
<route>
  <from uri="file-watch://some-directory"/>
  <log message="File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}"/>
</route>
- route:
    from:
      uri: file-watch://some-directory
      steps:
        - log:
            message: "File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}"

Recursive watch for creation and deletion of txt files:

  • Java

  • XML

  • YAML

from("file-watch://some-directory?events=DELETE,CREATE&antInclude=**/*.txt")
    .log("File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}");
<route>
  <from uri="file-watch://some-directory?events=DELETE,CREATE&amp;antInclude=**/*.txt"/>
  <log message="File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}"/>
</route>
- route:
    from:
      uri: file-watch://some-directory
      parameters:
        events: "DELETE,CREATE"
        antInclude: "**/*.txt"
      steps:
        - log:
            message: "File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelMessageTimestamp}"

Create a snapshot of file when modified:

  • Java

  • XML

  • YAML

from("file-watch://some-directory?events=MODIFY&recursive=false")
    .setHeader(Exchange.FILE_NAME, simple("${header.CamelFileName}.${header.CamelFileLastModified}"))
    .to("file:some-directory/snapshots");
<route>
  <from uri="file-watch://some-directory?events=MODIFY&amp;recursive=false"/>
  <setHeader name="CamelFileName">
    <simple>${header.CamelFileName}.${header.CamelFileLastModified}</simple>
  </setHeader>
  <to uri="file:some-directory/snapshots"/>
</route>
- route:
    from:
      uri: file-watch://some-directory
      parameters:
        events: MODIFY
        recursive: false
      steps:
        - setHeader:
            name: CamelFileName
            expression:
              simple:
                expression: "${header.CamelFileName}.${header.CamelFileLastModified}"
        - to:
            uri: file:some-directory/snapshots