Search code examples
apache-camel

Skip Camel Processor if Previous Processor Failed


I have a camel route that goes through a series of processors which each call some service. If processor 1 fails gracefully, I no longer want to call processors 2-5, but I do want to call consolidateResponse. The same for all the other processors. Is there a best practices way of achieving this without throwing an exception on failures?

<camel:routeContext id="myRouteRouteContext">
    <camel:route id="my-route-route">
    
        <camel:from uri="{{camel.uri.myRoute}}" />
        <camel:process ref="{{bean.processor.processor1}}" />
        <camel:process ref="{{bean.processor.processor21}}" />
        <camel:process ref="{{bean.processor.processor3}}" />
        <camel:process ref="{{bean.processor.processor4}}" />
        <camel:process ref="{{bean.processor.processor5}}" />
        <!--  Stringfy response object into a JSon text response -->
        <camel:process ref="{{bean.processor.consolidateResponse}}" />

        <!-- All catch exception handler -->
        <camel:onException>
        <camel:exception>java.lang.Exception</camel:exception>
        <camel:handled>
            <camel:constant>true</camel:constant>
        </camel:handled>
        <camel:to uri="{{camel.uri.error}}" />
        </camel:onException>

    </camel:route>
</camel:routeContext>

Solution

  • I think try catch is best solution also it is common programming solution also camel also has stop

    https://camel.apache.org/manual/try-catch-finally.html

    from("direct:start")
        .choice()
            .when(body().contains("Hello")).to("mock:hello")
            .when(body().contains("Bye")).to("mock:bye").stop()
            .otherwise().to("mock:other")
        .end()
    .to("mock:result");