简体   繁体   English

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

[英]Apache Camel - send split results to CXF endpoint

I have a route that should process request from CXF endpoint and return results as JSON: 我有一条路由,应处理来自CXF端点的请求并以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;
    }
}

} }

Everything works fine, I see correct aggregation results and aggregation completion in the log 一切正常,我在日志中看到正确的聚合结果和聚合完成

but CXF endpoint always returns output of msgRequestProcessor (before split): 但CXF端点始终返回msgRequestProcessor的输出 (在拆分之前):

{"string"}
{"string"}

while I expect to see Criteria object converted to string (that I can see in the logs). 而我希望看到Criteria对象转换为字符串(可以在日志中看到)。

Any help would be much appreciated! 任何帮助将非常感激! Thanks. 谢谢。

Note first that your indentation is misleading, the end() is really the end of the choice() , and not of the split() ; 首先请注意,您的缩进具有误导性, end()实际上是choice()的结尾,而不是split()的结尾; I was puzzled a while by this (as @Ralf was maybe.) 我对此感到困惑(因为@Ralf可能是。)

Now, the aggregation works, but its result is discarded because indeed the result of the split is the input message. 现在,聚合起作用了,但是它的结果被丢弃了,因为实际上拆分的结果就是输入消息。

For a request/reply usage of the splitter ( in-out ), you really have to declare the aggregation strategy along with the split() as explained here (same misleading indentation). 对于拆分器( in-out )的请求/答复用法,您确实必须声明聚合策略以及split() ,如此处所述 (相同的误导性缩进)。

In the official documentation you mention, the situation is the other way around ( in-only ): the result of the splitter is discarded, and the result of the aggregation is routed downstream. 在您提到的官方文档中,情况是相反的( ):拆分器的结果被丢弃,聚合的结果被路由到下游。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM