Search code examples
exceptionapache-camelcamel-ftp

camel ftp2 file throw error when there is no file present in the folder


I am working on reading the files from the sftp path and process the plain text files that are kept on the server every 5 hours. There is a requirement where I need to throw an exception when there is no file present/kept on the server by the producer. I am using the following to read the files

from(sftp://NUID@SERVER:PORT?&preferredAuthentications=password&delete=true)
            .routeId(ROUTE_ID)
            .log("${body}")
            .process(processor)
            .end();

Now if there is no file present when the above route starts it doesn't say anything once there is a file on the server it consumes and process it. I want to throw an exception if there is no file present during a period of time.


Solution

  • Some possible way to throw an exception when there is no file present on the target server.

    1. Use sendEmptyMessageWhenIdle option (from file)

    Set this option to true will let your route receive an exchange with empty message when the polling consumer scan no file present on the target server. Then, you can add a new step in your route to throw exception when an empty message (but not normal exchange) is encountered.

    2. Setup another route with timer component to check last file processing time

    In your original route, add a new step to record the last file processing time in somewhere, then have a new route to periodically check whether difference between the last update time and current time is in acceptable time range.

    Drawback: False alarm may occur from other problem (e.g. Continuous Network Issue)