Apache Camel - Aggregation split elements to a list

时间:2018-04-18 18:07:18

标签: java apache-camel

Problem :

I first have a list of cages, where I retrieve individually with a split the positions of those ones.

The following consumer get cages already one by one and the processor extract the position.

from("direct:cages-to-positions")
                    .process(new CageToPositionProcessor())
                    .aggregate(body(), new PositionsAggregation())
                    .completionTimeout(1000)
                    .to("mock:test");

After that, I wan't to get them back into a list, so I use the following aggregation.

public class PositionsAggregation implements AggregationStrategy {
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        if (oldExchange == null) {
            List<Map> positions = new ArrayList<Map>();
            positions.add((Map) newExchange.getIn().getBody());
            newExchange.getOut().setBody(positions);
            return newExchange;
        }

        Map newPosition = (Map) newExchange.getIn().getBody();
        List<Map> positions = (List<Map>) oldExchange.getIn().getBody();
        positions.add(newPosition);
        oldExchange.getIn().setBody(positions);
        return oldExchange;
    }
}

When I am debugging, I can see that I got the right ending oldExchange, but anyway the result in the mock is not the list at all :

Printing the last oldExchange body :

[{latitude=49.305, longitude=1.2157357}, {latitude=49.305142, longitude=1.2154067}]

Mock result :

11191 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.processor.SendProcessor  - >>>> mock://test Exchange[ID-1524074230289-0-6]
11191 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.component.mock.MockEndpoint  - mock://test >>>> 0 : Exchange[ID-1524074230289-0-6] with body: {latitude=49.305, longitude=1.2157357} and headers:{breadcrumbId=ID-1524074230289-0-6}
11192 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap  - Completion timeout triggered for correlation key: {latitude=49.305142, longitude=1.2154067}
11192 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.processor.aggregate.AggregateProcessor  - Aggregation complete for correlation key **{latitude=49.305142, longitude=1.2154067}** sending aggregated exchange: Exchange[ID-1524074230289-0-9]
11192 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.processor.aggregate.AggregateProcessor  - Processing aggregated exchange: Exchange[ID-1524074230289-0-9]
11192 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.processor.SendProcessor  - >>>> mock://test Exchange[ID-1524074230289-0-9]
11192 [Camel (camel-1) thread #1 - AggregateTimeoutChecker] DEBUG org.apache.camel.component.mock.MockEndpoint  - mock://test >>>> 1 : Exchange[ID-1524074230289-0-9] with body: {latitude=49.305142, longitude=1.2154067} and headers:{breadcrumbId=ID-1524074230289-0-9}

1 个答案:

答案 0 :(得分:2)

您的示例适用于相同的传入正文。 您应该将关联表达式从body()替换为constant(true),因为您没有在拆分器中使用内置聚合器。

所以你

1)要么重构使用拆分器的内置聚合器,例如

.split(body(), new MyAggregationStrategy())

并且在这种情况下不需要完成条件(.completionTimeout())。

这将聚合传入列表中的所有分割消息。这个answer中的一个例子。

2)如果您想在1000个时间段内收集所有来电交换,您必须进行以下更改

 .aggregate(constant(true), new PositionsAggregation())