The ScheduledExecutorService in Java is pretty handy for repeating tasks with either fixed intervals or fixed delay. I was wondering if there is an something like the existing ScheduledExecutorService that lets you specify a time of day to schedule the task at, rather than an interval i.e. "I want this task to fire at 10am each day".
I know you can achieve this with Quartz, but I'd rather not use that library if possible (it's a great library but I'd rather not have the dependency for a few reasons).
-
You can use the Timer class. Specifically, scheduleAtFixedRate(TimerTask task, Date firstTime, long period). Where you can set a task to start at 10am on a particular day and repeat every 24 hours.
GaryF : The big issue with this particular method is that it doesn't take daylight savings changes into account.GaryF : I picked this answer as it most accurately answered my question, but I went with the solution in my own answer. -
When you use scheduleAtFixedRate you provide a delay. So the delay can be the difference to 10 am and period is 24 hours. This could drift a bit, even with a timer so what you can do is schedule a task which adds itself to the ScheduledExecutorService with an appropriate delay each time.
-
JT Cron
-
A bit more searching has turned up CronExecutorService in HA-JDBC. Interestingly, it has a dependency on Quartz for its CronExpression class, but that's it. That's not too bad.
0 comments:
Post a Comment