views:

47

answers:

1

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?

+1  A: 

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.

Zak
Thanks for the response, looks like that will do the trick. Here's the a code snippet to calculate the initialDelay:Calendar now = Calendar.getInstance();Calendar start = reminder.getStartCalendar();while (start.before(now)) { start.add(Calendar.MILLISECOND, (int)reminder.getPeriod());}long initialDelay = start.getTimeInMillis() - now.getTimeInMillis();ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();this.scheduler.scheduleAtFixedRate(reminder, initialDelay, reminder.getPeriod(), TimeUnit.MILLISECONDS);
Dale Zak