Spring Integration:如何阅读多个RSS频道?

时间:2015-11-05 21:02:45

标签: rss spring-integration

我写了我的应用程序,读取RSS提要。它在我的beans.xml中有一个通道超级工作,如下所示:

<feed:inbound-channel-adapter id="news"
                                  channel="inputRssFeedChannel"
                                  url="http://feeds.feedburner.com/Techcrunch">
        <int:poller fixed-rate="5000" max-messages-per-poll="100"/>
    </feed:inbound-channel-adapter>

    <int:service-activator input-channel="inputRssFeedChannel"
                           ref="rssPrintOutService"
                           method="printRss"
                           output-channel="nullChannel"/>

每次只调用处理SyndEntry的RssHandler。但是,如果我想阅读几个RSS网址(2,5,20或其他等等),我该怎么办?

1 个答案:

答案 0 :(得分:1)

创建自己的org.springframework.integration.core.MessageSource实现,并在输入通道引用中使用它,如下所示:

<int:inbound-channel-adapter id="newsInput" ref="newsReader">
        <int:poller fixed-rate="1" time-unit="SECONDS" max-messages-per-poll="1"/>
    </int:inbound-channel-adapter>

    <bean id="newsReader" class="blog.NewsReader">
        <property name="fetcherListener">
            <bean class="blog.helper.FetcherEventListenerImpl"/>
        </property>
        <property name="urls">
            <list>
                <value>http://www.gridshore.nl/feed/</value>
                <value>https://spring.io/blog.atom</value>
                <value>http://feeds.foxnews.com/foxnews/video?format=xml</value>
            </list>
        </property>
    </bean>

类NewsReader使用urls propriety中提到的列表并检索feed。 请参阅下面的接收方法。

public class NewsReader implements MessageSource, InitializingBean {
    private static Logger logger = LoggerFactory.getLogger(NewsReader.class);
    private FeedFetcherCache feedInfoCache;
    private FeedFetcher feedFetcher;
    private FetcherListener fetcherListener;
    private List<String> urls;

    @Override
    public Message receive() {
        List<SyndFeed> feeds = obtainFeedItems();
        return MessageBuilder.withPayload(feeds)
                .setHeader("feedid", "newsfeed").build();
    }

    private List<SyndFeed> obtainFeedItems() {
        List<SyndFeed> feed = new ArrayList<>();
        try {
            for (String url : urls) {
                feed.add(feedFetcher.retrieveFeed(new URL(url)));
            }
        } catch (IOException e) {
            logger.error("IO Problem while retrieving feed", e);
        } catch (FeedException e) {
            logger.error("Feed Problem while retrieving feed", e);
        } catch (FetcherException e) {
            logger.error("Fetcher Problem while retrieving feed", e);
        }
        return feed;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        feedInfoCache = HashMapFeedInfoCache.getInstance();
        feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
        if (fetcherListener != null) {
            feedFetcher.addFetcherEventListener(fetcherListener);
        }
    }

    public void setFetcherListener(FetcherListener fetcherListener) {
        this.fetcherListener = fetcherListener;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }

如果您想查看我的完整代码:

git clone https://github.com/BikashShaw/MultipleRSSFeedRead.git