【问题标题】:Camel aggregation strategy xpath exampleCamel 聚合策略 xpath 示例
【发布时间】:2015-05-26 22:51:41
【问题描述】:

我需要将 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

这可能是什么问题?

【问题讨论】:

    标签: java xml xpath apache-camel activemq


    【解决方案1】:

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

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

    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;
        }
    }
    

    【讨论】:

    • 感谢您的回答。没有更多的例外,但它不会聚合具有指定登录名的消息,test.output 中有所有消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    相关资源
    最近更新 更多