How catch exception from endpoint which was created in code

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How catch exception from endpoint which was created in code

dariusz.skrudlik
This post was updated on .
I found a lot of post about that, but any working for me.

I have tested options
- throwExceptionOnConnectFailed
- consumer.bridgeErrorHandler
- pollStrategy


I have spring bean where define route:

@Service
public class MyRouteRunner {

    @Autowired
    CamelContext camelContext;

    public void startRoute( /* some arguments */) {

        route = "ftp:username@ftp.com.pl?pollStrategy=#myPollStrategy&throwExceptionOnConnectFailed=true&consumer.bridgeErrorHandler=true";

        Exchange exchange = null;
        ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();

        Endpoint endpoint = camelContext.getEndpoint(route);
        consumerTemplate.start();
        exchange = consumerTemplate.receive(endpoint, 2000);

        // I NEED EXCEPTION HERE because must perform extra action depends for argument

}

I have also own PoolStrategy bean

@Service
public class MyPollStrategy implements PollingConsumerPollStrategy {

 
    @Override
    public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {

        //HERE I GET EXCEPTION but any code don't propagate exception to main bean
//1
// throw e; // this should work

//2
//        Exchange exchange = endpoint.createExchange();
//        exchange.setException(e);
//       ((FtpConsumer)consumer).getProcessor().process(exchange);

        return false;
    }
}

Generally when all is ok and file on server exists I receive it, but when exception occurs then I cann't perform any  action.
How can I handle exception where I need it ?
Reply | Threaded
Open this post in threaded view
|

Re: How catch exception with endpoint was created dynamicly

Łukasz Dywicki
Hey Dariusz,
According to javadoc on PollingConsumerPollStrategy.rollback() throwing exception from this method have side effect and in general is used to hold scheduler.

You probably did that already, but having debug or trace logging helps a little bit. Also going with debugger may be helpful for you.

In General you may also try to add new route definition to Camel Context. This rute will be fully functional and let you use normal error handling you have. Routes may be stopped and removed dynamically. So maybe you could try doing that?

Some working code examples you may find in SwitchYard project [1].

[1] https://github.com/jboss-switchyard/core/blob/master/bus/camel/src/main/java/org/switchyard/bus/camel/CamelExchangeBus.java#L97

Cheers,
Łukasz Dywicki
--
[hidden email]
Twitter: ldywicki
Blog: http://dywicki.pl
Code-House - http://code-house.org

Wiadomość napisana przez dariusz.skrudlik <[hidden email]> w dniu 9 sie 2013, o godz. 16:13:

> I found a lot of post about that, but any working for me.
>
> I have tested options
> - throwExceptionOnConnectFailed
> - consumer.bridgeErrorHandler
> - pollStrategy
>
>
> I have spring bean where define route:
>
> @Service
> public class MyRouteRunner {
>
>    @Autowired
>    CamelContext camelContext;
>
>    public void startRoute( /* some arguments */) {
>
>        route =
> "ftp:[hidden email]?pollStrategy=#myPollStrategy&throwExceptionOnConnectFailed=true&consumer.bridgeErrorHandler=true";
>
>        Exchange exchange = null;
>        ConsumerTemplate consumerTemplate =
> camelContext.createConsumerTemplate();
>
>        Endpoint endpoint = camelContext.getEndpoint(route);
>        consumerTemplate.start();
>        exchange = consumerTemplate.receive(endpoint, 2000);
>
>        // I NEED EXCEPTION HERE because must perform extra action depends
> for argument
>
> }
>
> I have also own PoolStrategy bean
>
> public class FtpConsumerPollStrategy implements PollingConsumerPollStrategy
> {
>
>
>    @Override
>    public boolean rollback(Consumer consumer, Endpoint endpoint, int
> retryCounter, Exception e) throws Exception {
>
>        //HERE I GET EXCEPTION but any code don't propagate exception to
> main bean
> //1
> // throw e; // this should work
>
> //2
> //        Exchange exchange = endpoint.createExchange();
> //        exchange.setException(e);
> //       ((FtpConsumer)consumer).getProcessor().process(exchange);
>
>        return false;
>    }
> }
>
> Generally when all is ok and file on server exists I receive it, but when
> exception occurs then I cann't perform any  action.
> How can I handle exception where I need it ?
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-catch-exception-with-endpoint-was-created-dynamicly-tp5737037.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: How catch exception from endpoint which was created in code

ReDiya
In reply to this post by dariusz.skrudlik
dariusz.skrudlik,

Did you find a way to return the Exception back to the bean ? Please share the same.

regards
ReDiya