An alternative to ScheduledThreadPoolExecutor proof against the clock drift problem
CronScheduler
is an alternative to ScheduledThreadPoolExecutor
and Timer
with the following
advantages:
See this blog post
for more details and specific recommendations about when to use ScheduledThreadPoolExecutor
,
CronScheduler
, or other scheduling facilities.
Maven:
<dependency>
<groupId>io.timeandspace</groupId>
<artifactId>cron-scheduler</artifactId>
<version>0.1</version>
</dependency>
Gradle:
dependencies {
compile 'io.timeandspace:cron-scheduler:0.1'
}
Server-side usage example:
Duration syncPeriod = Duration.ofMinutes(1);
CronScheduler cron = CronScheduler.create(syncPeriod);
cron.scheduleAtFixedRateSkippingToLatest(0, 1, TimeUnit.MINUTES, runTimeMillis -> {
// Collect and send summary metrics to a remote monitoring system
});
Client-side usage example:
Duration oneHour = Duration.ofHours(1);
CronScheduler cron = CronScheduler.create(oneHour);
cron.scheduleAtRoundTimesInDaySkippingToLatest(oneHour, ZoneId.systemDefault(), runTimeMillis -> {
notifyUser("It's time to get up and make a short break from work!");
});
For drop-in replacement of ScheduledThreadPoolExecutor
and integration with existing code, there
is an adapter: cronScheduler.asScheduledExecutorService()
.
See Javadocs.