Task scheduling is the process of creating tasks to run in future time. The task can be for just one time execution or it can be repeated with a specified frequency.
Spring scheduling API is based on TaskScheduler interface.
Definition of TaskSchedulerVersion: 7.0.4 package org.springframework.scheduling;
public interface TaskScheduler {
default Clock getClock(); 1
ScheduledFuture<?> schedule(Runnable task, 2
Trigger trigger);
ScheduledFuture<?> schedule(Runnable task, 3
Instant startTime);
default ScheduledFuture<?> schedule(Runnable task, 4
Date startTime);
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, 5
Instant startTime,
Duration period);
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, 6
Date startTime,
long period);
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, 7
Duration period);
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, 8
long period);
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, 9
Instant startTime,
Duration delay);
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, 10
Date startTime,
long delay);
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, 11
Duration delay);
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, 12
long delay);
}
Above interface looks similar to JSE ScheduledExecutorService. Additionally Spring unifies JSE (JSR-166), JEE (JSR-236) and third party scheduling API into a single TaskScheduler interface.
Trigger interface determines the next execution time based on past execution outcomes or it can be based on any arbitrary conditions.
Definition of TriggerVersion: 7.0.4 package org.springframework.scheduling;
public interface Trigger {
default Date nextExecutionTime(TriggerContext triggerContext); 1
Instant nextExecution(TriggerContext triggerContext); 2
}
Definition of TriggerContextVersion: 7.0.4 package org.springframework.scheduling;
public interface TriggerContext {
default Clock getClock(); 1
default Date lastScheduledExecutionTime(); 2
Instant lastScheduledExecution(); 3
default Date lastActualExecutionTime(); 4
Instant lastActualExecution(); 5
default Date lastCompletionTime(); 6
Instant lastCompletion(); 7
}
Normally we shouldn't worry about implementing Trigger and TriggerContext interfaces, as Spring provides some implementations to fulfill our needs.
TaskScheduler implementations
SimpleAsyncTaskScheduler A simple implementation of Spring's TaskScheduler interface, using a single scheduler thread and executing every scheduled task in an individual separate thread.
ThreadPoolTaskScheduler A standard implementation of Spring's TaskScheduler interface, wrapping a native ScheduledThreadPoolExecutor and providing all applicable configuration options for it.
ConcurrentTaskScheduler Adapter that takes a java.util.concurrent.ScheduledExecutorService and exposes a Spring TaskScheduler for it. Extends ConcurrentTaskExecutor in order to implement the SchedulingTaskExecutor interface as well.
DefaultManagedTaskScheduler JNDI-based variant of ConcurrentTaskScheduler
TaskSchedulerRouter A routing implementation of the TaskScheduler interface, delegating to a target scheduler based on an identified qualifier or using a default scheduler otherwise. @since 6.1
Trigger implementation
PeriodicTrigger A trigger for periodic task execution.
CronTrigger Trigger implementation for cron expressions. Wraps a CronExpression.
In next tutorials we will see the example of each implementations.
|