Friday, June 27, 2008

Scheduler for web applications

What if your web application requires some periodical process? It is good when your hosting provides some kind of cron tab scheduler, lets you invoke your own application, but to be honest it is not widely used option.
Here we are describing one trick lets you deploy the scheduler (well – pseudo scheduler) right in your web application.

The idea is very simply. You can "borrow" requests from the visitors of your site. Of course, it assumes that your site consistently get visitors at a frequency greater than your scheduler’s interval. You can add to your site a hidden (invisible) iframe where src attribute points to the "scheduler". And your "scheduler" will perform the requests to your periodical tasks (implemented as JSP pages or servlets). Your periodical tasks could be long running of course, so the direct request may delays site response to the visitors. And the more practical approach is doing this processing not as part of the main request from your visitor but rather by putting in the response HTML some JavaScript that will asynchronously send you HTTP requests in the background (typically not visible to the user). Of course it only works if your application has visitors who use web browsers, not if it only consumed by programs (e.g. through RSS feeds or other XML format).

So you can add to your site some hidden frame:

<iframe style="display:none" src="my_scheduler.jsp"></iframe>

my_scheduler.jsp implements your pseudo scheduler. For the above mentioned approach (return the code for Ajax request to the real task) you can deploy Fork taglib from Coldtags suite. So your pseudo scheduler can simply deploy this taglib:

<%@ taglib uri="taglib.tld" prefix="f" %>

<f:Fork url="my_periodical_task.jsp"/>

where my_periodical.jsp task is your real periodical task. Taglib simply returns JavaScript code that performs asynchronous request to the real task with approximately one second (it is configurable of course) delay

No comments: