Camel - Handle exception with split operator

I have simplified my use case with an Order/items example.

An order has some items. Every item is controlled => An AvailabilityException can be thrown from "controlItem". After the exception is thrown, item is marked as Ok and next item is handled.

       onException(AvailabilityException.class)
           .onExceptionOccurred(ItemProcessor)
           .handled(true)
       .bean(service, "markAsOk");


       from(startQueue)
           .split(simple("${body.items}"))
                .to(direct:controlItem")                                            
           .end()
       .to(successQueue);


       from("direct:controlItem")
           .bean(service, "controlItem");

Now I have another case :

When an unexpected exception (NullPointerException, ...) is thrown I would like to stop the process. We don't handle next items and order is redirected to an error queue. How can I do that ?

1 answer

  • answered 2018-07-20 08:18 pvpkiran

    You can use doTry and doCatch

    1. from(startQueue)
        .doTry()
           .split(simple("${body.items}"))
                  .to(direct:controlItem")                                            
             .end()
           .to(successQueue);
        .doCatch(AvailabilityException.class)
                ....
                ....
                .handled(true)
         .doCatch(RunTimeException.class)
                ....
                ....
                .handled(false)
                .stop() // stop the execution      
         .doFinally()  //optional
      
    2. Instead of stop() you can write a processor there and use exchange.getContext().stop();

    3. You can as well add another onException for RunTimeExceptions and stop the route.

    4. Another possible way is to use the property ROUTE_STOP . You can set this property to true in a processor on exception. exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);