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.scheduling</groupId>
    <artifactId>helidon-scheduling</artifactId>
</dependency>
Copied

Usage

For scheduling periodic tasks, it is possible to choose a fixed rate or a Cron expression.

Fixed rate

Scheduling with fixed rate use Scheduling.fixedRate() builder.
Scheduling.fixedRate()
        .delay(10)
        .initialDelay(5)
        .timeUnit(TimeUnit.MINUTES)
        .task(inv -> System.out.println("Every 10 minutes, first invocation 5 minutes after start"))
        .build();
Copied

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

Invocation metadata
Scheduling.fixedRate()
        .delay(10)
        .task(inv -> System.out.println("Method invoked " + inv.description()))
        .build();
Copied

Type: io.helidon.scheduling.FixedRate

Configuration options

Required configuration options
keytypedefault valuedescription
delay

long

 

Fixed rate delay between each invocation. Time unit is by default java.util.concurrent.TimeUnit#SECONDS, can be specified with io.helidon.scheduling.Scheduling.FixedRateBuilder#timeUnit(java.util.concurrent.TimeUnit).

@return delay between each invocation
Optional configuration options
keytypedefault valuedescription
delay-type

DelayType (SINCE_PREVIOUS_START, SINCE_PREVIOUS_END)

@io.helidon.scheduling.FixedRate.DelayType@.SINCE_PREVIOUS_START

Configure whether the delay between the invocations should be calculated from the time when previous task started or ended. Delay type is by default FixedRate.DelayType#SINCE_PREVIOUS_START.

@return delay type
initial-delay

long

0

Initial delay of the first invocation. Time unit is by default java.util.concurrent.TimeUnit#SECONDS, can be specified with io.helidon.scheduling.Scheduling.FixedRateBuilder#timeUnit(java.util.concurrent.TimeUnit) timeUnit().

@return initial delay value
time-unit

TimeUnit (NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS)

TimeUnit.SECONDS

java.util.concurrent.TimeUnit TimeUnit used for interpretation of values provided with io.helidon.scheduling.Scheduling.FixedRateBuilder#delay(long) and io.helidon.scheduling.Scheduling.FixedRateBuilder#initialDelay(long).

@return time unit for interpreting values
        in io.helidon.scheduling.Scheduling.FixedRateBuilder#delay(long)
        and io.helidon.scheduling.Scheduling.FixedRateBuilder#initialDelay(long)

Cron

For more complicated interval definition, Cron expression can be leveraged with Scheduling.cron() builder.

Scheduling with Cron expression
Scheduling.cron()
        .expression("0 15 8 ? * *")
        .task(inv -> System.out.println("Executer every day at 8:15"))
        .build();
Copied

Type: io.helidon.scheduling.Cron

Configuration options

Required configuration options
keytypedefault valuedescription
expression

string

 

Cron expression for specifying period of execution.

<b>Examples:</b>
  • 0/2 * * * * ? * - Every 2 seconds

  • 0 45 9 ? * * - Every day at 9:45

  • 0 15 8 ? * MON-FRI - Every workday at 8:15

@return cron expression

Optional configuration options
keytypedefault valuedescription
concurrent

boolean

true

Allow concurrent execution if previous task didn’t finish before next execution. Default value is true.

@return true for allow concurrent execution.

Cron expression syntax

Cron expressions should be configured as follows.

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 provided as task parameter.

Configuration

Scheduling is configurable with Helidon Config.

Example of configuring
Scheduling.fixedRate()
        .config(Config.create(() -> ConfigSources.create(
                """
                        delay: 4
                        delay-type: SINCE_PREVIOUS_END
                        initial-delay: 1
                        time-unit: SECONDS
                        """,
                MediaTypes.APPLICATION_X_YAML)))
        .task(inv -> System.out.println("Every 4 minutes, first invocation 1 minutes after start"))
        .build();
Copied

Examples

Fixed rate

For simple fixed rate invocation use .

Example of scheduling with fixed rate use Scheduling.fixedRate() builder.
Scheduling.fixedRate()
        .delay(10)
        .initialDelay(5)
        .timeUnit(TimeUnit.MINUTES)
        .task(inv -> System.out.println("Every 10 minutes, first invocation 5 minutes after start"))
        .build();
Copied

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

Example with invocation metadata
Scheduling.fixedRate()
        .delay(10)
        .task(inv -> System.out.println("Method invoked " + inv.description()))
        .build();
Copied

Reference