如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

Feed Adapter

Spring Integration 通过 Feed 适配器提供支持,用于聚合内容。 其实现基于 ROME Frameworkspring-doc.cadn.net.cn

您需要将以下依赖项包含到您的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-feed</artifactId>
    <version>6.4.10</version>
</dependency>
compile "org.springframework.integration:spring-integration-feed:6.4.10"

Web syndication 是一种发布新闻故事、新闻稿、博客文章和其他通常可在网站上获取的内容的方式,同时以 RSS 或 ATOM 等订阅格式提供这些内容。spring-doc.cadn.net.cn

Spring Integration 通过其 'feed' 适配器提供对 Web 内容聚合的支持,并提供基于名称空间的便捷配置方式。 要配置 'feed' 名称空间,请在 XML 配置文件的头部包含以下元素:spring-doc.cadn.net.cn

xmlns:int-feed="http://www.springframework.org/schema/integration/feed"
xsi:schemaLocation="http://www.springframework.org/schema/integration/feed
	https://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd"

传入通道适配器

您真正需要提供的用于支持检索 feed 的适配器是入站通道适配器。 它允许您订阅特定的 URL。 以下示例展示了一种可能的配置:spring-doc.cadn.net.cn

@Configuration
@EnableIntegration
public class ContextConfiguration {

    @Value("org/springframework/integration/feed/sample.rss")
    private Resource feedResource;

    @Bean
    public IntegrationFlow feedFlow() {
        return IntegrationFlow
                .from(Feed.inboundAdapter(this.feedResource, "feedTest")
                                .preserveWireFeed(true),
                        e -> e.poller(p -> p.fixedDelay(100)))
                .channel(c -> c.queue("entries"))
                .get();
    }

}
@Bean
@InboundChannelAdapter(inputChannel = "fromFeed")
public FeedEntryMessageSource feedEntrySource() {
    return new FeedEntryMessageSource("https://feeds.bbci.co.uk/news/rss.xml", "metadataKey");
}
<int-feed:inbound-channel-adapter id="feedAdapter"
        channel="feedChannel"
        url="https://feeds.bbci.co.uk/news/rss.xml">
    <int:poller fixed-rate="10000" max-messages-per-poll="100" />
</int-feed:inbound-channel-adapter>

在前面的配置中,我们订阅了由 url 属性标识的 URL。spring-doc.cadn.net.cn

随着新闻条目的检索,它们被转换为消息并发送到由 channel 属性标识的通道。 每条消息的有效负载是一个 com.rometools.rome.feed.synd.SyndEntry 实例。 每个实例封装了有关新闻条目的各种数据(内容、日期、作者和其他详细信息)。spring-doc.cadn.net.cn

入站源适配器是一个轮询消费者。 这意味着您必须提供轮询器配置。 然而,关于源的一个非常重要的事项是,其内部运作方式与其他大多数轮询消费者略有不同。 当入站源适配器启动时,它会执行首次轮询并接收一个 com.rometools.rome.feed.synd.SyndFeed 实例。 该对象包含多个 SyndEntry 对象。 每个条目都存储在本地条目队列中,并根据 max-messages-per-poll 属性中的值进行释放,使得每条消息仅包含一个条目。 如果在从条目队列检索条目的过程中队列为空,适配器将尝试更新源,从而用更多条目(SyndEntry 实例)填充队列(如果有的话)。 否则,下一次轮询源的时机由轮询器的触发器决定(在前述配置中为每十秒一次)。spring-doc.cadn.net.cn

重复条目

轮询订阅源可能会导致出现已处理过的条目(“我已经读过那条新闻了,为什么又显示给我?”)。 Spring Integration 提供了一种便捷的机制,以消除对重复条目的担忧。 每个订阅源条目都有一个“发布日期”字段。 每当生成并发送一个新的 Message 时,Spring Integration 会将最新日期的值存储在 MetadataStore 策略的实例中(参见 元数据存储)。 metadataKey 用于持久化最新的发布日期。spring-doc.cadn.net.cn

其他选项

从版本 5.0 开始,已弃用的 com.rometools.fetcher.FeedFetcher 选项已被移除,并为 org.springframework.core.io.Resource 提供了重载的 FeedEntryMessageSource 构造函数。 当源不是 HTTP 端点而是其他任何资源(例如本地或远程 FTP)时,此功能非常有用。 在 FeedEntryMessageSource 逻辑中,此类资源(或提供的 URL)会被 SyndFeedInput 解析为 SyndFeed 对象,以便进行前述处理。 您还可以将自定义的 SyndFeedInput(例如使用 allowDoctypes 选项)实例注入到 FeedEntryMessageSource 中。spring-doc.cadn.net.cn

如果到 feed 的连接需要一些自定义配置,例如连接超时和读取超时,则必须使用 org.springframework.core.io.UrlResource 扩展及其 customizeConnection(HttpURLConnection) 覆盖方式,而不是直接将 URL 注入到 FeedEntryMessageSource 中。 例如:spring-doc.cadn.net.cn

@Bean
@InboundChannelAdapter("feedChannel")
FeedEntryMessageSource feedEntrySource() {
    UrlResource urlResource =
	    new UrlResource(url) {

	        @Override
	        protected void customizeConnection(HttpURLConnection connection) throws IOException {
	            super.customizeConnection(connection);
	            connection.setConnectTimeout(10000);
	            connection.setReadTimeout(5000);
	        }
	    };
    return new FeedEntryMessageSource(urlResource, "myKey");
}