Using Rome to Generate RSS Feeds

I wanted to add RSS feed support to my project and ended up choosing Rome. I did this a while back so I honestly don’t remember the other toolkits I looked at. I do remember posting something and getting feedback pretty quickly from one of the Rome authors so I figured that the project was still pretty active.

It was pretty easy to generate a feed with the toolkit. I know that some people probably use it in more advanced ways or have more complicated needs but I just needed to be able to pass something a list of objects and have it spit out a feed in a particular format. Nothing too fancy.

Here’s a quick run down on how simple it is to create a feed:

 SyndFeed feed = new SyndFeedImpl();
 feed.setFeedType(format);
 feed.setTitle(title);
 feed.setLink(link);
 feed.setDescription(description);
 
 List entries = new ArrayList();
 for(Object item : listOfItems) {
     entries.add(entryAdapter.createSyndEntry(item));
 }

 feed.setEntries(entries);

In the code above the items such as format, title, link and so on are passed in to this function. The entryAdapter is also provided and acts as a translator to move data from a domain object(s) to a syndication entry (SyndEntry). There is generally one entry adapter implementation for each type of object I make availabe in an RSS feed.

The next step is to send the feed to an output target. In most cases you would just set this to be the servlet’s output stream. Here’s the code for that. Assume the Output is also provided as the writer instance.

 SyndFeedOutput output = new SyndFeedOutput();
 try {
     output.output(feed, writer);
 } catch (FeedException e) {
     throw new RuntimeException("Failed to write output", e);
 }

Rome supports multiple formats including all RSS versions and ATOM 0.3. I’m not sure if I’ll need more of the framework but they made it pretty easy to export feeds so it passed my first test. The next step is possibly caching feeds for popular requests so that I don’t have keep retrieving the necessary data and generating the feed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s