It's not a secret anymore!
The moose likes Servlets and the fly likes automatic running servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "automatic running servlet" Watch "automatic running servlet" New topic

automatic running servlet

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
How can I automatically invoke a servlet periodically at specified time intervals, like cron?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Manish
As far as I can think, following could be possible solutions
1. write a timer (using Timer and TimerTask classes) where you put URLConnection code to your servlet in the TimerTask so whenever the task is scheduled it makes URLconnection...that way you can invoke the servlet
- you would have to invoke this timer somehow first time though...
Possible PROBLEM:
- if the webserver restarts (jvm dies for sometime) then you would loose this timer so this solution doesn't provide you persistent store you know
To solve the above problem,
- you can make the scheduling information about the servlet persistent
- you can write a servlet that on webserver start gets initiated (this setting would be specific to each webserver configuration things)
- make that servlet load the persistent data and start timer object accordingly...
does this help?
Bart Fransen

Joined: Feb 12, 2004
Posts: 4
Hi Manish,
I don't know which app server your running on, but if you're using Resin like me, there is an option in the configuration file to run servlets automatically at certain times. Check out ...
<servlet servlet-name='alarm' servlet-class=''>
<run-at period='1m'/>
This will run the Alarm servlet service() method every minute.
I don't know if any of the other app servers out there have the same functionality, but check out the docs ...
Hope that helps,
I agree. Here's the link:
subject: automatic running servlet
It's not a secret anymore!