Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a thread daily in the server automatically  RSS feed

 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have to run a thread automatically daily which extends the present date by 30 days to all users. I am getting this and it is working fine and deployed the same on the server and I am getting the results correct but the major problem is whenever i restart my server the thread is called everytime i restart the server by which the date is extended to 30 more days again and this is happening everytime I restart my server.

Thanks in advance
regards
 
Mukesh Mittal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you can try schedular software for this.

You can use OpenSymphony Quartz scheduler. Its open source.

www.opensymphony.com/quartz/
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quartz sounds like overkill for this. Instead of scheduling the thread to execute right when you start the server, and then every 24 hours, have it execute at a fixed time, say, 3am every morning. Then it doesn't matter when and how often you restart the server. The java.util.Timer and TimerTask classes can help with this.

As a side note, have you considered controlling this job using cron, instead of executing it in the app server?
[ January 05, 2006: Message edited by: Ulf Dittmer ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that for a single event like this you can throw your own timer. Setting it is mildly tricky. If I start the server at 4:00 AM it won't know whether the 3:00 AM timer fired or maybe the server has been down since midnight. You may have to persist the "last time fired" or the "next scheduled fire" to a database or something.

CRON is certainly an option, too, if you can run the task outside the main JVM. I think CRON and Quartz have already solved the persistence problem and have policies for how to "make up" if a scheduled event is missed due to something being down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!