Search code examples
apache-camelspring-camel

Setting handled as true in onException block prevents redeliveries


I am reading a file from a directory, and trying to call an API based on the data in the file.

While trying to handle the exceptions, I am facing an issue. I am trying to configure the onException block to redeliver 3 times, with a delay of 5 seconds. The issue occurs, when I am setting handled(true). This configuration does not redeliver, and stops as soon as the exception occurs.

This is my onException block:

 onException(HttpOperationFailedException.class)
        .log(LoggingLevel.ERROR, logger, "Error occurred while connecting to API for file ${header.CamelFileName} :: ${exception.message}")
        .log("redelivery counter :: ${header.CamelRedeliveryCounter}")
        .maximumRedeliveries(3)
        .redeliveryDelay(5000)
        .handled(true);

How do I do both, i.e. handle as well as redeliver?


Solution

  • Unless you use a buggy version of Camel, the redeliveries are made as expected whatever if it is handled or not.

    The only difference between handled or not, is the fact that the result sent back to the client once the retries are exhausted will be either the exception (not handled) or the result of your onException (handled).

    Your mistake here, is the fact that you assume that the log EIPs that you have defined in your onException are called for each retry while they are actually called only when the retries are exhausted.

    If you want to see the retries in your logs, you can use retryAttemptedLogLevel as next:

     onException(HttpOperationFailedException.class)
            .maximumRedeliveries(3)
            .redeliveryDelay(5000)
            .retryAttemptedLogLevel(LoggingLevel.WARN);
    

    You will then get warning messages of type:

    Failed delivery for (MessageId: X on ExchangeId: Y). On delivery attempt: Z caught: ...