问题
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:
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.
回答2:
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
);
来源:https://stackoverflow.com/questions/3196888/scheduledexecutorservice-scheduleatfixedrate-and-setting-initialdelay-to-date-in