niyasc
niyasc

Reputation: 4490

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?

Upvotes: 0

Views: 5573

Answers (2)

Vadim
Vadim

Reputation: 4120

and what about Camel throwException? define exception:

<spring:bean id="exception-unimplemented-operation"
    class="java.lang.Exception">
    <spring:constructor-arg name="message"
        value="Unimplemented Operation." />
</spring:bean>

... and in route ...

<cml:otherwise>
    <cml:throwException ref="exception-unimplemented-operation" />
</cml:otherwise>

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55540

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

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

Upvotes: 1

Related Questions