Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timing events in a servlet

 
Thomas Goorden
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Siggelkow
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Thomas Goorden
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
T.
 
Simon Willison
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic