Integration / Apache Camel Interview Questions
How does onException work in Camel and how do you configure retry, redelivery, and backoff?
onException(ExceptionClass.class) defines per-exception handling rules that override the route error handler for matched exception types. It must be declared BEFORE the from() in the RouteBuilder (in the configure() method). Multiple onException() clauses can coexist; Camel matches the closest superclass.
public void configure() {
// Per-exception handling
onException(IOException.class)
.maximumRedeliveries(5)
.redeliveryDelay(2000)
.backOffMultiplier(2.0)
.maximumRedeliveryDelay(30000) // cap at 30s
.retryAttemptedLogLevel(LoggingLevel.WARN)
.handled(true) // swallow the exception
.to("jms:queue:io-errors");
onException(ValidationException.class)
.handled(true) // mark Exchange as handled
.transform(exceptionMessage()) // body = exception message text
.to("direct:sendBadRequestResponse");
from("jms:queue:orders")
.to("http://payment-service/pay");
}Key configuration points: handled(true) marks the exception as consumed so Camel does not re-throw it; continued(true) marks it handled AND continues routing the original message; useOriginalMessage() restores the original body before sending to the DLQ. Exponential backoff is configured with backOffMultiplier() + maximumRedeliveryDelay(). For Camel Spring Boot, configure these via application.properties: camel.springboot.default-error-handler.maximum-redeliveries=3.
More Related questions...