如何在Spring Integration DSL中为通道设置几个消息处理程序?

时间:2019-08-22 13:33:56

标签: java spring spring-boot spring-integration spring-integration-dsl

我编写了我的第一个spring集成应用程序,该应用程序从spring RSS中读取数据并将其记录到控制台:

@Configuration
@EnableIntegration
@IntegrationComponentScan
public class DslConfig {

    @Bean
    public IntegrationFlow feedFlow() throws MalformedURLException {
        return IntegrationFlows.from(inBoundFeedDataAdapter(), configurer -> configurer.poller(Pollers.fixedDelay(1000)))
                .channel(newsChannel())
                .transform(source -> {
                    SyndEntry e = ((SyndEntry) source);
                    return e.getTitle() + " " + e.getLink();
                })
                .handle(messageHandler())
                .get();
    }

    @Bean
    public FeedEntryMessageSourceSpec inBoundFeedDataAdapter() throws MalformedURLException {
        return Feed.inboundAdapter(new URL("https://spring.io/blog.atom"), "some_key");
    }

    @Bean
    public MessageChannel newsChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageHandler messageHandler() {
        return System.out::println;
    }
}

但是我不知道如何添加一个额外的处理程序以将结果写入文件。

如何实现?

其他问题:

元数据键是什么意思?

1 个答案:

答案 0 :(得分:2)

在流程中放置一个publishSubscribeChannel(),您可以在其中添加subscribe()用于几个子流程。他们每个人都将得到相同的消息来处理。如果您还向配置中添加Executor,则该过程将并行进行:

.publishSubscribeChannel(s -> s
                        .applySequence(true)
                        .subscribe(f -> f
                                .handle((p, h) -> "Hello"))
                        .subscribe(f -> f
                                .handle((p, h) -> "World!"))
                );

在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/5.2.0.BUILD-SNAPSHOT/reference/html/dsl.html#java-dsl-subflows