【问题标题】:Exchange.GROUPED_EXCHANGE not available after aggregation in Apache Camel 3.7.2Exchange.GROUPED_EXCHANGE 在 Apache Camel 3.7.2 中聚合后不可用
【发布时间】:2021-09-20 14:54:08
【问题描述】:

在我们的 Java 11 项目中从 Apache Camel 2.x 升级到 3.7.2 后,我们在路由配置类中遇到了一些粗鲁的问题,扩展了 RouteBuilder @ 987654321@班级。在 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(...)

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

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

感谢你们的帮助!

【问题讨论】:

    标签: java maven apache-camel camel-cxf


    【解决方案1】:

    我们发现 Camel 3.7.2 中的 AbstractListAggregationStrategy 现在在完成时将属性设置为 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() 访问交易所列表。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2015-11-22
      相关资源
      最近更新 更多