繁体   English   中英

如何在Spring Integration Java DSL中自定义消息聚合逻辑

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM