Feed my inbox; reading RSS feeds with Mule ESB

January 26 2011

0 comments
motif

I read a couple of RSS feeds regularly. Unfortunately, I work across a couple of machines: my laptop, the machine in the office, my wife’s laptop. This rules out using a local RSS reader as I’d have to manage the redundant subscriptions let alone I’d have to remember which feed entries I have already read and which one not. Sure, I could use Google’s feed reader but being mostly eMail centric anyway I’d love to see RSS feed entries in my INBOX. But wait! Mule has RSS support and it can send emails … why not hack this together in Mule?

Polling the feed

Retrieving an RSS feed is easy using the tag that was introduced with Mule 3:

This config snippet polls the feed URL every 10 seconds.

Parsing the feed

The RSS module handles all the parsing and splitting. All we need is a feed splitter:

<:feed-splitter/>

This will parse the payload of the request and split the RSS feed into individual SyndEntry instances. Each SyndEntry will be sent as a separate MuleMessage.

Preparing the message

The SMTP transport expects the sender, recipient and subject as properties of the MuleMessage that’s being sent to the endpoint. Let’s add a message properties transformer to enrich the message:

The fromAddress and toAddresses are hardcoded for now. The subject is extracted from the message’s payload using the Groovy expression evaluator. Remember that the payload is actually a SyndEntry object.

Before we can finally send out an eMail we must give it a body. The transport uses the payload of the MuleMessage it receives as the mail body. It would have a hard time converting the SyndEntry into a meaningful text – that’s our job. Again, Mule’s expression language comes in handy to transform the payload:

This extracts the description of the SyndEntry which is actually a SyndContent instance. We want the value of that content object as payload of the MuleMessge.

Sending the message

Now that the MuleMessage is in shape we can actually route it to an SMTP outbound endpoint:

Note that the SMTP transport expects a MimeMessage as the payload of the MuleMessage, hence the string-to-email-transformer.

Hooray!

That’s it. We have implemented a simple bridge from RSS to eMail in Mule without actually writing code.

I have put a working project up on github so you can download and try it out locally.

You’ll notice that running this service can pretty quick become a spambot. While the RSS feed splitter makes sure it sends out each feed entry only once it does not persist that information. So when you restart the rss2mail application you’ll get eMail for feed entries you most probably have already read. I’ll leave the fix for this issue for the next blog post, though.


We'd love to hear your opinion on this post