Search code examples
javaexceptionapache-cameljbossfuse

Throwing exception with custom error message in camel


I'm trying to throw a custom exception with custom error message in camel. I can do this by setting values to a already defined bean as follows:

<CamelContext>
  <route>
    <from uri="timer:myTimer"/>
    <bean ref="myException" method="setErrorCode(errorCode)"/>
    <bean ref="myException" method="setErrorMessage(errorMessage)"/>
    <throwException ref="myException"/>
  </route>
</CamelContext>

This approach is working fine. But the problem with this approach is, there will be only one instance of MyException and it will cause a kind of race condition when there are multiple concurrent consumers.

Because of this problem, I want to create a new excpetion instance each time. Unfortunately, I don't see any way to create an exception instance inside camel route, set values to it and then throw.

I have read that, beginning from camel 2.17 there is one additional attribute message to set error message. For me, there is constraints that forces me to stick with camel 2.15.

So, what would be the proper way to create a custom exception in camel with error code and error message?


Solution

  • Just call a bean method that creates and throws a new exception

    public void blowUp(...) {
       throw new MyException(...);
    }