You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Samuel Cox <cr...@gmail.com> on 2012/02/03 17:16:17 UTC

Exception handling: Processor and Spring DSL

Hi,

I'm trying to define a route specific exception handling mechanism.
It's purpose is to apply a custom format to the response body of an
HTTP response.  My route is:

   <camel:route id="my-servlet">
      <camel:from
uri="servlet:///?servletName=diAdapterServlet&amp;matchOnUriPrefix=true&amp;disableStreamCache=true"/>
      <camel:onException>
        <camel:exception>java.lang.Exception</camel:exception>
        <camel:redeliveryPolicy maximumRedeliveries="0"/>
        <camel:handled>
          <camel:constant>true</camel:constant>
        </camel:handled>
        <camel:to uri="exceptionHandlingProcessor"/>
      </camel:onException>
      <camel:to  uri="httpInboundLoggingProcessor"/>
      <camel:to uri="httpToServicesProcessor"/>
      <camel:to uri="httpOutboundLoggingProcessor"/>
    </camel:route>
  </camel:camelContext>


Exception Handler is defined like so:

public class ExceptionHandlingProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        System.out.println("hi from error!");
    }
}

Nothing appears to go through that handler though.  Any ideas on what
I'm doing wrong would be greatly appreciated.

Re: Exception handling: Processor and Spring DSL

Posted by Samuel Cox <cr...@gmail.com>.
On 2012-02-03 4:58 PM, Babak Vahdat wrote:
> Instead of using a ProducerTemplate it would be much easier to do a to("...")
> to fire an exchange into your second route inside the same camel context.

Those were my exact words to my colleague:)  He told me that the 
separate ProducerTemplate was used to do a synchronous type operation.

I'm going to play around with that for a bit and see if I can collapse 
it all into one Camel route, as you suggest.

> And if you want to access the Exception attached to the failed exchange you
> can make use of a Processor or your Bean (Bean-Binding) inside your
> onException clause where using exchange.getException() would deliver you the
> attached exception by the given exchange.

That is what I was expecting.  I see your next response.  I'll check 
that property.

Thanks.


Re: Exception handling: Processor and Spring DSL

Posted by Babak Vahdat <ba...@swissonline.ch>.
I did a typo by my previous post:

you can access the exception attached to the failed exchange using:

exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);

Babak

--
View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5455366.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Babak Vahdat <ba...@swissonline.ch>.
Instead of using a ProducerTemplate it would be much easier to do a to("...")
to fire an exchange into your second route inside the same camel context.

And if you want to access the Exception attached to the failed exchange you
can make use of a Processor or your Bean (Bean-Binding) inside your
onException clause where using exchange.getException() would deliver you the
attached exception by the given exchange.

Babak 

--
View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5455352.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Samuel Cox <cr...@gmail.com>.
My colleague and I were just talking about how I didn't give enough
information...

The httpToServicesProcessor uses an injected ProducerTemplate to fire
off an exchange to another route...

I have since determined that is somehow involved in the problem.  When
I simply threw an exception from the httpToServiceProcessor, the
exception handler was invoked (although I couldn't find the exception,
which I assumed would be in exchange.getException).

On Fri, Feb 3, 2012 at 3:01 PM, Babak Vahdat
<ba...@swissonline.ch> wrote:
> Where does that Seda endpoint come from?! I don't see any to("seda:...") in
> your route!
>
> Babak
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5455034.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Babak Vahdat <ba...@swissonline.ch>.
Where does that Seda endpoint come from?! I don't see any to("seda:...") in
your route!

Babak

--
View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5455034.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Samuel Cox <cr...@gmail.com>.
We're using camel-servlet.  Here is the exception that is ultimately
exposed the HTTP client:

org.apache.camel.ExchangeTimedOutException: The OUT message was not
received within: 1 millis. Exchange[Message: [Body is null]]
	at org.apache.camel.component.seda.SedaProducer.process(SedaProducer.java:117)
	at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)
	at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:299)
	at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:208)
	at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:102)
	at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)

