|
| 1 | +# CronScheduler: a reliable Java scheduler for external interactions |
| 2 | + |
| 3 | +[]( |
| 4 | +https://maven-badges.herokuapp.com/maven-central/io.timeandspace/cron-scheduler) |
| 5 | +[]( |
| 6 | +https://travis-ci.org/TimeAndSpaceIO/CronScheduler) |
| 7 | + |
| 8 | +`CronScheduler` is an alternative to `ScheduledThreadPoolExecutor` and `Timer` with the following |
| 9 | +advantages: |
| 10 | + |
| 11 | + - CronScheduler is proof against unbounded [clock drift](https://en.wikipedia.org/wiki/Clock_drift) |
| 12 | + relative to UTC or system time for both one-shot or periodic tasks. |
| 13 | + - It [takes into account machine suspension](https://bugs.openjdk.java.net/browse/JDK-8146527) |
| 14 | + (like sleep or hibernation). |
| 15 | + - It has convenient methods to prevent several tasks from piling up in case of blocks, long GC |
| 16 | + pauses, or abrupt forward system time shifts (likely made by the user or the administrator of the |
| 17 | + machine). |
| 18 | + - It correctly handles significant system time setbacks (backward shifts), likely made by the user |
| 19 | + or the administrator of the machine. |
| 20 | + - It has convenient methods to schedule tasks at round times within a day in a given time zone |
| 21 | + (for example, every midnight), transparently handling all time zone and [daylight saving time]( |
| 22 | + https://en.wikipedia.org/wiki/Daylight_saving_time) complexity. |
| 23 | + |
| 24 | +See [this blog post]( |
| 25 | +https://medium.com/@leventov/cronscheduler-a-reliable-java-scheduler-for-external-interactions-cb7ce4a4f2cd) |
| 26 | +for more details and specific recommendations about when to use `ScheduledThreadPoolExecutor`, |
| 27 | +`CronScheduler`, or other scheduling facilities. |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Maven: |
| 32 | +```xml |
| 33 | +<dependency> |
| 34 | + <groupId>io.timeandspace</groupId> |
| 35 | + <artifactId>cron-scheduler</artifactId> |
| 36 | + <version>0.1</version> |
| 37 | +</dependency> |
| 38 | +``` |
| 39 | + |
| 40 | +Gradle: |
| 41 | +```xml |
| 42 | +dependencies { |
| 43 | + compile 'io.timeandspace:cron-scheduler:0.1' |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Server-side usage example: |
| 48 | +```java |
| 49 | +Duration syncPeriod = Duration.ofMinutes(1); |
| 50 | +CronScheduler cron = CronScheduler.create(syncPeriod); |
| 51 | +cron.scheduleAtFixedRateSkippingToLatest(0, 1, TimeUnit.MINUTES, runTimeMillis -> { |
| 52 | + // Collect and send summary metrics to a remote monitoring system |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +Client-side usage example: |
| 57 | +```java |
| 58 | +Duration oneHour = Duration.ofHours(1); |
| 59 | +CronScheduler cron = CronScheduler.create(oneHour); |
| 60 | +cron.scheduleAtRoundTimesInDaySkippingToLatest(oneHour, ZoneId.systemDefault(), runTimeMillis -> { |
| 61 | + notifyUser("It's time to get up and make a short break from work!"); |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +See [**Javadocs**]( |
| 66 | +https://javadoc.io/doc/io.timeandspace/cron-scheduler/latest/io/timeandspace/cronscheduler/CronScheduler.html). |
0 commit comments