【问题标题】:Multiple RSS feeds into one channel多个 RSS 提要到一个频道
【发布时间】:2017-04-16 11:29:42
【问题描述】:

我一直在研究通过 Spring Integration 使用多个 RSS 提要。我在这里遵循了集成指南:https://spring.io/guides/gs/integration/,这很棒。

是否可以将多个入站通道适配器(用于多个提要)写入一个通道?

是否也可以使用数据中的通道适配器 ID 来识别提要(例如 Spring 博客)?

任何示例代码都会很棒。

【问题讨论】:

    标签: rss spring-integration


    【解决方案1】:

    是的,只需给通道适配器一个id 并命名通道(如果没有channel,id 将成为适配器的通道名称)。您可以将多个适配器馈送到同一通道。添加标题丰富器以识别源...

    <feed:inbound-channel-adapter id="spring"
             channel="springblog" url="http://spring.io/blog.atom" auto-startup="${auto.startup:true}">
        <int:poller fixed-rate="5000"/>
    </feed:inbound-channel-adapter>
    
    <int:header-enricher input-channel="springblog" output-channel="news">
        <int:header name="source" value="spring.blog"/>
    </int:header-enricher>
    
    <int:transformer
        input-channel="news"
        expression=
          "headers['source'] + ':' + payload.title + ' @ ' + payload.link + '#{systemProperties['line.separator']}'"
        output-channel="file"/>
    
    <file:outbound-channel-adapter id="file"
        mode="APPEND"
        charset="UTF-8"
        directory="/tmp/si"
        filename-generator-expression="'${feed.file.name:SpringBlog}'"/>
    

    使用较新的 Java DSL 而不是 XML,这将是...

    @Bean
    public IntegrationFlow blog() throws Exception {
        return IntegrationFlows
            .from(new FeedEntryMessageSource(new URL(BLOG_URI), "blog"), e -> e.id("blog").poller(Pollers.fixedDelay(5000)))
            .enrichHeaders(h -> h.header("source", "spring.blog"))
            .channel("news")
            .get();
    }
    
    @Bean
    public IntegrationFlow newsFlow() {
        return IntegrationFlows.from("news")
            .transform("headers['source'] + ':' + payload.title + ' @ ' + payload.link + '" + newline + "'") // SpEL
            .handle(Files.outboundAdapter(new File("/tmp/si/"))
                .fileNameExpression("'SpringBlogDSL'")
                .fileExistsMode(FileExistsMode.APPEND))
            .get();
    }
    

    编辑

    动态流注册...

    @Autowired
    private IntegrationFlowContext flowContext;
    
    ...
    
        IntegrationFlow newFLow = IntegrationFlows
            .from(new FeedEntryMessageSource(new URL(BLOG_URI), "blog"), e -> e.id("blog").poller(Pollers.fixedDelay(5000)))
            .enrichHeaders(h -> h.header("source", "spring.blog"))
            .channel("news")
            .get();
        this.flowContent.registration(newFlow).register();
    

    有关完整示例,请参阅dynamic-tcp-client sample。

    【讨论】:

    • 感谢加里,这是一个很大的帮助。考虑一下,我可能想将其创建为一个微服务,它允许在运行时动态添加提要。这可能吗?我假设每个提要都必须有一个单独的入站通道适配器,以便它可以扩展以处理大量提要。
    • 是的,您可以动态添加流;请参阅我的答案的编辑。如果使用 XML 配置,则必须创建子应用程序上下文;有关示例,请参见动态 ftp 示例。 README 讨论了使用子上下文作为消息源。
    • 谢谢 Gary,这周我会去。很高兴得到这种支持。
    【解决方案2】:

    是的,您可以将多个通道适配器连接到同一个消息通道。实际上,任何生产者都可以发送到任何频道并要求您提供有关其他信息的请求,我建议在每个提要入站适配器之后有一个标头丰富器来填充所需的特征。之后,您可以发送到该单个通道进行处理。

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2016-02-06
      • 1970-01-01
      相关资源
      最近更新 更多