多个 RSS 提要到一个频道

Multiple RSS feeds into one channel

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

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

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

任何示例代码都会很棒。

是的,您可以将多个通道适配器连接到同一个消息通道。实际上,任何生产者都可以发送到任何渠道并收到您关于其他信息的请求,我建议在每个 feed Inbound Adapter 之后添加一个 header enricher 来填充所需的特征。然后你可以发送到那个单一的渠道进行处理。

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

<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