DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Spring Integration Feed Adapter: How to receive RSS feeds using Spring?

Spring Integration Feed Adapter: How to receive RSS feeds using Spring?

Jagadeesh Motamarri user avatar by
Jagadeesh Motamarri
·
Mar. 14, 13 · Interview
Like (0)
Save
Tweet
Share
12.89K Views

Join the DZone community and get the full member experience.

Join For Free

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



 

Spring Integration Spring Framework Integration

Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Cloud-Based Transportation Management System
  • Apache Kafka vs. Memphis.dev
  • How To Validate Three Common Document Types in Python

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: