Apache Camel - 将拆分结果发送到 CXF 端点

Apache Camel - send split results to CXF endpoint

我有一个路由应该处理来自 CXF 端点的请求并且 return 结果为 JSON:

public class MyRoute extends RouteBuilder
{

// ... Autowired:
// msgRequestProcessor - converts json {string,string} to String of "\n" delimited json string: {string}\n{string}
// RangeProcessor, SingleProcessor - create mongodb Criteria object from json string
// msgTypeMapper - adds corresponding header "msg.type"

@Override
public void configure()
{
    from("direct:list")
            .process(msgRequestProcessor)
            .split(body())
                .bean(msgTypeMapper.class)
                .choice()
                    .when(header("msg.type").isEqualTo("single"))
                        .log("Go to direct:single")
                        .to("direct:single")
                    .otherwise()
                        .log("Go to direct:range")
                        .to("direct:daterange")
            .end()
            .to("direct:aggregate");

    from("direct:range")
            .process(new RangeProcessor());

    from("direct:single")
            .process(new SingleProcessor());

    from("direct:aggregate")
            .aggregate(new MyAgg()).header("msg.collection").completionSize(2)
            .log("RETVAL: ${body}")
            .marshal().json(JsonLibrary.Gson).end();
}

public static final class MyAgg implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
    {
        if (oldExchange == null) {
            return newExchange;
        }

        Criteria oldCriteria = oldExchange.getIn().getBody(Criteria.class);
        Criteria newCriteria = newExchange.getIn().getBody(Criteria.class);

        Criteria criteria = new Criteria();
        criteria.andOperator(oldCriteria, newCriteria);
        oldExchange.getIn().setBody(criteria.getCriteriaObject().toString());

        return oldExchange;
    }
}

}

一切正常,我在日志中看到正确的聚合结果和聚合完成

但 CXF 端点总是 returnmsgRequestProcessor 的输出(拆分前):

{"string"}
{"string"}

虽然我希望看到 Criteria 对象转换为字符串(我可以在日志中看到)。

如有任何帮助,我们将不胜感激!谢谢

首先请注意,您的缩进具有误导性,end() 实际上是 choice() 的结尾,而不是 split() 的结尾;我对此感到困惑(@Ralf 可能是这样。)

现在,聚合起作用了,但它的结果被丢弃了,因为拆分的结果确实是输入消息。

对于拆分器的 request/reply 用法 (in-out),您确实必须声明聚合策略以及 split() as explained here(相同的误导性缩进)。

在你说的官方文档中,情况是相反的(in-only):splitter的结果被丢弃,聚合的结果是路由到下游。