|
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.0! |
Feed 适配器
Spring 集成通过Feed适配器提供对联合的支持。 该实现基于 ROME 框架。
您需要将此依赖项包含在您的项目中:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
<version>6.4.1-SNAPSHOT</version>
</dependency>
compile "org.springframework.integration:spring-integration-feed:6.4.1-SNAPSHOT"
Web 联合是一种发布材料(如新闻报道、新闻稿、博客文章和其他项目)的方法,这些材料通常在网站上提供,但也以 RSS 或 ATOM 等源格式提供。
Spring 集成通过其“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 入站通道适配器
您真正需要为检索源提供支持的唯一适配器是入站通道适配器。 它允许您订阅特定的 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属性。
检索新闻项时,它们将转换为消息并发送到由channel属性。
每条消息的有效负载是一个com.rometools.rome.feed.synd.SyndEntry实例。
每个 API 都封装了有关新闻项的各种数据(内容、日期、作者和其他详细信息)。
入站源通道适配器是轮询使用者。
这意味着你必须提供一个 poller 配置。
但是,您必须了解的关于源的一件重要事情是,它的内部工作原理与大多数其他轮询消费者略有不同。
当入站 Feed 适配器启动时,它会执行第一次轮询并接收一个com.rometools.rome.feed.synd.SyndFeed实例。
该对象包含多个SyndEntry对象。
每个条目都存储在本地条目队列中,并根据max-messages-per-poll属性,以便每条消息都包含一个条目。
如果在从条目队列中检索条目期间,队列已变为空,则适配器将尝试更新源,从而在队列中填充更多条目 (SyndEntry实例)的 Properties。
否则,下一次轮询源的尝试由 Poller 的触发器确定(在前面的配置中每 10 秒)。
重复条目
轮询 Feed 可能会导致条目已被处理(“我已经阅读了该新闻项,为什么还要再次向我显示它?
Spring 集成提供了一种方便的机制,无需担心重复的条目。
每个 Feed 条目都有一个 “published date” 字段。
每次新的Message生成并发送时,Spring Integration 会将最新发布日期的值存储在MetadataStore策略(请参阅元数据存储)。
这metadataKey用于保留最新发布日期。
其他选项
从版本 5.0 开始,已弃用的com.rometools.fetcher.FeedFetcher选项已被删除,并且FeedEntryMessageSource构造函数org.springframework.core.io.Resource。
当源源不是 HTTP 端点而是任何其他资源(如 FTP 上的 local 或 remote)时,这非常有用。
在FeedEntryMessageSource逻辑,例如资源(或提供的URL) 由SyndFeedInput到SyndFeedobject 进行前面提到的处理。
您还可以注入自定义的SyndFeedInput(例如,使用allowDoctypesoption) 实例复制到FeedEntryMessageSource.
|
如果与 Feed 的连接需要一些自定义,例如连接和读取超时,则
|