Contents

Overview

Scheduling is an essential feature for the Enterprise. Helidon has its own implementation of Scheduling functionality based on Cron-utils.

Maven Coordinates

To enable Scheduling, add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
    <groupId>io.helidon.microprofile.scheduling</groupId>
    <artifactId>helidon-microprofile-scheduling</artifactId>
</dependency>
Copied

Usage

For scheduling tasks in Helidon you can choose from @Scheduling.Cron or @Scheduling.FixedRate annotations by required complexity of invocation interval. All you need is to define a method with one of the annotations in an application scoped bean.

Fixed rate

For simple fixed rate invocation interval, the @Scheduling.FixedRate is the easiest way to schedule task invocation.

@Scheduling.FixedRate(delayBy = "PT5M", value = "PT10M")
Copied

Metadata like human-readable interval description or configured values are available through FixedRateInvocation injected as method parameter.

Cron expression

For more complicated interval definition, cron expression can be leveraged with @Scheduling.Cron annotation.

@Scheduling.Cron(value = "0 15 8 ? * *", concurrent = false)
public void methodName() { /* ... */ }
Copied

Cron expression

Cron expression format
<seconds> <minutes> <hours> <day-of-month> <month> <day-of-week> <year>
Copied
Cron expression fields
OrderNameSupported valuesSupported field formatOptional
1seconds0-59CONST, LIST, RANGE, WILDCARD, INCREMENTfalse
2minutes0-59CONST, LIST, RANGE, WILDCARD, INCREMENTfalse
3hours0-23CONST, LIST, RANGE, WILDCARD, INCREMENTfalse
4day-of-month1-31CONST, LIST, RANGE, WILDCARD, INCREMENT, ANY, LAST, WEEKDAYfalse
5month1-12 or JAN-DECCONST, LIST, RANGE, WILDCARD, INCREMENTfalse
6day-of-week1-7 or SUN-SATCONST, LIST, RANGE, WILDCARD, INCREMENT, ANY, NTH, LASTfalse
7year1970-2099CONST, LIST, RANGE, WILDCARD, INCREMENTtrue
Field formats
NameRegex formatExampleDescription
CONST\d+12exact value
LIST\d+,\d+(,\d+)*1,2,3,4list of constants
RANGE\d+-\d+15-30range of values from-to
WILDCARD\**all values withing the field
INCREMENT\d+\/\d+0/5initial number / increments, 2/5 means 2,7,9,11,16,…​
ANY\??any day(apply only to day-of-week and day-of-month)
NTH\#1#3nth day of the month, 2#3 means third monday of the month
LAST\d*L(+\d+|\-\d+)?3L-3last day of the month in day-of-month or last nth day in the day-of-week
WEEKDAY\#1#3nearest weekday of the nth day of month, 1W is the first monday of the week
Examples
Cron expressionDescription
* * * * * ?Every second
0/2 * * * * ? *Every 2 seconds
0 45 9 ? * *Every day at 9:45
0 15 8 ? * MON-FRIEvery workday at 8:15

Metadata like human-readable interval description or configured values are available through CronInvocation injected as method parameter.

@Scheduling.Cron("0 15 8 ? * *")
public void methodName(CronInvocation inv) {
    { /* ... */ }
}
Copied

Configuration

Scheduling annotation properties can be overridden using application.yaml properties, if configured in the source code.

The following annotation options can use configuration "expression":

  • Scheduling.Fixed#delayBy()

  • Scheduling.FixedRate#value()

  • Scheduling.Cron#value()

Configuration expressions is a reference to a configuration key, with optional default value:

${config.key:default-value}

Fixed Rate

The Fixed rate annotation can have the delay by and value overridden using config.

Annotation that allows config overrides
@Scheduling.FixedRate(delayBy = "${app.schedule.cache.delay-by:PT5M}", value = "${app.schedule.cache.interval:PT10M}")
Copied

The default values are 5 minutes for delay-by, and 10 minutes for interval, and could be overridden:

Overriding annotated values from config
app:
  schedule:
    cache:
      delay-by: "PT10M"
      interval: "PT1H"
Copied

The configured values would be a 10-minute delay, with 1-hour interval.

Cron

The Cron annotation can have the value overridden using config.

Annotation that allows config overrides
@Scheduling.Cron("${app.schedule.cache.cron:0 15 8 ? * *}")
Copied

The default value is an expression of 0 15 8 ? * *.

Overriding annotated values from config
app:
  schedule:
    cache:
      cron: "* * * * * ?"
Copied

The configured values would be executing every 1 second.

Examples

Fixed rate

Example of scheduling with fixed rate
@Scheduling.FixedRate(delayBy = "PT5M", value = "PT10M")
public void methodName() {
    System.out.println("Every 10 minutes, first invocation 5 minutes after start");
}
Copied

FixedRate Metadata Injection

Example with invocation metadata
@Scheduling.FixedRate(delayBy = "PT5M", value = "PT10M")
public void methodName(FixedRateInvocation inv) {
    System.out.println("Method invoked " + inv.description());
}
Copied

Cron expression

Example of scheduling with cron expression
@Scheduling.Cron(value = "0 15 8 ? * *", concurrent = false)
public void methodName() {
    System.out.println("Executer every day at 8:15");
}
Copied

Scheduled Metadata Injection.

Example with invocation metadata
@Scheduling.Cron("0 15 8 ? * *")
public void methodName(CronInvocation inv) {
    System.out.println("Method invoked " + inv.description());
}
Copied

Reference