CronScheduler

Alternative to ScheduledThreadPoolExecutor proof against the clock drift problem

License

License

GroupId

GroupId

io.timeandspace
ArtifactId

ArtifactId

cron-scheduler
Last Version

Last Version

0.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

CronScheduler
Alternative to ScheduledThreadPoolExecutor proof against the clock drift problem
Project URL

Project URL

https://github.com/TimeAndSpaceIO/CronScheduler
Source Code Management

Source Code Management

https://github.com/TimeAndSpaceIO/CronScheduler

Download cron-scheduler

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

CronScheduler: a reliable Java scheduler for external interactions

Maven Central Build Status

CronScheduler is an alternative to ScheduledThreadPoolExecutor and Timer with the following advantages:

  • CronScheduler is proof against unbounded clock drift relative to UTC or system time for both one-shot or periodic tasks.
  • It takes into account machine suspension (like sleep or hibernation).
  • It has convenient methods to prevent several tasks from piling up in case of blocks, long GC pauses, or abrupt forward system time shifts (likely made by the user or the administrator of the machine).
  • It correctly handles significant system time setbacks (backward shifts), likely made by the user or the administrator of the machine.
  • It has convenient methods to schedule tasks at round times within a day in a given time zone (for example, every midnight), transparently handling all time zone and daylight saving time complexity.

See this blog post for more details and specific recommendations about when to use ScheduledThreadPoolExecutor, CronScheduler, or other scheduling facilities.

Usage

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.

io.timeandspace

Versions

Version
0.1