Spring integration Spring集成-拆分后的publishSubscribeChannel聚合

Spring integration Spring集成-拆分后的publishSubscribeChannel聚合,spring-integration,spring-integration-dsl,Spring Integration,Spring Integration Dsl,我有一个基于DSL的流,它使用split迭代对象列表并发送卡夫卡消息: .transform(...) .split() .channel(KAFKA_OUT_CHANNEL) 发送完所有消息后,我需要调用服务,还需要记录处理了多少消息。 我知道一种方法是使用publishSubscribeChannel,其中第一个subscribe执行实际的卡夫卡发送,然后聚合执行服务调用: .transform(...) .split(). .publishSubscribeChannel(pubSub

我有一个基于DSL的流,它使用
split
迭代对象列表并发送卡夫卡消息:

.transform(...)
.split()
.channel(KAFKA_OUT_CHANNEL)
发送完所有消息后,我需要调用服务,还需要记录处理了多少消息。 我知道一种方法是使用
publishSubscribeChannel
,其中第一个
subscribe
执行实际的卡夫卡发送,然后
聚合
执行服务调用:

.transform(...)
.split().
.publishSubscribeChannel(pubSub -> pubSub
        .subscribe(f -> f.channel(KAFKA_OUT_CHANNEL)))
我在弄清楚如何使用DSL在pubSubChannel中实际执行
.aggregate
部分时遇到了问题。到目前为止,我已经尝试:

.subscribe(f ->  f.channel(KAFKA_OUT_CHANNEL)
.subscribe(f -> f.aggregate(c -> c.processor( ?? ))))

有任何指针吗?

默认情况下,
AbstractMessageSplitter
有一个
applySequence=true

/**
 * Set the applySequence flag to the specified value. Defaults to true.
 * @param applySequence true to apply sequence information.
 */
public void setApplySequence(boolean applySequence) {
因此,我们在消息中包含以下标题:

if (this.applySequence) {
    builder.pushSequenceDetails(correlationId, sequenceNumber, sequenceSize);
}
聚合器的默认关联策略实际上基于
IntegrationMessageHeaderAccessor.correlation\u ID
头。通过这种方式,它将具有相同
correlationKey
的消息收集到相同的
MessageGroup
中。默认的
ReleaseStrategy
基于
MessageGroup
sequenceSize
标题比较。最后,默认的
MessageGroupProcessor
只需将组中的所有消息收集到一条消息中,并将
集合作为有效负载。换句话说,聚合器的默认行为与拆分器完全相反

我不知道您将从聚合器执行什么输出,但是您不需要任何其他逻辑来配置聚合器-关联和释放逻辑应该基于默认状态


您可以在中找到足够的信息。

这取决于聚合后需要什么-如果您只需要有效负载列表,只需使用
aggregate()

@SpringBootApplication
public class So51059703Application {

    public static void main(String[] args) {
        SpringApplication.run(So51059703Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(ApplicationContext context) {
        return args -> {
            context.getBean("flow.input", MessageChannel.class).send(new GenericMessage<>(
                    Arrays.asList("a", "b", "c")));
        };
    }

    @Bean
    public IntegrationFlow flow() {
        return f -> f
                .split()
                .publishSubscribeChannel(p -> p
                        .subscribe(f1 -> f1.handle(System.out::println))
                        .subscribe(f2 -> f2
                                .aggregate()
                                .handle(System.out::println)));
    }

}
@springboot应用程序
公共类SO51059703应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So51059703Application.class,args);
}
@豆子
公共应用程序运行程序(ApplicationContext上下文){
返回参数->{
getBean(“flow.input”,MessageChannel.class).send(新的GenericMessage(
数组。asList(“a”、“b”、“c”);
};
}
@豆子
公共集成流(){
返回f->f
.split()
.publishSubscribeChannel(p->p
.subscribe(f1->f1.handle(System.out::println))
.订阅(f2->f2)
.aggregate()
.handle(System.out::println));
}
}
如果您只想知道计数:

@SpringBootApplication
public class So51059703Application {

    public static void main(String[] args) {
        SpringApplication.run(So51059703Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(ApplicationContext context) {
        return args -> {
            context.getBean("flow.input", MessageChannel.class).send(new GenericMessage<>(
                    Arrays.asList("a", "b", "c")));
        };
    }

    @Bean
    public IntegrationFlow flow() {
        return f -> f
                .split()
                .publishSubscribeChannel(p -> p
                        .subscribe(f1 -> f1.handle(System.out::println))
                        .subscribe(f2 -> f2
                                .aggregate(c -> c
                                        .processor(processor(), "reduce"))
                                .handle(System.out::println)));
    }

    @Bean
    public Object processor() {
        return new Object() {

            public int reduce(List<Message<?>> messages) {
                return messages.size();
            }

        };
    }

}
@springboot应用程序
公共类SO51059703应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So51059703Application.class,args);
}
@豆子
公共应用程序运行程序(ApplicationContext上下文){
返回参数->{
getBean(“flow.input”,MessageChannel.class).send(新的GenericMessage(
数组。asList(“a”、“b”、“c”);
};
}
@豆子
公共集成流(){
返回f->f
.split()
.publishSubscribeChannel(p->p
.subscribe(f1->f1.handle(System.out::println))
.订阅(f2->f2)
.骨料(c->c
.processor(processor(),“reduce”))
.handle(System.out::println));
}
@豆子
公共对象处理器(){
返回新对象(){
公共整数减少(列表)