4

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?

Dale Zak
  • 1,106
  • 13
  • 22

2 Answers2

2

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
  • 24,947
  • 11
  • 38
  • 68
  • 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 Jul 07 '10 at 23:26
0

I solved it by running it once on startup and then at the time I wanted every day:

// check once initial on startup
doSomething();

// and then once every day at midnight utc
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
LocalDateTime firstRun = LocalDate.now(ZoneOffset.UTC).atStartOfDay().plusDays(1);
Duration durationUntilStart = Duration.between(LocalDateTime.now(ZoneOffset.UTC), firstRun);
scheduler.scheduleAtFixedRate(
        () -> doSomething(),
        durationUntilStart.getSeconds() + 1,
        Duration.ofDays(1).getSeconds(),
        TimeUnit.SECONDS
);
Tigerware
  • 3,196
  • 2
  • 23
  • 39