如何解决在 spring 集成中一次获取 SyndEntry 列表而不是一个 SyndEntry 对象?
我希望 RssFeed 适配器提供对象列表,但它一次生成一个对象。如何获取多个对象?如果我可以缓冲这些对象并创建它的列表,然后将其转发以供进一步处理,是否有可能?
@Configuration
@EnableIntegration
public class RSSFeedFlow {
@Value("https://www.aljazeera.com/xml/rss/all.xml")
private Resource feedResource;
@Bean
public MetadataStore metadataStore() {
PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
metadataStore.setBaseDirectory("src/main/resources");
return metadataStore;
}
public MessageChannel outputChannel() {
return MessageChannels.direct().get();
}
@Bean
public IntegrationFlow rssFeedFlow() {
return IntegrationFlows.from(Feed.inboundAdapter(feedResource,"feedTest")
.metadataStore(metadataStore()),spec -> spec.poller(p -> p.fixedDelay(5000).maxMessagesPerPoll(5)))
.channel("outputChannel")
.get();
}
@Bean
public IntegrationFlow print() {
return IntegrationFlows.from("outputChannel")
.handle(message -> {
SyndEntry syndEntry = (SyndEntry) message.getPayload();
System.out.println(syndEntry.getLink());
})
.get();
}
}
解决方法
Feed.inboundAdapter()
基于 FeedEntryMessageSource
。查看它的 JavaDoc:
* This implementation of {@link org.springframework.integration.core.MessageSource} will
* produce individual {@link SyndEntry}s for a feed identified with the 'feedUrl'
* attribute.
因此,将 SyndFeed
分散到其条目中确实是一种预期行为。
您可以考虑使用聚合器端点将多个条目聚合到某个列表中:here
或者你可以考虑实现自己的消息源。像这样:
@Bean
public IntegrationFlow rssFeedFlow() {
return IntegrationFlows.fromSupplier(() -> new SyndFeedInput().build(new XmlReader(this.feedResource.getInputStream())),spec -> spec.poller(p -> p.fixedDelay(5000).maxMessagesPerPoll(5)))
.channel("outputChannel")
.get();
}
FeedEntryMessageSource
执行一些其他逻辑(如缓存和过滤)的单个条目发射点。因此,拥有一个普通的 SyndFeed
生产者并不会为框架带来太多价值,该生产者可能会在每个轮询周期获得相同的数据。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。