Check if file exists for content based routing

1.1k Views Asked by At

In one camel router, we are check if a flag file exists and only continue if it does. Here is the code we intend to use, but it does not work.

String flagFilePath = "file:" + flagFileFolder
        + "?noop=true&idempotent=false&fileName=" + flagFileName;
    from(flagFilePath)
        .choice()
        .when(header("CamelFileName").isNotNull())
            .log(LoggingLevel.TRACE, "Flag file exists.")
        .otherwise()
            .log(LoggingLevel.INFO, "Flag file does not exist.");

I understand that otherwise is not reachable here because the whole router is not triggered if the files does not exist.

Is there an easy way to check if the file exists without hand-writing Predicates?

(Note: As you can see in the code above, I need this condition to trigger a warning log.)

1

There are 1 best solutions below

0
On

I am going to re-post @ClausIbsen's comments as an answer. The reputation should all go to @ClausIbsen.

Adding sendEmptyMessageWhenIdle=true to the file URI solves my problem.

However, I decided to use a Predicate in my code.

    from("timer://a-processor?period=" + timerPeriod)
        .routeId("ATimer")
        .choice()
        .when(exchange -> Files.exists(Paths.get(flagFileFolder, flagFileName)))
            .log(LoggingLevel.DEBUG, "Flag file exists.")
        .otherwise()
            .log(LoggingLevel.DEBUG, "Flag file does not exist.")
        .end();

I find it more readable and more clear in semantics in my case.