Scheduling a local job

If your application have to schedule a job in a scheduled way (ie: every 5 seconds), not dependending when the application has started, you can schedule job by using delay. In other hand, if you want a precise scheduling not depending on when the application has started, you can use a CRON expression.


Installation

To enable this module, just add following lines to the pom.xml file of your project.

<dependency>
    <groupId>dev.voidframework</groupId>
    <artifactId>voidframework-scheduler</artifactId>
    <version>1.16.0</version>
</dependency>

Define a Job

To define a job, simply use the @Scheduled annotation on one or more methods of a bindable class. As a reminder, a class is said to be bindable when it is annotated with, at least, the @Bindable annotation.

The @Scheduled annotation accepts the following parameters:

  • fixedDelay the time in milliseconds between the end of the last execution and the next execution. The default value is 0 (disabled).
  • fixedRate the time in milliseconds between each execution. The default value is 0 (disabled).
  • initialDelay the time in milliseconds to wait before the first execution of “fixedDelay” or “fixedRate”. The default value is 0 (disabled).
  • cron a CRON-like expression. The default value is empty (disabled).
  • cronZone the time zone for which the CRON expression will be resolved. The default value is UTC.

CRON expression

The format of the CRON expression have to be composed of : second, minute, hour, day of month, month and day of week.

UnitValueStep ValueDetails
Second0 – 591 – 60
Minute0 – 591 – 60
Hour0 – 231 – 24
Day of Month1 – 311 – 32
Month1 – 121 – 131: January, 2: February, …, 12: December
Day of Week0 – 61 – 70: Sunday, 1: Monday, …, 6: Saturday

To improve readability, you can also replace Month and Day of Week with an abbreviation of the desired value.

  • Month: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
  • Day of Week: SUN, MON, TUE, WEB, THU, FRI, SAT

Examples

@BindClass
public final class Ping {

    private static final Logger LOGGER = LoggerFactory.getLogger(Ping.class);

    @Scheduled(fixedDelay = 5000, initialDelay = 10000)
    public void doPingEveryFiveSecondsAfter10Seconds() {
        LOGGER.info("PING!!");
    }

    @Scheduled(cron = "0 */5 * * * *")
    public void doPingEveryFiveMinutesUTC() {
        LOGGER.info("PING!!");
    }

    @Scheduled(cron = "0 0 0 * * mon", cronZone = "Europe/Paris")
    public void doPingEveryMondayAtMidnightEuropeParis() {
        LOGGER.info("PING!!");
    }

    @Scheduled(initialDelay = 1000)
    public void doPingOnceAfter1Second() {
        LOGGER.info("PING!!");
    }
}