Spring Integration Feed Adapter: How to receive RSS feeds using Spring?
Join the DZone community and get the full member experience.
Join For FreeSpring Integration provides a feed adapter for Web Syndication to subscribe to an RSS or an ATOM feed.
Maven Dependency
The following Maven dependency is required for the feed adapter
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-feed</artifactId> <version>2.1.3.RELEASE</version> </dependency>
Spring Configuration
The feed adapter is configured by providing the URL of the RSS feed
and the channel where the feed will be sent. The below sample
configuration shows that feed adapter will listen to the RSS feed at http://www.skilledmonster.com/feed/
and will send a message to the channel feedChannel
.
rss-inbound.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:feed="http://www.springframework.org/schema/integration/feed" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/feed http://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd"> <int:channel id="feedChannel"> <int:queue /> </int:channel> <feed:inbound-channel-adapter id="feedAdapter" channel="feedChannel" url="http://www.skilledmonster.com/feed/"> <int:poller fixed-rate="30000" max-messages-per-poll="1" /> </feed:inbound-channel-adapter> </beans>
The message will have a payload of the type com.sun.syndication.feed.syn.SyndEntry
,
which encapsulates information about the news item including content,
dates, and authors. A poller element is required for the feed adapter,
because it is a poller consumer.
Note that the feed adapter is slightly different than other polling
consumers. When the feed adapter is first started and does its first
poll, a com.sun.syndication.feed.synd.SyndEntryFeed
instance
is received. This object contains multiple SyndEntry objects and each
entry is stored in a local entry queue and released based on the max-messages-per-poll
property of the poller. This queue will be refreshed if it becomes empty and there are additional new entries available.
Test Run
This a simple test class where I create a Spring context and messages are pulled from the message channel feedChannel
.
The payload coming from the feed adapter is of the type SyndEntry
.
The main class logs the published date and the title of the entry.
Additional information such as the context may also be derived from the SyndEntry
instance
The feed adapter may be tested using the main class as shown below
package com.skilledmonster.spring.integration.rss; import org.apache.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.core.PollableChannel; import com.skilledmonster.spring.integration.twitter.TwitterMessageConsumer; import com.sun.syndication.feed.synd.SyndEntry; /** * Feed adapter for pulling rss feeds from my own website * @author Jagadeesh * */ public class WebSyndicationInbound { private static Logger LOG = Logger.getLogger(TwitterMessageConsumer.class); public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "/rss-inbound.xml"); // create a pollable channel PollableChannel feedChannel = context.getBean("feedChannel", PollableChannel.class); for (int i = 0; i < 10; i++) { // receive the message feed Message<SyndEntry> message = (Message<SyndEntry>) feedChannel.receive(1000); if (message != null) { SyndEntry entry = message.getPayload(); // display LOG.info(entry.getPublishedDate() + " - " + entry.getTitle()); } else { break; } } } }
Now, perhaps with little more work you can start building your own RSS reader!
Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
AI and Cybersecurity Protecting Against Emerging Threats
-
Why I Prefer Trunk-Based Development
-
How To Integrate Microsoft Team With Cypress Cloud
-
CPU vs. GPU Intensive Applications
Comments