如何在拆分器 AggregateStrategy 上获取原始消息
How can I get the original Message on a splitter AggregateStrategy
在我的路线中,我使用拆分器将位置拆分为单个消息。单个消息丰富了一些数据。拆分后我想将所有消息合并为一个。但是当我这样做时,我只得到所有位置,而不是边框 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")
路线之后我的 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>
插入了新数据,但order-Border丢失了。我该怎么做才能获取所有数据?
我的拆分器 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;
}
}
我知道,我只从拆分的消息中复制正文。但我不知道如何获得原始消息,以获取所有部分。你有什么想法吗?
据我所知,当您使用 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" 以交换属性并重建新的订单消息。
另一种选择:
你总是可以通过 UnitOfWork 接口获得路线的原始主体
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message originalMessage = (Message)
exchange.getUnitOfWork().getOriginalInMessage();
(do something...)
}
})
我自己写处理器。使用 Camel 内置方法很难解决这个问题。
感谢您的帮助。
在我的路线中,我使用拆分器将位置拆分为单个消息。单个消息丰富了一些数据。拆分后我想将所有消息合并为一个。但是当我这样做时,我只得到所有位置,而不是边框 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")
路线之后我的 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>
插入了新数据,但order-Border丢失了。我该怎么做才能获取所有数据?
我的拆分器 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;
}
}
我知道,我只从拆分的消息中复制正文。但我不知道如何获得原始消息,以获取所有部分。你有什么想法吗?
据我所知,当您使用 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" 以交换属性并重建新的订单消息。
另一种选择: 你总是可以通过 UnitOfWork 接口获得路线的原始主体
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message originalMessage = (Message)
exchange.getUnitOfWork().getOriginalInMessage();
(do something...)
}
})
我自己写处理器。使用 Camel 内置方法很难解决这个问题。
感谢您的帮助。