Preface: I'm fairly new to Camel, and after digesting Camel in action as best as possible, I'm adapting it to a project I'm on. In this project, we have some rather complex error handling, and I want to make sure I can replicate this as we Camel-ize our code.
On our project (as most) there are a set of Exceptions we want to retry and a set that we don't - but more specifically, there are a set that we want to retry more than others (not all recoverable errors can be treated the same). In this case, I was attempting to define an onException
block to change the redelivery policy. However, it seems that the Exchange maintains the count (Exchange.REDELIVERY_COUNTER
) and that this count is not dependent on which exception is thrown. Is there a way to make this count be specific for a given exception?
For example - I have two exceptions FooException
and BarException
. In my route (or really in the whole context), I want to retry FooExceptions 10 times, but BarException
s should only retry 2 times. So the context will contain:
<onException>
<exception>my.exception.FooException</exception>
<redeliveryPolicy maximumRedeliveries="10" redeliveryDelay="2000"
</onException>
<onException>
<exception>my.exception.BarException</exception>
<redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="5000"
</onException>
Now, the concern - if my application throws a FooException
and retries 4 times (each time throwing a FooException
) and then on the 5th attempt, it throws a BarException
, it seems that the way this works is the Exchange will have a REDELIVERY_COUNTER
of 5, and when I reset the policy to only try twice, it (logically) concludes that the route should not be retried and throws the exception back out. However, in my application BarExceptions
should be retried twice, regardless of how many FooExceptions
get thrown. And likewise, if it alternates throwing Foo and Bar exceptions, I would like it to only increment the counter for the given exception.
The very end of Camel in Action promotes using a retryWhile
- is this the only way to grab the kind of control I'm looking for? Do I need to create a stateful bean that is aware of the count per exception? Or am I overlooking something simple? I want to make sure that as I approach this refactor I don't start us off on an ugly path.
Using Camel 2.10.1