Camel聚合策略xpath示例

时间:2015-03-23 19:36:59

标签: java xml xpath apache-camel activemq

我需要将xml发送到队列中,并使用xpath通过其中一个字段进行聚合。 这是我的RouteBuilder类实现的代码:

public class SimpleRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("activemq:queue:test.input").aggregate(new MyAggregationStrategy()).
                xpath("login/(login)='manager", String.class).completionPredicate(header("aggregated").isEqualTo(5))
                .to("activemq:queue:test.output").end()
        ;
    }
}

class MyAggregationStrategy implements AggregationStrategy {
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        Message newIn = newExchange.getIn();
        String oldBody = oldExchange.getIn().getBody(String.class);
        String newBody = newIn.getBody(String.class);
        newIn.setBody(oldBody + newBody);
        return newExchange;
    }
}

正在发送的xml如下所示:

<person>
    <login>login</login>
    <password>pass</password>
</person>

当我将此jar复制到activemq lib文件夹并启动activemq时,会出现此异常:

ERROR: java.lang.IllegalArgumentException: Invalid broker URI, no scheme specified: start

这可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您的代码提取不会导致报告的异常。我不确定,你的用例是什么。

无论如何,以下路由匹配输入消息的login内容。当收到5条具有相同登录信息的消息时,聚合将被发送到输出目标。请注意oldExchange方法中aggregate的空检查:

public class SimpleRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("activemq:queue:test.input")
                .aggregate(new MyAggregationStrategy()).xpath("/person/login", String.class)
                .completionSize(5)
                .to("activemq:queue:test.output");
    }
}

public class MyAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
        Message newIn = newExchange.getIn();
        String newBody = newIn.getBody(String.class);
        if (oldExchange == null) {
            newIn.setBody(newBody);
        } else {
            String oldBody = oldExchange.getIn().getBody(String.class);
            newIn.setBody(oldBody + newBody);
        }
        return newExchange;
    }
}