prashanth-g
prashanth-g

Reputation: 1233

How to get the groupedExchanges when there is an exception occurred in the camel split?

I have an aggregation Strategy in my camel split() route.

from("direct:split")
    .split()
        .method(new SplitBean(), "splitMessage")
        .aggregationStrategy(AggregationStrategies.groupedExchange())
        .stopOnException()
    .to("direct:destination")
    .end();

The splitMessage method has split the data into 3 request data. So I am hitting the http destination endpoint 3 times.

Using the aggregation Strategy my http response got aggregated for the first 2 times.

Third time when the http call failed with an exception. The exchange returned to the caller does not contain the first two grouped exchanges.

How can I get the grouped exchanges with (success, exception) this case.

Please tell me if the question is not clear.

Upvotes: 0

Views: 345

Answers (1)

Stephen kamanu
Stephen kamanu

Reputation: 31

  1. Change from .stopOnException() to .stopOnAggregateException()

  2. create an AggregationStrategy strategy class and handle the exception from there

    public void configure() throws Exception {

         from("direct:split")             
                 .split()
                 .method(new SplitBean(), "splitMessage")
                 .aggregationStrategy(new ErrorStrategy())
                 .stopOnAggregateException()
                 .to("direct:destination")
                 .end();
    
     }
    
     public class ErrorStrategy  implements CompletionAwareAggregationStrategy {
             @Override
         public void onCompletion(Exchange exchange) {
    
         }
    
         @Override
         public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
             if (newExchange.getException() != null) {
                 return oldExchange;
             }
    
             if (oldExchange == null) {
             ....
             return newExchange;
             }
       .....
         return oldExchange;
         }
         }
    

Upvotes: 1

Related Questions