当在Camel分割过程中出现异常时,如何获取groupedExchanges?

huangapple go评论76阅读模式
英文:

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

问题

我在我的Camel split()路由中有一个聚合策略

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

splitMessage方法已将数据分割为3个请求数据。因此,我会连续3次命中http目标终端点。

使用聚合策略,前两次的http响应已进行了聚合。

第三次,当http调用因异常而失败时。返回给调用者的交换不包含前两个分组的交换。

在这种情况下,如何获得具有(成功、异常)状态的分组交换。

如果问题不清楚,请告诉我。


<details>
<summary>英文:</summary>

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

``` java
from(&quot;direct:split&quot;)
    .split()
        .method(new SplitBean(), &quot;splitMessage&quot;)
        .aggregationStrategy(AggregationStrategies.groupedExchange())
        .stopOnException()
    .to(&quot;direct:destination&quot;)
    .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.

答案1

得分: 1

1..stopOnException() 更改为 .stopOnAggregateException()
2. 创建一个 AggregationStrategy 策略类并从那里处理异常

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;
    }
}
英文:
  1. Change from .stopOnException() to .stopOnAggregateException()

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

    public void configure() throws Exception {

         from(&quot;direct:split&quot;)             
                 .split()
                 .method(new SplitBean(), &quot;splitMessage&quot;)
                 .aggregationStrategy(new ErrorStrategy())
                 .stopOnAggregateException()
                 .to(&quot;direct:destination&quot;)
                 .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;
         }
         }
    

huangapple
  • 本文由 发表于 2020年9月8日 17:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63790607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定