You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring 2.6项目迁移Apache Camel至3.14:无法解析聚合策略接口问题

问题解决:Camel 3.14 替换已移除的 CompletionAwareAggregationStrategy/TimeoutAwareAggregationStrategy

在Camel 3.x版本中,CompletionAwareAggregationStrategy 和 TimeoutAwareAggregationStrategy 确实已被移除,它们的功能被整合到核心 AggregationStrategy 接口中,通过默认方法提供支持。以下是具体的替换方案:

1. 替换 TimeoutAwareAggregationStrategy

原有超时处理逻辑可以通过两种方式实现:

  • 方式一:直接重写AggregationStrategy的默认timeout方法
    AggregationStrategy 在Camel 3.x中新增了默认的timeout方法,直接在自定义聚合策略类中重写即可:
    import org.apache.camel.AggregationStrategy;
    import org.apache.camel.Exchange;
    
    public class CustomAggregationStrategy implements AggregationStrategy {
        @Override
        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
            // 保留原有聚合逻辑
            return oldExchange != null ? oldExchange : newExchange;
        }
    
        @Override
        public Exchange timeout(Exchange oldExchange, int index, int total, long timeout) {
            // 迁移原TimeoutAwareAggregationStrategy中的超时处理逻辑
            return oldExchange;
        }
    }
    
  • 方式二:在路由中独立配置超时回调
    若不想在聚合策略类中耦合超时逻辑,可在aggregate组件中直接指定onTimeout处理器:
    from("direct:start")
        .aggregate(constant(true), new CustomAggregationStrategy())
        .completionSize(5)
        .timeout(1000)
        .onTimeout().process(exchange -> {
            // 独立实现超时处理逻辑
        })
        .end()
        .to("direct:end");
    

2. 替换 CompletionAwareAggregationStrategy

原completed方法同样被整合为AggregationStrategy的默认方法,直接重写即可:

import org.apache.camel.AggregationStrategy;
import org.apache.camel.Exchange;

public class CustomAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        // 保留原有聚合逻辑
        return oldExchange != null ? oldExchange : newExchange;
    }

    @Override
    public void completed(Exchange exchange) {
        // 迁移原CompletionAwareAggregationStrategy中的完成后处理逻辑
    }
}

3. 依赖确认

确保Maven中只引入核心Camel依赖即可,无需额外导入camel-core-model等子包:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>3.14.0</version>
</dependency>

注:Camel 3.14文档中仍提及旧接口属于文档更新滞后,实际代码以核心AggregationStrategy的默认方法实现为准。

内容的提问来源于stack exchange,提问作者danard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:55:08