数据源适配器
Spring Integration 通过 feed 适配器提供对聚合的支持。实现是基于 ROME Framework 的。
你需要将这个依赖添加到你的项目中:
- Maven
- Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
<version>6.4.2</version>
</dependency>
compile "org.springframework.integration:spring-integration-feed:6.4.2"
Web syndication 是一种发布材料的方式,例如新闻报道、新闻稿、博客文章等通常在网站上可用的项目,也可以通过 RSS 或 ATOM 等信息格式提供。
Spring 集成通过其 'feed' 适配器为网络聚合提供支持,并为其提供了基于命名空间的便捷配置。要配置 '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 输入通道适配器
你真正需要提供支持以检索 feeds 的唯一适配器是入站通道适配器。它让你可以订阅特定的 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
属性标识的通道。每个消息的有效载荷是一个 com.rometools.rome.feed.synd.SyndEntry
实例。每个实例封装了关于新闻项目的各种数据(内容、日期、作者和其他详细信息)。
入站馈送通道适配器是轮询消费者。这意味着你必须提供一个轮询器配置。然而,关于馈送有一个重要的事情你需要理解,即其内部工作方式与其他大多数轮询消费者略有不同。当入站馈送适配器启动时,它会进行第一次轮询并接收一个 com.rometools.rome.feed.synd.SyndFeed
实例。该对象包含多个 SyndEntry
对象。每个条目都存储在本地条目队列中,并根据 max-messages-per-poll
属性中的值逐个释放,使得每条消息包含一个条目。如果在从条目队列中检索条目期间,队列变为空,则适配器尝试更新馈送,从而用更多条目(SyndEntry
实例)填充队列,如果有的话。否则,下一次轮询馈送的尝试将由轮询器的触发器确定(在前面的配置中为每十秒一次)。
重复条目
轮询一个信息源可能会导致出现已经处理过的条目(“我已经读过那条新闻,为什么你又显示给我?”)。Spring Integration 提供了一种便捷的机制来消除对重复条目的担忧。每个信息源条目都有一个“发布日期”字段。每次生成并发送新的 Message
时,Spring Integration 都会在 MetadataStore
策略的一个实例中存储最新的发布日期值(参见元数据存储)。metadataKey
用于持久化最新的发布日期。
其他选项
从 5.0 版本开始,已弃用的 com.rometools.fetcher.FeedFetcher
选项已被移除,并提供了一个重载的 FeedEntryMessageSource
构造函数用于 org.springframework.core.io.Resource
。这在 feed 源不是 HTTP 终端而可以是任何其他资源(例如本地或 FTP 上的远程资源)时非常有用。在 FeedEntryMessageSource
逻辑中,此类资源(或提供的 URL
)由 SyndFeedInput
解析为 SyndFeed
对象,以进行前面提到的处理。你还可以将自定义的 SyndFeedInput
(例如,带有 allowDoctypes
选项)实例注入到 FeedEntryMessageSource
中。
如果连接到源需要一些自定义设置,例如连接和读取超时,则应使用带有 customizeConnection(HttpURLConnection)
重写的 org.springframework.core.io.UrlResource
扩展,而不是直接将 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");
}