[英]How to customize message aggregation logic in Spring Integration Java DSL
在集成流中,具有默認策略的拆分將發布列表中的一項。 該項目的處理可能會失敗。 我要處理該錯誤,並將包含映射信息的新消息從前一個(除了自定義錯誤標頭之外)定向到普通消息通道。
在聚合器中,我想自定義聚合邏輯以生成其他類型的消息,其中包含失敗進程的數量和未失敗消息的結果。
在這里,我解釋了如何發送帶有標題的錯誤消息:
@Bean
public IntegrationFlow socialMediaErrorFlow() {
return IntegrationFlows.from("socialMediaErrorChannel")
.wireTap(sf -> sf.handle("errorService", "handleException"))
.<MessagingException>handle((p, h)
-> MessageBuilder.withPayload(Collections.<CommentEntity>emptyList())
.copyHeaders(p.getFailedMessage().getHeaders())
.setHeader("ERROR", true)
.build()
)
.channel("directChannel_1")
.get();
}
我希望聚合器生成這種類型的對象:
public class Result {
private Integer totalTask;
private Integer taskFailed;
private List<CommentEntity> comments;
}
我應該如何處理?
提前致謝。
在Artem的幫助下,我實現了此實現:
.aggregate(a -> a.outputProcessor(new MessageGroupProcessor() {
@Override
public Object processMessageGroup(MessageGroup mg) {
Integer failedTaskCount = 0;
Integer totalTaskCount = mg.getMessages().size();
List<CommentEntity> comments = new ArrayList<>();
for(Message<?> message: mg.getMessages()){
if(message.getHeaders().containsKey("ERROR"))
failedTaskCount++;
else
comments.addAll((List<CommentEntity>)message.getPayload());
}
return new IterationResult(totalTaskCount, failedTaskCount, comments);
}
}))
AggregatorSpec
具有outputProcessor
屬性:
/**
* A processor to determine the output message from the released group. Defaults to a message
* with a payload that is a collection of payloads from the input messages.
* @param outputProcessor the processor.
* @return the aggregator spec.
*/
public AggregatorSpec outputProcessor(MessageGroupProcessor outputProcessor) {
在這里,您可以提供自己的自定義邏輯,以分析組中的所有消息並為其構建Result
。
來自測試用例的樣本:
.aggregate(a -> a.outputProcessor(g -> g.getMessages()
.stream()
.map(m -> (String) m.getPayload())
.collect(Collectors.joining(" "))))
Cafe Demo示例:
.aggregate(aggregator -> aggregator
.outputProcessor(g ->
new Delivery(g.getMessages()
.stream()
.map(message -> (Drink) message.getPayload())
.collect(Collectors.toList())))
.correlationStrategy(m -> ((Drink) m.getPayload()).getOrderNumber()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.