How to use set states in routes

Prerequisite: How to configure states

Prevent from processing a file several times

In routing contexts, file, ftp and ftps behave the exact same way regarding states.

Consider that you have a state named incomingFiles.

The following routing context definition process the files only one time even after a node restart.

<routes xmlns:u="http://www.systar.com/aluminium/camel-util" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="file://D:/incoming?noop=true&amp;idempotentRepository=#incomingFiles" />
        <to uri="direct:processFile" />
    </route>
    <!-- Rest of the definition -->
</routes>

The  noop option indicates that the files aren't moved or deleted after processing.

Use the idempotentRepository option to configure which state to use. Its value, the state name, must start with a # (hash character).

By default, the state will contain the absolute paths of the processed files.  You can change this behavior by defining the idempotentKey option. For example: idempotentKey=${file:onlyname}-${file:modified} uses the file name (without path) and the modification date (in millisecond) as key.

Prevent processing the same element several times

There is a generic way to prevent from processing something other than a file several times.

Consider that you have a state named processed.

<routes xmlns:u="http://www.systar.com/aluminium/camel-util" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:input"/>
        <idempotentConsumer messageIdRepositoryRef="processed">
            <simple>${body[identifier]}</simple>
            <to uri="direct:process" />
        </idempotentConsumer>
    </route>
    <!-- Rest of the definition -->
</routes>

The expression defined on line 6 indicates how to retrieve the value to check and store in the state.

Related Links