camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <>
Subject Re: Error Handler
Date Fri, 04 Nov 2011 09:48:18 GMT
On Thu, Nov 3, 2011 at 3:21 PM, Calvert, Zach (Zach)** CTR **
<> wrote:
> I have a CXF bean sending out a camel message on a ProducerTemplate and I want any exceptions
thrown on the route to immediately come back as the response.  It is an inOut message:
> producer.sendBodyAndHeaders(URI, ExchangePattern.InOut, body, headers);
> I have not figured out the magic method to getting the response to automatically fast
fail if an exception is thrown by any method along the way.  For example, the URI goes to
an ActiveMQ queue, and if I have it immediately run
> If (true) throw new RuntimeException();
> The exception eventually results the jetty response of HTTP 500 server error on the jetty
CXF API.  I have two bundles, one bundle is the jetty CXF followed by sending this to an
active MQ queue and the second bundle consumes the message with another route.  If on the
second route, the operation throws an Exception and I create an error handler, to the tune
>        <camel:onException>
>            <camel:exception>java.lang.Throwable</camel:exception>
>            <camel:bean ref="fastFailErrorHandler" method="handleError" />
>        </camel:onException>
>    public Object handleError(Exchange exchange) {
>        Throwable caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
>        exchange.getIn().setFault(true);
>        exchange.getOut().setFault(true);
>        logger.debug("Error handler triggered");
>        return caused;
>    }
> The original problem will be a timeout exception, waiting the default 20 seconds.  If
I modify it to be
>        <camel:onException>
>            <camel:exception>java.lang.Throwable</camel:exception>
>            <camel:handled>
>                <camel:constant>true</camel:constant>
>            </camel:handled>
>            <camel:bean ref="fastFailErrorHandler" method="handleError" />
>        </camel:onException>
> Then the body continues working its way through subsequent queues as if nothing went
> How do I tell Camel, "hey Camel, please throw this thing to the original caller as the
response body".

You can handle the exception from onException and construct a response
body to be returned.
Or do a doTry .. doCatch style etc.

> Thanks,
> Zach Calvert

Claus Ibsen
Twitter: davsclaus, fusenews
Author of Camel in Action:

View raw message