Schedule a task in Spring Boot

Aleksandar Grujic
3 min readMar 18, 2022

--

Schedule a task in Spring Boot

Spring Boot is the most popular Framework in the Java world. There are countless things we can do to make our app the way we want.

In this post, I will show you how to schedule a task that will be triggered based on a rate or cron expression.

How to schedule a task in Spring Boot?

To schedule a task in Spring Boot we use the @Scheduled annotation.
We place it above the declaration of the method that should not expect any parameters, and the return type should be void.

Spring Boot internally uses the TaskScheduler interface to schedule the annotated execution methods.

Let’s see some steps required to implement the scheduling:

  1. Create one simple Spring Boot app using Spring Initializr. I’m sure you already know how to create a Spring Boot app.
  2. Enable scheduling by adding the @EnableScheduling annotation in your app’s main class.

3. Now, we need to add @Scheduled annotation. Create a service class and inside create a method that will be decorated with a @Scheduled annotation.

Let’s run the app and see the output:

Output:
2021-11-15 12:35:00.828 INFO 12408 --- [main] c.a.scheduled.ScheduledDemoApplication : Started ScheduledDemoApplication in 5.725 seconds (JVM running for 6.908)
Code is being executed... Time: 15/11/2021 12:35:05
Code is being executed... Time: 15/11/2021 12:35:10
Code is being executed... Time: 15/11/2021 12:35:15
Code is being executed... Time: 15/11/2021 12:35:20

As you can see, we have created our first scheduled task in Spring Boot!
Here, we set the fixed rate to 5000ms, which means this task will be executed every 5 seconds.

Other types of scheduling

Schedule a Task with Fixed Delay

In the above example, we scheduled a task with a fixed rate. However, it can also be scheduled with a fixed delay (fixedDelay), which counts the delay after the completion of the last invocation.

Output:


2021–11–15 13:00:59.973 INFO 8088 — — [main] c.a.scheduled.ScheduledDemoApplication : Started ScheduledDemoApplication in 3.747 seconds (JVM running for 4.478) Code is being executed… Time: 15/11/2021 01:01:02
Code is being executed… Time: 15/11/2021 01:01:10
Code is being executed… Time: 15/11/2021 01:01:19
Code is being executed… Time: 15/11/2021 01:01:27

Schedule a task with Initial Delay

The task can be scheduled with some initial delay (initialDelay), which delays the first execution of the task.

Output:

2021-11-15 13:07:15.268  INFO 5752 --- [ main] c.a.scheduled.ScheduledDemoApplication: Started ScheduledDemoApplication in 5.063 seconds (JVM running for 5.812)
Code is being executed... Time: 15/11/2021 01:07:19
Code is being executed... Time: 15/11/2021 01:07:24
Code is being executed... Time: 15/11/2021 01:07:29
Code is being executed... Time: 15/11/2021 01:07:34

Schedule a task Using the cron expression

If you need more control over scheduling then use the cron expressions.

The above task will be executed every day at 10 am.

Use value from the property file

It is always better to externalize the value of the scheduled task into a property file instead of hardcoding it.
To read the value from the property file, use the “{full_path_to_property}” pattern.

application.properties

com.scheduled.cron=*/10 * * * * *

Let’s read the value in our service class:

That was all about how to schedule a task in Spring Boot. Check out the Spring Boot tutorial here.

--

--