I have it the timeout set to 1ms so that I can test this exception
handling mechanism.  I need to change the HTTP response to something
like this:

<error>
  <message>A timeout occurred</message>
  <stackTrace>...</stackTrace>
</error>

Unfortunately, upgrading isn't an option right now.  Thanks for the help.

On Fri, Feb 3, 2012 at 11:02 AM, Babak Vahdat
<ba...@swissonline.ch> wrote:
> Can you turn the log level to DEBUG and send the output or eventually the
> stacktrace of any exception you probably may see?
>
> BTW the oldest supported version of Apache Camel are the 2.8.x branch
> releases so maybe you want to consider an upgrade as well.
>
> Babak
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5454470.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Babak Vahdat <ba...@swissonline.ch>.
Can you turn the log level to DEBUG and send the output or eventually the
stacktrace of any exception you probably may see?

BTW the oldest supported version of Apache Camel are the 2.8.x branch
releases so maybe you want to consider an upgrade as well.

Babak 

--
View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5454470.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Samuel Cox <cr...@gmail.com>.
A changed all the to-uri pairs to process-ref pairs.  The behaviour
appears to be the same.

On Fri, Feb 3, 2012 at 10:46 AM, Babak Vahdat
<ba...@swissonline.ch> wrote:
> Hi
>
> a xyzProcessor can't be referenced as a URI inside a Route because as the
> name already says it a Processor. So that changing those:
>
> <camel:to  uri="xyzProcessor"/>
>
> to:
>
> <camel:process ref="xyzProcessor"/>
>
> should help.
>
> And as you make use of XML-DSL that processor should have been already
> declared beforehand like:
>
> <bean id="xyzProcessor" class="com.foo.processor.xyzProcessor"/>
>
> Babak
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5454409.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Babak Vahdat <ba...@swissonline.ch>.
Hi

a xyzProcessor can't be referenced as a URI inside a Route because as the
name already says it a Processor. So that changing those:

<camel:to  uri="xyzProcessor"/>

to:

<camel:process ref="xyzProcessor"/>

should help.

And as you make use of XML-DSL that processor should have been already
declared beforehand like:

<bean id="xyzProcessor" class="com.foo.processor.xyzProcessor"/>

Babak

--
View this message in context: http://camel.465427.n5.nabble.com/Exception-handling-Processor-and-Spring-DSL-tp5454311p5454409.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Exception handling: Processor and Spring DSL

Posted by Samuel Cox <cr...@gmail.com>.
I should have specified the version...  We're using Camel 2.6.

On Fri, Feb 3, 2012 at 10:16 AM, Samuel Cox <cr...@gmail.com> wrote:
> Hi,
>
> I'm trying to define a route specific exception handling mechanism.
> It's purpose is to apply a custom format to the response body of an
> HTTP response.  My route is:
>
>   <camel:route id="my-servlet">
>      <camel:from
> uri="servlet:///?servletName=diAdapterServlet&amp;matchOnUriPrefix=true&amp;disableStreamCache=true"/>
>      <camel:onException>
>        <camel:exception>java.lang.Exception</camel:exception>
>        <camel:redeliveryPolicy maximumRedeliveries="0"/>
>        <camel:handled>
>          <camel:constant>true</camel:constant>
>        </camel:handled>
>        <camel:to uri="exceptionHandlingProcessor"/>
>      </camel:onException>
>      <camel:to  uri="httpInboundLoggingProcessor"/>
>      <camel:to uri="httpToServicesProcessor"/>
>      <camel:to uri="httpOutboundLoggingProcessor"/>
>    </camel:route>
>  </camel:camelContext>
>
>
> Exception Handler is defined like so:
>
> public class ExceptionHandlingProcessor implements Processor {
>    public void process(Exchange exchange) throws Exception {
>        System.out.println("hi from error!");
>    }
> }
>
> Nothing appears to go through that handler though.  Any ideas on what
> I'm doing wrong would be greatly appreciated.