You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by nilspreusker <n....@gmail.com> on 2012/09/01 20:49:54 UTC

Synchronous consumer

Hi there,

I'm wondering whether it is possible to write a component with a consumer
that synchronously processes incoming messages. What I would like to achieve
is that, if an exception occurs during the processing of the route, the
exception is propagated back to the caller rather than passed on to camel's
built in default error handler.

So if I have a route like this:
from("myComponent://in).beanRef("beanThatTrowsAnException");

...which is invoked within a transaction, and the referenced bean throws an
exception, I'd like camel to throw the exception back to the caller (i.e.
the consumer) in order to handle it there or roll back the transaction.

Is there a way of doing that or is there even a component that does
something similar so I could have a look at how it is done there?

Cheers!
Nils



--
View this message in context: http://camel.465427.n5.nabble.com/Synchronous-consumer-tp5718500.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Synchronous consumer

Posted by Willem jiang <wi...@gmail.com>.
If you don't want to let the default camel error handler work for you, you need to set error handler to noErrorHandler like this if you are using Spring configuration.

<!-- You can also define the errorHandler inside the camelContext -->  
<camelContext errorHandlerRef="noErrorHandler" xmlns="http://camel.apache.org/schema/spring">  
<errorHandler id="noErrorHandler" type="NoErrorHandler"/>  
</camelContext>


or Java DSL
public void configure() {
errorHandler(noErrorHandler());


        ……
}



--  
Willem Jiang

FuseSource
Web: http://www.fusesource.com (http://www.fusesource.com/)
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: willemjiang





On Monday, September 3, 2012 at 12:55 AM, nilspreusker wrote:

> Claus, thanks for the quick reply! Turns out the consumer was synchronous all
> along, I was simply missing that the exception I was expecting was set to
> the exchange. All I had to do to make it work the way I wanted was to
> retrieve the exception by calling exchange.getException() and re-throw it...
> Thanks again!
>  
> Nils
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Synchronous-consumer-tp5718500p5718515.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: Synchronous consumer

Posted by nilspreusker <n....@gmail.com>.
Claus, thanks for the quick reply! Turns out the consumer was synchronous all
along, I was simply missing that the exception I was expecting was set to
the exchange. All I had to do to make it work the way I wanted was to
retrieve the exception by calling exchange.getException() and re-throw it...
Thanks again!

Nils



--
View this message in context: http://camel.465427.n5.nabble.com/Synchronous-consumer-tp5718500p5718515.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Synchronous consumer

Posted by Claus Ibsen <cl...@gmail.com>.
On Sat, Sep 1, 2012 at 8:49 PM, nilspreusker <n....@gmail.com> wrote:
> Hi there,
>
> I'm wondering whether it is possible to write a component with a consumer
> that synchronously processes incoming messages. What I would like to achieve
> is that, if an exception occurs during the processing of the route, the
> exception is propagated back to the caller rather than passed on to camel's
> built in default error handler.
>
> So if I have a route like this:
> from("myComponent://in).beanRef("beanThatTrowsAnException");
>
> ...which is invoked within a transaction, and the referenced bean throws an
> exception, I'd like camel to throw the exception back to the caller (i.e.
> the consumer) in order to handle it there or roll back the transaction.
>
> Is there a way of doing that or is there even a component that does
> something similar so I could have a look at how it is done there?
>

This is how it works in Camel. The consumer kicks off the routing, when you
use the processor to process the Exchange, the consumer has created.

You can do this synchronously

try {
   getProcessor().process(exchange);
} catch (Exception e) {
   exchange.setException(e);
}

if (exchange.getException() != null) {
   // some exception happened
}


Then in your Camel routes you should of course not use an error
handler that will handle the exception such
as the DeadLetterChannel etc.



> Cheers!
> Nils
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Synchronous-consumer-tp5718500.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen