2015-01-21 78 views
2

原始消息我使用分離器拆分職位在單個消息。單個消息充滿了一些數據。拆分後,我想將所有消息合併到一個。但是當我這樣做時,我只能得到所有職位,而不是邊界XML。我怎樣才能得到在我的路由Splitter AggregateStrategy

我的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") 

的路線後,我的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> 

新的數據被插入,但訂單界丟失。我能做些什麼來獲取所有數據?

我AggregatePositionsStrategy的Splitter是:

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

我知道,我只是人體的從劈裂郵件複製。但我不知道如何獲得原始信息,以獲取所有部分。你有好主意嗎?

回答

0

我寫我自己的處理器。駱駝內置方法很難解決這個問題。

謝謝你的幫助。

0

據我所知,當您使用splitter模式時,原始消息確實「丟失」。

您可以將其保存爲交易所上的一個屬性,稍後再次使用它來替換消息正文和更新的位置列表。

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") 

或者,您可以保存「名字」和「姓氏」以交換屬性並重新構建新的訂單消息。

+0

如果我不使用AggregateStrategy。分割後我得到原始的消息體,但沒有豐富的數據。 http://camel.apache.org/splitter.html#Splitter-WhattheSplitterreturns所以必須有原始的Message體到處 – Burner 2015-01-21 09:09:50

1

另一種選擇: 你總是可以得到的路線的原體的UnitOfWork接口

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

     (do something...) 
    }  
})