此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-doc.cadn.net.cn

进纸适配器

Spring Integration 通过Feed适配器提供对联合的支持。 该实施基于罗马框架spring-doc.cadn.net.cn

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

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

Web 联合是一种发布材料的方式,例如新闻报道、新闻稿、博客文章和其他通常在网站上提供但也以 RSS 或 ATOM 等提要格式提供的内容。spring-doc.cadn.net.cn

Spring 集成通过其“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"

进给入站通道适配器

真正需要为检索源提供支持的唯一适配器是入站通道适配器。 它允许您订阅特定的 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属性。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选项已被删除,并且重载了FeedEntryMessageSource构造函数org.springframework.core.io.Resource被提供。 当源源不是 HTTP 端点而是任何其他资源(例如 FTP 上的本地或远程)时,这很有用。 在FeedEntryMessageSource逻辑,此类资源(或提供URL) 被SyndFeedInputSyndFeed对象。 您还可以注入自定义的SyndFeedInput(例如,使用allowDoctypesoption) 实例添加到FeedEntryMessageSource.spring-doc.cadn.net.cn

如果与源的连接需要一些自定义,例如连接和读取超时,则org.springframework.core.io.UrlResource扩展名及其customizeConnection(HttpURLConnection)必须使用 override 而不是 plainURL注入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");
}