Grab Headlines From A Remote RSS File
By Nicholas Chase2003-12-19
Caching The Feed
This system would work fine on a personal server where you're the only one accessing it, but in the real world, it would be impractical (and rude) to pull the feed every time someone wants to read it. Instead, you need to build the system with some sort of time delay, so if the feed's been pulled recently, the existing headlines.html file is used.
To do that, you can take advantage of a Java application's nature. A static variable that represents the last time the feed was pulled would be constant for all instances of the RSSProcessor class, so you can check the current time against it before actually pulling the feed:
Listing 10. Choosing a stylesheet
|
The first time the server instantiates RSSProcessor, _LastUpdated gets initialized with the current date. At (essentially) the same time, the server executes the setRSSFile() method, and because the difference between the current time and the _LastUpdated time is zero, the transformation takes place.
The next time someone calls the page, a new instance of RSSProcessor is created, but because _LastUpdated is static, the new instance sees the existing value of _LastUpdated rather than initializing it. The interval is measured in minutes, with the difference between _LastUpdated and the current time measured in milliseconds. If the amount time that has elapsed is less than the interval, nothing else happens. The headlines.html file isn't updated, so the server uses the old one instead.
If, on the other hand, the interval has passed, _LastUpdated gets the current time, which is passed on to any subsequent RSSProcessor objects, and the bean pulls a new copy of the feed to transform.
Tutorial Pages:
» Retrieve Syndicated Content, Transform It, & Display The Result
» The Source File
» The Primary Stylesheet
» The Basic JSP Page
» Transforming The File
» Adjusting For Multiple Formats
» Choosing A Version
» Caching The Feed
» Conclusion
» Resources
First published by IBM developerWorks
