Search code examples
javatimerfuturetask

ScheduledExecutorService.scheduleAtFixedRate And Setting initialDelay To Date In The Past


I'm working on a scheduling system in Java that sends out reminders based on a startDate, endDate and occurrence (hourly, daily, weekly, monthly, Mondays, etc). Originally I was using Timer and TimerTask classes to schedule the reminders:

Timer timer = new Timer();
timer.scheduleAtFixedRate(reminder, firstDate, period);

I recently switched to ScheduledExecutorService so I could have more control on cancelling events. The ScheduledExecutorService is working well for recurring reminders, except for the one case of rescheduling a reminder with a startDate in the past. The scheduleAtFixedRate function only allows you to specify long value for initialDelay, and not an actual Date object:

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(reminder, initialDelay, period, unit);

This poses a problem, since passing in a negative initialDelay still causes the event to be fired immediately thus causing it to reoccur at now + period, rather than startDate + period.

Any ideas how I can (re)schedule a reminder with the startDate in the past?


Solution

  • Just do a quick check to see if the date is in the past, then create a new temporary start datetime that is the increment of the start past now.