0

在我们的 Java 11 项目中从 Apache Camel 2.x升级到3.7.2后,我们在路由配置类中遇到了一些粗陋的东西,扩展了RouteBuilder (文档)类。在 Camel 2 中,我使用了 SimpleExpression ${property[" + Exchange.GROUPED_EXCHANGE + "]},现在在 Camel 3.x 中已将其重命名为exchangeProperty (Migration Guide)。到目前为止,一切都很好。

@Override
public void configure() throws Exception {

    final SimpleExpression documentAppendix =
        new SimpleExpression("${body.appendix}");

    final SimpleExpression groupedExchanges =
        new SimpleExpression("${exchangeProperty[" + Exchange.GROUPED_EXCHANGE + "]}");

    from("direct://input")
        .choice()
            .when(documentAppendix)
                .split(documentAppendix, new GroupedExchangeAggregationStrategy())
                .to("direct://input.splitted")
                .end()
                .setBody(groupedExchanges)
                .endChoice()
            .otherwise()
                .setBody(constant(Lists.newArrayList()))
        .end();
    
    // ... omitted
    
}

运行测试后,一切都失败了,因为正文没有包含预期数量的附录。起初,我们认为可能存在问题,exchangeProperty但在使用调试器一段时间后,我们发现了以下属性“丢失”的线索:

         GroupedExchangeAggregationStrategy
                         |
                         v
           AbstractListAggregationStrategy
(This class sets the "CamelGroupedExchange" property!)
                         |
                         v
                 AggregateProcessor
                 doAggregation(...)
       ExchangeHelper.preparteAggregation(...)

聚合后的预期返回应该是一个对象列表,可以通过CamelGroupedExchangeor访问,${exchangeProperty[" + Exchange.GROUPED_EXCHANGE + "]}但它会被newExchangein覆盖ExchangeHelper.preparteAggregation

由于我们没有找到更多证据,有人可以在将 Camel 升级到 3.7.2 后解释这种奇怪的行为吗?也许 ExchangeHelper 和可用的 ExchangePattern/MEP 模式(CAMEL-13286)发生了重大变化,但我们无法解决问题。

谢谢你们的帮助!

4

1 回答 1

1

我们发现AbstractListAggregationStrategyCamel 3.7.2 中的现在在完成时将属性设置为In主体:

public abstract class AbstractListAggregationStrategy<V> implements AggregationStrategy {
    public AbstractListAggregationStrategy() {
    }

    public abstract V getValue(Exchange exchange);

    public boolean isStoreAsBodyOnCompletion() {
        return true;
    }

    public void onCompletion(Exchange exchange) {
        if (exchange != null && this.isStoreAsBodyOnCompletion()) {
            List<V> list = (List)exchange.removeProperty("CamelGroupedExchange");
            if (list != null) {
                exchange.getIn().setBody(list);
            }
        }

    }

    // omitted

}

有了这个改变,我们.setBody(groupedExchanges)就变得多余了,我们可以通过 访问交易所列表getIn()

于 2021-09-21T11:13:02.257 回答