【问题标题】:How to set several message handlers for channel in spring integration DSL?如何在 Spring Integration DSL 中为通道设置多个消息处理程序?
【发布时间】:2019-12-27 20:26:31
【问题描述】:

我编写了我的第一个 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;
    }
}

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

我怎样才能实现它?

其他问题:

元数据键是什么意思?

【问题讨论】:

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


    【解决方案1】:

    有一个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

    【讨论】:

    • 它有帮助。管道配置有 3 个选项是否正确:xml、注释和 java config?有比较吗?
    • 它们在最终运行时模型方面是等价的。只是您的选择!我们推荐使用 Java DSL,因为我们在底层做了很多工作以避免一些样板代码,并且有很多 IDE 体验的语法糖。
    • 谢谢你,阿尔乔姆。我想开始学习了解基本示例github.com/spring-projects/spring-integration-samples/tree/…,但它只包含 xml 配置(我检查了几个示例)。我真的不喜欢xml。你有使用 java DSL 配置的相同示例吗?
    • 当然不是全部,但您可以在以下网址找到:github.com/spring-projects/spring-integration-samples/tree/…
    • github.com/spring-projects/spring-integration-samples/blob/... 看起来像是注释示例,但它位于 DSL 下。对我来说,这有点乱,或者我就是不明白
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2016-12-21
    • 2018-04-15
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多