Hi All,

What is the best way to handle the situation when there is a need to 
automatically adjust  the exchange as part of the error handling and proceed 
with retry?

For example, there is a route1 that invokes route2 with the 'token' property 
set. When, during the route2 execution  there is  a failure (401 http error) I 
want to reset the token property value and make route1 to do the retry (with 
the updated property).
Here is the sample route that doesn't work (onException never called):

public class ErrorHandlingTest extends CamelTestSupport {

    @Test
    public void runIt() throws Exception {
        template.setDefaultEndpointUri("direct:route2");
        template.sendBodyAndProperty("test", "token", "bad");
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {

                from("direct:route1")
                        
.errorHandler(defaultErrorHandler().maximumRedeliveries(2))
                        .to("direct:route2");

                Predicate got401 = 
exchangeProperty("CamelExceptionCaught").method("getStatusCode").isEqualTo(401);

                from("direct:route2")
                        .errorHandler(noErrorHandler())
                        
.onException(HttpOperationFailedException.class).onWhen(got401).process(e -> {
                            System.out.println("resetting token");
                            e.setProperty("token", "good");}
                            ).end()
                        .choice()
                            .when()
                                .exchange(e -> 
e.getProperty("token").equals("bad"))
                                .throwException(new 
HttpOperationFailedException("http://test.com";, 401, "not authenticated", 
"/doIt", new HashMap<>(), "bad token"))
                            .otherwise()
                                .log("token is good");
            }
        };
    }
}

Cheers,
Dmitry

Reply via email to