Apache Camel – Polling RSS Feed

Maven Dependencies

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-core</artifactId>
	<version>${camel-version}</version>
</dependency>

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-rss</artifactId>
	<version>${camel-rss-version}</version>
</dependency>

Example Program

package com.dineshkrish.example7;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class PollRSSFeed {

	public static void main(String[] args) throws Exception {

		// creating camel context
		CamelContext context = new DefaultCamelContext();

		try {

			context.addRoutes(new RouteBuilder() {

				@Override
				public void configure() throws Exception {

					from("rss:http://www.feedforall.com/sample.xml?alt=rss&splitEntries=false&consumer.delay=1000")
							.to("seda:end");
				}
			});

			// creating a consumer template
			ConsumerTemplate template = context.createConsumerTemplate();

			while (true) {
				context.start();

				// receiving the RSS feed
				String rssFeed = template.receiveBody("seda:end", String.class);
				System.out.println(rssFeed);
			}

		} catch (Exception e) {
			context.stop();
			System.out.println(e.getMessage());
		}
	}
}

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>FeedForAll Sample Feed</title>
    <link>http://www.feedforall.com/industry-solutions.htm</link>
    <description>RSS is a fascinating technology. The uses for RSS are expanding daily. Take a closer look at how various industries are using the benefits of RSS in their businesses.</description>
    <language>en-us</language>
    <copyright>Copyright 2004 NotePage, Inc.</copyright>
    <pubDate>Tue, 19 Oct 2004 17:38:55 GMT</pubDate>
    <category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
    <dc:creator>[email protected]</dc:creator>
    <dc:subject>
      <rdf:Description>
        <taxo:topic rdf:resource="www.dmoz.com" />
        <rdf:value>Computers/Software/Internet/Site Management/Content Management</rdf:value>
      </rdf:Description>
    </dc:subject>
    <dc:date>2004-10-19T17:38:55Z</dc:date>
    <dc:language>en-us</dc:language>
    <dc:rights>Copyright 2004 NotePage, Inc.</dc:rights>
 ................
.................

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.