Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Calling a thread daily in the server automatically  RSS feed

Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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.
Posts: 42974
  • 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 ]
(instanceof Sidekick)
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.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!