Will a java timer code scheduled to run every 24 hours continue to run even if server stops or is restarted? I am using java.until.timer
Asked
Active
Viewed 686 times
0
-
Scheduled where ? In crontab ? If stops it won't run anyway. – PeterMmm Feb 06 '14 at 17:02
-
Maybe using quartz: http://quartz-scheduler.org/ – PeterMmm Feb 06 '14 at 17:06
1 Answers
0
No, the timer cannot remember anything that happened before the JVM started... you need to record the last-run time somewhere, and on a server restart you would have to check that last-run and schedule the next run for 24 hours after that. Probably using scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
... though I'd urge you to look into converting this code to use a ScheduledExecutorService.

dcsohl
- 7,186
- 1
- 26
- 44
-
Will ScheduledExecutorService start the job again when server is restarted? Any other way out to solve this? – user3280577 Feb 06 '14 at 19:15
-
No, `ScheduledExecutorService` will not perform any magic tricks... you have to have an external storage mechanism that the server can use on next startup to know when the next run should be. As one of the comments above suggests, that mechanism could be Quartz, or cron, or homegrown. – dcsohl Feb 06 '14 at 20:14
-
As for why I recommended `ScheduledExecutorService`, take a look at [this question](http://stackoverflow.com/questions/409932/java-timer-vs-executorservice) for more details. – dcsohl Feb 07 '14 at 16:36