Feed Adapter

Spring Integration 通过 Feed 适配器提供聚合支持。 其实现基于 ROME 框架。 您需要将此依赖项添加到您的项目中:

  • Maven

  • Gradle

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

Web 聚合是一种发布材料的方式,例如新闻报道、新闻稿、博客文章和其他通常在网站上提供但也可以以 RSS 或 ATOM 等 Feed 格式提供的项目。 Spring Integration 通过其“Feed”适配器提供 Web 聚合支持,并为其提供方便的基于命名空间的配置。 要配置“Feed”命名空间,请在 XML 配置文件头中包含以下元素:

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 入站通道适配器

您真正需要提供支持以检索 Feed 的唯一适配器是入站通道适配器。 它允许您订阅特定的 URL。 以下示例显示了可能的配置:

  • Java DSL

  • Java

  • XML

@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。

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

入站 Feed 通道适配器是一个轮询消费者。 这意味着您必须提供轮询器配置。 然而,关于 Feed,您必须了解一个重要的事情是,它的内部工作方式与大多数其他轮询消费者略有不同。 当入站 Feed 适配器启动时,它会进行第一次轮询并接收一个 com.rometools.rome.feed.synd.SyndFeed 实例。 该对象包含多个 SyndEntry 对象。 每个条目都存储在本地条目队列中,并根据 max-messages-per-poll 属性的值释放,以便每条消息包含一个条目。 如果在从条目队列检索条目期间,队列已变为空,则适配器会尝试更新 Feed,从而用更多条目(SyndEntry 实例)填充队列(如果有)。 否则,下一次轮询 Feed 的尝试由轮询器的触发器决定(在前面的配置中每十秒一次)。

重复条目

轮询 Feed 可能会导致已经处理过的条目(“我以前读过那条新闻,为什么又给我看一遍?”)。 Spring Integration 提供了一种方便的机制来消除对重复条目担忧的需要。 每个 Feed 条目都有一个“发布日期”字段。 每次生成并发送新 Message 时,Spring Integration 都会将最新发布日期的值存储在 MetadataStore 策略的实例中(参见 Metadata Store)。 metadataKey 用于持久化最新发布日期。

其他选项

从 5.0 版本开始,已移除已弃用的 com.rometools.fetcher.FeedFetcher 选项,并提供了一个 org.springframework.core.io.Resource 的重载 FeedEntryMessageSource 构造函数。 当 Feed 源不是 HTTP 端点而是任何其他资源(例如本地或 FTP 上的远程)时,这很有用。 在 FeedEntryMessageSource 逻辑中,此类资源(或提供的 URL)由 SyndFeedInput 解析为 SyndFeed 对象以进行前面提到的处理。 您还可以将自定义的 SyndFeedInput 实例(例如,带有 allowDoctypes 选项)注入到 FeedEntryMessageSource 中。

如果到 Feed 的连接需要一些自定义,例如连接和读取超时,则必须使用 org.springframework.core.io.UrlResource 扩展及其 customizeConnection(HttpURLConnection) 覆盖,而不是将纯 URL 注入到 FeedEntryMessageSource 中。 例如:

@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");
}