• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting a timer task even if the server restarts

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic