骆驼拆分并行处理聚合消息

时间:2018-07-26 11:49:16

标签: rest concurrency split parallel-processing apache-camel

可能看起来像个小问题,但不幸的是无法解决问题

这是代码

from("direct:START")
.process( (ex) -> {
   List<Integer> pages = IntStream.range(1,5).boxed().collect(Collectors.toList());
   ex.getOut().setBody( pages );
 })
.split(body())
.parallelProcessing()
.to("http://someurl?page=${body}");
 --> Get the collective body here

如何获得这项工作!

1 个答案:

答案 0 :(得分:2)

您可以将AggregatorcompletionSize表达式一起使用来聚合拆分的消息。

.split(body()).parallelProcessing().to("log:splitted_body_is_here")
.aggregate(constant(true), AggregationStrategies.groupedBody())
    .completionSize(exchangeProperty(Exchange.SPLIT_SIZE))
.to("log:aggregated_body_is_here")

如果您使用的是较早版本的骆驼(2.20.x), AggregationStrategies.groupedBody()将不可用。您可以使用其他方法。我使用了一种简单的自定义方法来执行聚合。

代码更改为

.split(body()).parallelProcessing().to("log:splitted_body_is_here")
.aggregate(constant(true), (in,out) ->{
                if( in == null ){
                    return out;
                }
                else{
                    String body = in.getIn().getBody(String.class);
                    body = body + "," + out.getIn().getBody( String.class );
                    in.getOut().setBody( body );
                    return in;
                }
            })
    .completionSize(exchangeProperty(Exchange.SPLIT_SIZE))
.to("log:aggregated_body_is_here")

上面的代码仅假设主体为String / JSON,并以逗号将其附加。

看起来,您想在to中使用动态URL调用端点。不支持,请改用toD