3

I am working on an implementation for RSS feeds for a collaboration platform. Say there are several thousands of different collaboration rooms where users can share information, and each needs to publish an RSS feed with news, changes, etc...

Using a plain servlet (i.e. http://www.site.com/RSSServlet/?id=roomID) is costly, every time an RSS client is calling the servlet (and this will happen say every 10 minutes for each user registered to an RSS feed on one of the thousand of rooms) this will trigger the entire servlet lifecycle, which is costly.

On the other hand, keeping a static XML file on the disk for each of the thousands of rooms is costly as well, in terms of hard disk space as well as IO operations...

One more limitation - using already existing frameworks might not be an option...

So, how would you implement RSS feeds in a Java envoronment?

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • Why do you say that every request for RSS will trigger the entire servlet lifecycle. Usually, the servlet will be initialized once, and will then be left alive until the container terminates. The only thing a request would then trigger is a method of the servlet to process the request. – Alexander Oct 13 '08 at 10:58

2 Answers2

5

You say that a new http request to your servlet "will trigger the entire servlet lifecycle", which as Alexander has already pointed out, isn't exactly true. It will simply trigger another method call to your doGet() or doPost() methods.

I think what you mean to say is that if you have a doGet/doPost method which contains code to build the data needed for the RSS feed from scratch, then each request triggers this fetching of data over and over again.

If this is your concern, and you are ruling static content out, simply modify your Servlet doGet/doPost method to cache the RSS content that you would otherwise return, so that handling each request does not mean re-fetching all of the data all over again.

For example

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    //build the objects you need for the RSS response
    Room room = getRoom(request.getParameter("roomid"));
    //loadData();
    //moreMethodCalls();
    out.println( createRssContent(...) );
}

becomes

Map rssCache;

public void doGet(HttpServletRequest request, HttpServletResponse response) {

    //Map is initialized in the init() method or somewhere else    
    String roomId = request.getParameter("roomid");

    String rssDocument = rssCache.get(roomId);
    if (rssDocument == null) {

        //build the objects you need for the RSS response
        Room room = getRoom(roomId);
        //loadData();
        //moreMethodCalls();
        rssDocument = createRssContent(...);
        rssCache.put(roomId, rssDocument);
    }
    out.println( rssDocument );
}

If you only want to store items in a "cache" for a certain amount of time you can use one of a dozen different caching frameworks, but the idea here is that you don't reconstruct the entire object graph necessary for your RSS response with each http request. If I am reading your original question right then I think that this is what you hoping to accomplish.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Matt thank you for your thorough answer. Taking into consideration that the RSS responses will certainly be cached, the question is regarding the performance of basing the entire RSS mechanism on a servlet. In your opinion, will this single servlet support the numbers I talked about? – Yuval Adam Oct 15 '08 at 14:24
  • @Yuval: why do you think invoking a servlet method is necessarily slower than returning a file from disk? In fact, Tomcat and others also use a servlet (the DefaultServlet) to serve static files. I wrote such a servlet here: http://stackoverflow.com/questions/132052/ – Bruno De Fraine Oct 15 '08 at 14:35
3

You should try the ROME framework. It is excellent for RSS.

Shimi Bandiel
  • 5,773
  • 3
  • 40
  • 49
  • like i added to the question, external frameworks are out of the question... do you know how they implement this? – Yuval Adam Oct 12 '08 at 15:40