此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

Spring Integration 通过源适配器提供对联合的支持。 该实施基于罗马框架Spring中文文档

您需要将此依赖项包含在项目中:Spring中文文档

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

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

Spring 集成通过其“feed”适配器提供对 Web 联合的支持,并为其提供方便的基于命名空间的配置。 若要配置“feed”命名空间,请在 XML 配置文件的标头中包含以下元素:Spring中文文档

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中文文档

@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。urlSpring中文文档

检索新闻项时,它们将转换为消息并发送到由属性标识的频道。 每条消息的有效负载都是一个实例。 每个都封装了有关新闻项目的各种数据(内容、日期、作者和其他详细信息)。channelcom.rometools.rome.feed.synd.SyndEntrySpring中文文档

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

重复条目

对 Feed 进行轮询可能会导致已处理的条目(“我已经阅读了该新闻,为什么您再次向我显示它? Spring Integration 提供了一种方便的机制,无需担心重复条目。 每个 Feed 条目都有一个“发布日期”字段。 每次生成并发送新内容时,Spring Integration都会将最新发布日期的值存储在策略的实例中(请参阅元数据存储)。 用于保留最新发布日期。MessageMetadataStoremetadataKeySpring中文文档

其他选项

从 V5.0 开始,删除了已弃用的选项,并提供了 的重载构造函数。 当源源不是 HTTP 终结点而是任何其他资源(例如 FTP 上的本地或远程资源)时,这很有用。 在逻辑中,这样的资源(或提供的)由对象解析,以便进行前面提到的处理。 您还可以将自定义(例如,使用选项)实例注入到 .com.rometools.fetcher.FeedFetcherFeedEntryMessageSourceorg.springframework.core.io.ResourceFeedEntryMessageSourceURLSyndFeedInputSyndFeedSyndFeedInputallowDoctypesFeedEntryMessageSourceSpring中文文档

如果与源的连接需要一些自定义,例如连接和读取超时,则必须使用带有其覆盖的扩展,而不是普通注入到 . 例如:org.springframework.core.io.UrlResourcecustomizeConnection(HttpURLConnection)URLFeedEntryMessageSourceSpring中文文档

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

如果与源的连接需要一些自定义,例如连接和读取超时,则必须使用带有其覆盖的扩展,而不是普通注入到 . 例如:org.springframework.core.io.UrlResourcecustomizeConnection(HttpURLConnection)URLFeedEntryMessageSourceSpring中文文档

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