Cron-Based Scheduling

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Overview

Instances of Runnable and Callable can be scheduled for periodic execution on local nodes using IgniteScheduler.scheduleLocal() methods and Cron syntax.

📘

You will need to include the ignite-schedule dependency to use the scheduling methods/examples below.

The example below triggers periodic cache metrics reporting on all available nodes:

ignite.compute().broadcast(new IgniteCallable<Object>() {
    @IgniteInstanceResource
    Ignite ignite;

    @Override
    public Object call() throws Exception {
        ignite.scheduler().scheduleLocal(new Runnable() {
            @Override public void run() {
                sendReportWithCacheMetrics(cache.metrics());
            }
        }, "0 0   *");
        return null;
    }
});

🚧

Cron shorthands

In the current implementation, Cron shorthands (@hourly, @daily, @weekly...) are not supported, and the minimal scheduling time unit is 1 minute.

SchedulerFuture

The IgniteScheduler.scheduleLocal() methods return SchedulerFuture, which has a bunch of useful methods for monitoring scheduled execution and getting results. To cancel periodic execution, you also use the future.

SchedulerFuture<?> fut = ignite.scheduler().scheduleLocal(new Runnable() {
    @Override public void run() {
        ...
    }
}, "0 0 * * *");

System.out.println("The task will be next executed on " + new Date(fut.nextExecutionTime()));

fut.get(); // Wait for next execution to finish.

fut.cancel(); // Cancel periodic execution.

Syntax Extension

Ignite introduces an extension to Cron syntax, which you can use to specify an initial delay in seconds and a number of runs. These two optional numbers go in curly braces, comma-separated, before the Cron specification. The example below specifies execution 5 times each minute with an initial 2 second delay.

{2, 5} * * * * *