Apache Camel: GroupedExchangeAggregationStrategy groups DefaultExchange instead of message body

守給你的承諾、 提交于 2019-12-11 14:25:42

问题


In continuation to the other thread, Apache Camel : File to BeanIO and merge beanIO objects based on id

Trying to group the EmployeeDetails using GroupedExchangeAggregationStrategy as below

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());
        }
    }
}

My understanding is when GroupExchangedAggregationStrategy is used, the current Exchange will be added to the List, but when tested it adds DefaultExchange instead of current 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

Can you give some examples or directions on grouping the exchanges? Thanks in advance.


回答1:


No you are wrong in your EmployeeDetailsBeanProcessor. The exchange in body is a List and not a List.

Using GroupedExchangeAggregationStrategy is only in special use-cases, and its likely you are using the wrong strategy. If you want to group together as a List<EmployeeDetails> then you can use the FlexibleAggregationStrategy see this unit test for examples: https://github.com/apache/camel/blob/master/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java



来源:https://stackoverflow.com/questions/46870073/apache-camel-groupedexchangeaggregationstrategy-groups-defaultexchange-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!