Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting a timer task even if the server restarts  RSS feed

 
Rob Hyx
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I *think* this is the right forum since it pertains to a servlet container. I have a web app that accepts requests. Once a request is sent, I want to create a thread that will sleep for several days if no user action is taken on the request to remind the user to do something. The problem is, how do I ensure the timer will pick up at the same place if the server is restarted? Would serializing the thread do that? I'm guessing no because I think you're just creating a new instance at start up. Just checking If there's anything built into Java to do this.

Thanks in advance!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not use a timer to do this. I'd build it the capability into the database that will maintain state regardless of what happens to the server. The requests can check to see if any reminders are due and take appropriate action if so.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads would not be part of the solution. I think the Quartz scheduling library persists its tasks to the DB.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quartz has configurable persistence options. A database is just one of them.

I do feel obliged to mention, since there's always some people who aren't aware, that threads and timer services cannot be spawned or executed from the servlet request methods or any method that those methods may invoke. Servlets are not permitted to place themselves in a "wait" state. And, of course, servlets cannot wake up and send unsolicited traffic to clients, since HTTP itself doesn't permit that.

Incidentally, the Linux operating system has a facility called "anacron" which was designed to deal with that sort of problem for OS-level timer tasks. Actually, it was designed with laptops and other machines which might be powered up and down a lot an thus not guaranteed to to running when the event fired.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!