Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define exception to be thrown through ref in Apache Camel

Tags:

apache-camel

Have to throw an exception in my camel route defined in XML. Found throwException statement available from Camel 2.3 which looks like:

 <throwException ref="forced"></throwException>

However, I don't know how to define forced exception class to be thrown. Since same exception could be thrown couple of times with different exception messages - would be good to know if throwException has some other form of definition so exception class and exception message are defined in-place.

like image 684
Archer Avatar asked Apr 30 '11 04:04

Archer


People also ask

How do you throw an exception on a Camel route?

Using onException to handle known exceptions is a very powerful feature in Camel. You can mark the exception as being handled with the handle DSL, so the caller will not receive the caused exception as a response. The handle is a Predicate that is overloaded to accept three types of parameters: Boolean.

What is CamelContext in Apache Camel?

The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.

Is Apache Camel outdated?

Many open source projects and closed source technologies did not withstand the tests of time and have disappeared from the middleware stacks for good. After a decade, however, Apache Camel is still here and becoming even stronger for the next decade of integration.

What is toD in Apache Camel?

The to is used for sending messages to a static endpoint. In other words to sends message only to the same endpoint. The toD is used for sending message to a dynamic endpoint. The dynamic endpoint is evaluated on-demand by an Expression.


2 Answers

The ref is just a reference to a so you can do

<bean id="forced" class="java.lang.IllegalArgumentException">
   <constructor-arg index="0" value="This is forced"/>
</bean>

<camelContext ...>
  ...
</camelContext>
like image 157
Claus Ibsen Avatar answered Nov 03 '22 18:11

Claus Ibsen


Since version 2.16.0 there is more elegant way to do it, with optional exception message:

<route>
     <throwException exceptionType="java.lang.IllegalArgumentException" message="illegal argument found"/>
</route>
like image 24
Enigo Avatar answered Nov 03 '22 17:11

Enigo