Apache Camel:GroupedExchangeAggregationStrategy 组 DefaultExchange 而不是消息正文

Posted

技术标签:

【中文标题】Apache Camel:GroupedExchangeAggregationStrategy 组 DefaultExchange 而不是消息正文【英文标题】:Apache Camel: GroupedExchangeAggregationStrategy groups DefaultExchange instead of message body 【发布时间】:2018-04-02 20:43:00 【问题描述】:

继续另一个线程, Apache Camel : File to BeanIO and merge beanIO objects based on id

尝试使用 GroupedExchangeAggregationStrategy 对 EmployeeDetails 进行分组,如下所示

from("seda:aggregate").aggregate(simple("$body.id"), new 
MergeAggregationStrategy()).completionSize(3).log("Details - $header.details").to("seda:formList");

from("seda:formList").aggregate(new 
GroupedExchangeAggregationStrategy()).constant(true).completionTimeout(10)
    .process(EmployeeDetailsBeanProcessor).log("Final list of groupedExchangeAggr $body")
    .log("FILE PROCESSING COMPLETED");

EmployeeDetailsBeanProcessor:

public class EmployeeDetailsBeanProcessor implements Processor
    public void process(Exchange exchange) throws Exception 
        ArrayList<EmployeeDetails> rows = exchange.getIn().getBody(ArrayList.class);
        for (EmployeeDetails record : rows)     
            System.out.println("----- Record:: ----- "+ 
                record.getId() + "    "+ 
                record.getName() + "    " +
                record.getJob() +"    "+
                record.getEmail() + "    "+
                record.getCity()+"    "+
                record.getCode());
        
    

我的理解是当使用 GroupExchangedAggregationStrategy 时,当前的 Exchange 将被添加到 List 中,但是在测试时它添加了 DefaultExchange 而不是当前的 Exchange with EmployeeDetails。

ERROR 4236 --- [eTimeoutChecker] o.a.camel.processor.DefaultErrorHandler  : Failed delivery for (MessageId: ID-admin-PC-49678-1508604904882-0-30 on ExchangeId: ID-admin-PC-49678-1508604904882-0-29). Exhausted after delivery 
attempt: 1 caught: java.lang.ClassCastException: org.apache.camel.impl.DefaultExchange cannot be cast to com.test.EmployeeDetails

您能否提供一些关于对交易所进行分组的示例或说明?提前致谢。

【问题讨论】:

【参考方案1】:

不,你的EmployeeDetailsBeanProcessor 错了。正文中的交换是 List&lt;Exchange&gt; 而不是 List&lt;xxx&gt;

使用GroupedExchangeAggregationStrategy 仅在特殊用例中,您可能使用了错误的策略。如果您想将List&lt;EmployeeDetails&gt; 组合在一起,那么您可以使用FlexibleAggregationStrategy 查看此单元测试的示例:https://github.com/apache/camel/blob/master/core/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java

【讨论】:

以上是关于Apache Camel:GroupedExchangeAggregationStrategy 组 DefaultExchange 而不是消息正文的主要内容,如果未能解决你的问题,请参考以下文章

apache camel 条件路由

Apache Camel 压缩包

如何在apache camel中附加速度文件内容

如何在apache camel DSL或camel Processor内部设置其他身份验证属性?

使用状态码 405 获取 org.apache.camel.component.http.HttpOperationFailedException

ServiceMix 中 Apache-Camel 路由的管理和监控