简体   繁体   中英

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?

只需快速检查一下日期是否是过去的日期,然后创建一个新的临时开始日期时间即可,即现在开始的时间增量。

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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM