jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Timing events in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Timing events in a servlet" Watch "Timing events in a servlet" New topic

Timing events in a servlet

Thomas Goorden
Ranch Hand

Joined: Aug 15, 2001
Posts: 39
Suppose one wishes to let a servlet run a certain routine once every day or week, how would one do this? Currently, servlets get "unloaded" from the engine when they haven't been called for a while. Do you have to keep it up, or can you do some other nifty things?
PS: We use JRun, if that's any help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17423

Servlets only run in response to requests -- they are not "programs", so the only way you could do this is to have some process submit a periodic servlet request.
I have my doubts about this "servlet unloading" thing, since a servlet is normally expected to be able to initialize its global state at startup or first reference (depending on option) and depend on global state forever after, so the worst that should happen is serialization, and that would have to be done transparently.

An IDE is no substitute for an Intelligent Developer.
Bill Siggelkow
Ranch Hand

Joined: Jun 27, 2001
Posts: 57
I recently had to build a client-side utility that could automatically upload a file to a servlet on a daily basis without any user interaction. What I did was use the java.io classes to read the file off the client machine and the java.net.HttpUrlConnection and related classes to send the data to the servlet. Then I simply used an O/S specific scheduler to run this program on a daily basis (for NT use Task Scheduler, for Unix use kron).

Bill Siggelkow
Jade Cove Solutions
Thomas Goorden
Ranch Hand

Joined: Aug 15, 2001
Posts: 39
Would it be possible to use a Timer class object in a servlet?
Of course it's supposedly a part of the Swing package, so it seems something weird to do, but would it work?
Simon Willison

Joined: Nov 23, 2001
Posts: 1
There's a simple but effective "hack" for doing this which will work provided you have access to a UNIX server somewhere which you can set up cron jobs on. Have a daily (or weekly) cron task that does this:
lynx -dump http://yoursite.com/your/servlet > /dev/null
This will then cause the Lynx browser "access" your servlet at a set time every day, dumping the output HTML to /dev/null. It's not a very elegant solution but it works
I agree. Here's the link: http://aspose.com/file-tools
subject: Timing events in a servlet
It's not a secret anymore!