3

I wrote my app that reads RSS feed. It works super with one channel which I have in beans.xml like this:

<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"/>

Every time it just calls RssHandler which deal with SyndEntry. But what should I do if I'd like to read few rss urls (2,5,20 or etc...)?

kryger
  • 12,906
  • 8
  • 44
  • 65
user3742622
  • 1,037
  • 3
  • 22
  • 40

1 Answers1

1

Create your own implementation of org.springframework.integration.core.MessageSource and use it in input-channel reference like the following:

<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>

The class NewsReader uses list mentioned in urls propriety and retrieve the feed. Please refer to the receive method below.

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;
    }

In case you want to take a look of my complete code:

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

Bikash Shaw
  • 49
  • 3
  • 9