Question

Apache Camel provides 2 ways for error handling at context level: errorHandler and onException.

I am looking for a solution that provides best of both the worlds in one configuration. What I want is that I should configure one error handler (may be a custom one) and within that custom error handler, I should be able to control the behavior on per exception basis For Example:

if I get IOException, I would make 3 redelivery attempts at an interval of 3 seconds

if I get FileNotFoundException, I would make 5 redelivery attempts at an interval of 10 seconds

errorHandler would enable me to use one of the provided Error Handlers and would handle all the exception in a similar ways. For example using this approach I can’t have different redelivery attempts for different exceptions.

So, here where onException comes into picture, which is equivalent of try-catch at camel context level. I can use this to define per Exception behavior.

I tried to use a custom bean with Dead Letter Channel in an attempt to control the redelivery attempts from the bean. However, Camel attempts redelivery before it passes the control to the custom bean.

Any thoughts about implementing the solution that I want.


Edited after first set of comments seeking more clarity: I am aware of this feature of onException. There would be different type of exceptions that can be thrown by the routes. Also new exceptions added added as new routes/processors/transformers are developed once the system goes live. If I use onException, then I (or developers) have to specify each one of them. (I don't want to use the onException(Exception.class) approach.) So, this would require adding more onException clauses to the context new exception get added to the system. Also, some exception might require special handling. I don't want to rely on developers for error handling. So,I am looking at a generic solution based on a custom error handler. This will help me to keep the config simple, as I have to specify only one error handler on the context. This error handler would be manageable from a JMX Console as well as provide me the flexibility to handle any exception in a customized way.

Was it helpful?

Solution

You can use onException(Exception.class).onWhen(predicate) and then use a java method call as the predicate that returns true or false. Then you can use java code to trigger if the onException should react or not.

See more details at

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top