英文:
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("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.
答案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;
}
}
英文:
-
Change from .stopOnException() to .stopOnAggregateException()
-
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; } }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论