首页 文章

如何在拆分器AggregateStrategy上获取原始消息

提问于
浏览
2

在我的路线中,我使用Splitter在单个消息中拆分位置 . 单个Message包含一些数据 . 分割后我想将所有消息合并为一个 . 但是当我这样做时,我只获得所有位置,而不是边界XML .

我的XML:

<order>
    <firstname>Max</firstname>
    <lastname>Mustermann</lastname>
    <positions>
        <position>
            <articlename>Article 1</articlename>
            <amount>1</amount>
        </position>
        <position>
            <articlename>Article 2</articlename>
            <amount>2</amount>
        </position>
    </positions>
</order>

我的路线:

from("activemq:orders")
    .split(body().tokenizeXML("POSITION",""), new AggregatePositionsStrategy())
        .enrich("direct:getArticleNumber", new addArticleNrToPositionStrategy())
    .end()
    .to("file://c:/temp")

在Route之后我的XML是:

<position>
            <articlenumber>654321</articlenumber>
            <articlename>Article 1</articlename>
            <amount>1</amount>
        </position>
        <position>
            <articlenumber>123456</articlenumber>
            <articlename>Article 2</articlename>
            <amount>2</amount>
        </position>

插入新数据,但订单边框丢失 . 我该怎么做才能获得所有数据?

Splitter的My AggregatePositionsStrategy是:

public class AggregatePositionsStrategy implements AggregationStrategy {
    public Exchange aggregate(Exchange exchange, Exchange response) {

        if (exchange == null) {
            return response;
        }

        if (exchange.getIn().getHeader("Artikelnummer") != null && exchange.getIn().getHeader("Artikelnummer").equals("Not Found")) {
            exchange.getIn().setHeader("Artikelnummer", "Not Found");
        }

        if (response.getIn().getHeader("Artikelnummer") != null && response.getIn().getHeader("Artikelnummer").equals("Not Found")) {
            exchange.getIn().setHeader("Artikelnummer", "Not Found");
        }

        String orders = exchange.getIn().getBody(String.class);
        String newLine = response.getIn().getBody(String.class);

        orders = orders + "\n" + newLine;
        exchange.getIn().setBody(orders);

        return exchange;
    }
}

我知道,我只从分裂的消息中复制Bodys . 但我不知道如何获得原始消息,以获得所有部分 . 你有好主意吗?

3 回答

  • 1

    据我所知,当您使用 splitter 模式时,原始消息确实是"lost" .

    您可以将其另存为交换机上的属性,稍后再次使用它来替换消息正文,并使用更新的位置列表 .

    from("activemq:orders")
        .setProperty("OriginalMessage", body())
        .split(body().tokenizeXML("POSITION",""), new AggregatePositionsStrategy())
            .enrich("direct:getArticleNumber", new addArticleNrToPositionStrategy())
        .end()
        // set the positions on the "OriginalMessage" and set it to the body here
        .to("file://c:/temp")
    

    或者,您可以保存“FirstName”和“LastName”来交换属性并重新构建新的Order消息 .

  • 0

    另一种选择:您可以随时使用UnitOfWork接口获取路径的原始主体

    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            Message originalMessage = (Message)
                    exchange.getUnitOfWork().getOriginalInMessage();
    
            (do something...)
        }       
    })
    
  • 0

    我写自己的处理器 . 使用Camel内置方法很难解决这个问题 .

    谢谢您的帮助 .

相关问题