对于最新稳定版本,请使用 Spring Integration 7.0.0spring-doc.cadn.net.cn

供电适配器

Spring Integration 支持通过供源适配器进行联合传输。 该实现基于ROME框架spring-doc.cadn.net.cn

你需要把这种依赖性纳入你的项目中:spring-doc.cadn.net.cn

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

网络联合发布是一种发布新闻报道、新闻稿、博客文章及其他通常可在网站上获取的材料,同时也以RSS或ATOM等订阅源形式提供的方式。spring-doc.cadn.net.cn

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

在之前的配置中,我们订阅的是一个由网址属性。spring-doc.cadn.net.cn

当新闻内容被检索时,会被转换为消息并发送到由渠道属性。 每个消息的有效载荷是com.rometools.rome.feed.synd.SyndEntry实例。 每个版本都包含了关于新闻项目的各种数据(内容、日期、作者及其他细节)。spring-doc.cadn.net.cn

入站的通道适配器是一个轮询消费者。 这意味着你必须提供一个轮询器配置。 然而,关于订阅源,你必须明白一个重要的事实是,它的内部运作与大多数其他民调消费者略有不同。 当一个入站信号适配器启动时,它会进行第一次轮询并接收到com.rometools.rome.feed.synd.SyndFeed实例。 该对象包含多个联合入口对象。 每个条目都存储在本地条目队列中,并根据每轮询最大消息数属性,使得每个消息包含一个条目。 如果在从入口队列检索条目时队列变空,适配器会尝试更新数据流,从而在队列中填充更多条目(联合入口实例),如果有的话。 否则,下一次轮询源的尝试由轮询器的触发器决定(在前一配置中每十秒一次)。spring-doc.cadn.net.cn

重复参赛

投票推送时,可能会显示已经处理过的条目(“我已经看过那条新闻了,你为什么还要再给我看?”)。 Spring Integration 提供了一种便捷的机制,消除了对重复条目担忧的担忧。 每个订阅源条目都有一个“发布日期”字段。 每次都有新的消息生成并发送,Spring Integration 将最近发布日期的值存储在元数据存储策略(参见元数据存储)。 这元数据键用于持久化最新发布日期。spring-doc.cadn.net.cn

其他选择

从5.0版本开始,已被弃用的com.rometools.fetcher.FeedFetcher选项已被移除且超载信息来源构造器用于org.springframework.core.io.Resource提供。 当源不是HTTP端点,而是任何其他资源(如本地或FTP上的远程)时,这非常有用。 在信息来源逻辑,这样的资源(或提供的)网址)被解析为SyndFeedInput前往SyndFeed用于前述处理的对象。 你也可以注射定制的SyndFeedInput(例如,其中allowDoctypes选项)实例进入信息来源.spring-doc.cadn.net.cn

如果连接源需要定制,例如连接和读取超时,则org.springframework.core.io.UrlResource其扩张customizeConnection(HttpURLConnection)必须使用覆盖而非普通网址注入信息来源. 例如: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");
}