如何使用Spring集成DSL来聚合来自队列通道的消息?

时间:2018-03-23 06:16:23

标签: spring spring-integration spring-integration-dsl

我定义了一个队列通道

@Bean("mail-action-laundry-list-channel")
public MessageChannel mailRecipientActionMessageChannel() {
    return new QueueChannel(20);
    }

下面的流程,我将从队列通道中聚合消息,我试过这个:

@Bean
public IntegrationFlow mailRecipientActionLaundryListMessageFlow(@Qualifier("laundryListMessageHandler") MessageHandler laundryListMessageHandler) {
    return IntegrationFlows.from("mail-action-laundry-list-channel")
            .log("--> laundry list messages::")
            .aggregate(aggregatorSpec -> aggregatorSpec
                    .correlationExpression("#this.payload.email")
                    .releaseExpression("#this.size() == 5")
                    .messageStore(new SimpleMessageStore(100))
                    .groupTimeout(2000))
            .transform(laundryListMessageToItemProcessDtoTransformer())
            .handle(laundryListMessageHandler)
            .get();
}

但是为什么它总是从频道聚合前5条消息,并且不再聚合其他消息

1 个答案:

答案 0 :(得分:1)

您需要在聚合器上配置expireGroupsUponCompletion(true)

  

设置为true(默认为false)时,将从邮件存储中删除已完成的组,从而允许具有相同关联的后续邮件组成新组。默认行为是将与已完成组具有相同相关性的消息发送到丢弃信道。

看起来您队列中的后续消息具有相同的email属性。因此,聚合器不能为同一相关键形成新组。

https://docs.spring.io/spring-integration/docs/5.0.3.RELEASE/reference/html/messaging-routing-chapter.html#aggregator-config