微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法使用DSL分叉Spring IntegrationFlow?

如何解决有没有办法使用DSL分叉Spring IntegrationFlow?

我想做这样的事情,其中​​网关有效载荷是一个String,而serviceA和serviceB都返回列表。

    final IntegrationFlow flowA = flow -> flow
            .handle(serviceA) 
            .handle((payload,headers) -> payload); // List<Object>

    final IntegrationFlow flowB = flow -> flow
            .handle(serviceB) 
            .handle((payload,headers) -> payload); // List<Object>


    return IntegrationFlows
            .from(myGateway) // String payload
            .forkAndMerge(flowA,flowB,executor)
            .handle((payload,headers) -> payload)
            .get();

是否可以将流程分成两部分,然后收集结果?拆分器和聚合器的大多数示例都涉及拆分列表。

解决方法

请参见.scatterGather()变体。

Main docs for the ScatterGatherer here

编辑

示例:

@SpringBootApplication
public class So63605348Application {

    private static final Logger log = LoggerFactory.getLogger(So63605348Application.class);

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

    @Bean
    IntegrationFlow flow(TaskExecutor exec) {
        return IntegrationFlows.from(() -> "foo",e -> e.poller(Pollers.fixedDelay(5000)))
                .scatterGather(s -> s.applySequence(true)
                        .recipientFlow(subFlow -> subFlow.channel(c -> c.executor(exec))
                                .<String>handle((p,h) -> {
                                    log.info(p.toString());
                                    return p.toUpperCase();
                                }))
                        .recipientFlow(subFlow -> subFlow.channel(c -> c.executor(exec))
                                .<String>handle((p,h) -> {
                                    log.info(p.toString());
                                    return p + p;
                                })))
                .handle(System.out::println)
                .get();
    }

    @Bean
    public TaskExecutor exec() {
        ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
        exec.setCorePoolSize(2);
        return exec;
    }

}

结果

2020-08-26 17:33:56.769  INFO 50829 --- [         exec-1] com.example.demo.So63605348Application   : foo
2020-08-26 17:33:56.769  INFO 50829 --- [         exec-2] com.example.demo.So63605348Application   : foo
GenericMessage [payload=[foofoo,FOO],headers=...

EDIT2

如果您不想嵌套子流,则可以将它们排除在外...

@Bean
IntegrationFlow flow(TaskExecutor exec) {
    return IntegrationFlows.from(() -> "foo",e -> e.poller(Pollers.fixedDelay(5000)))
            .scatterGather(s -> s.applySequence(true)
                    .recipientFlow(flow2())
                    .recipientFlow(flow3()))
            .handle(System.out::println)
            .get();
}

@Bean
IntegrationFlow flow2() {
    return f -> f
        .<String>handle((p,h) -> {
            log.info(p.toString());
            return p + p;
        });
}

@Bean
IntegrationFlow flow3() {
    return f -> f
        .<String>handle((p,h) -> {
            log.info(p.toString());
            return p.toUpperCase();
        });
}

或者您可以使用pub / sub频道变体...

@Bean
IntegrationFlow flow() {
    return IntegrationFlows.from(() -> "foo",e -> e.poller(Pollers.fixedDelay(5000)))
            .scatterGather(pubSub())
            .handle(System.out::println)
            .get();
}

@Bean
PublishSubscribeChannel  pubSub() {
    PublishSubscribeChannel pubSub = new PublishSubscribeChannel(exec());
    pubSub.setApplySequence(true);
    return pubSub;
}

@Bean
IntegrationFlow flow2() {
    return IntegrationFlows.from("pubSub")
        .<String>handle((p,h) -> {
            log.info(p.toString());
            return p + p;
        })
        .get();
}

@Bean
IntegrationFlow flow3() {
    return IntegrationFlows.from("pubSub")
        .<String>handle((p,h) -> {
            log.info(p.toString());
            return p.toUpperCase();
        })
        .get();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。