Task scheduling with Spring

πŸ—“οΈ
β€’
πŸ”„
β€’
⏳ 3 min

Enable the resource

There are many ways to manage repeating tasks in Spring, but by far the easiest one is using the built-in Scheduler.

To enable it you just have to annotate the main class with @EnableScheduling.

It’s worth pointing out that the default behavior doesn’t allow for parallel execution of tasks. To do this you’ll also use @EnableAsync on the main class and @Async on the desired function.

Types of Scheduling

Spring offers three ways of managing recurrent jobs:

Fixed Rate

Runs the method every β€˜X’ milliseconds. Enable it with @Scheduled(fixedRate = timeInMilliseconds).

java
@Scheduled(fixedRate = 2000)
public void repeatEveryTwoSeconds() {
System.out.println("I run every two seconds, no matter the previous run!");
}

Fixed Delay

Runs the method β€˜X’ milliseconds after the previous execution is done. Enable it with @Scheduled(fixedDelay = timeInMilliseconds).

java
@Scheduled(fixedRate = 2000)
public void repeatAfterTwoSeconds() {
System.out.println("I run two seconds after the previous run is over!");
}

You can also adjust the initial execution delay adding initialDelay, as such: @Scheduled(fixedDelay = 2000, initialDelay = 3000).

Cron

For greater flexibility, Spring allows us to adjust the repetition pattern with Cron. Enable it with @Scheduled(cron = "* * * * * *").

java
@Scheduled(cron = "0 0 0 * * *")
public void repeatEveryMidnight() {
System.out.println("I run every day at midnight");
}

Unix cron vs Spring cron

There are some subtle differences between the cron schedules you’ll set up in Spring applications and the ones you’ll find in your typical Linux machine.

Unix Cron

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month(1 - 31)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of week (0 - 6) (Sunday to Saturday)
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚
* * * * *

Spring Cron

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ second (0-59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month (1 - 31)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of week (0 - 7) (Saturday to Saturday)
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
* * * * * *

As you can see, where Unix-like Cron has only 5 fields (some systems have 6, but that’s used for user permissions), Spring-like Cron has 6; adding the ability do manage tasks my the second.

Moreover, while traditional Cron only supports macros in some systems, Springs version does so by default:

MacroDescriptionCron
@yearlyOnce a year0 0 0 1 1 *
@monthlyOnce a month0 0 0 1 * *
@weeklyOnce a week0 0 0 * * 0
@dailyOnce a day0 0 0 * * *
@hourlyOnce evey hour0 0 * * * *

Other posts you might like