In this article, we will explore Spring Scheduler how we could use it by @Scheduled annotation in the Spring Boot application. We can use it to configure and schedule tasks.
This annotation can be used in a Spring Boot application to schedule any task. But there are some simple rules that we need to follow to annotate a method with @Scheduled are:
Next, let’s see how we could use the @Scheduled Annotation in the Spring Boot application.
The @EnableScheduling annotation enables support for scheduling tasks and the @Scheduled annotation in Spring Boot as below in Java-based configuration:
@Configuration
@EnableScheduling
public class SchedulerConfig {
...
}
Let’s move to use @Scheduled Annotation in the Spring Boot application.
We can use this annotation @Scheduled in Spring Boot with several defined attributes based on the requirements.
Let’s start by configuring a task to run after a fixed delay:
@Scheduled(fixedDelay = 1000)
public void scheduleFixedDelayTask() {
//Scheduled work here
}
In the above code, the fixedDelay attribute has a duration (1000 milliseconds). This duration is fixed as the end of the last execution and the start of the next execution. In this case, the task always waits until the previous one is finished. This option is better when you want that the previous execution is completed before running again.
Let’s explore another way of configuring a task to run with a fixed rate:
Let’s now execute a task at a fixed interval of time:
@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTask() {
//Scheduled work here
}
In this case, the scheduled task will start running again after crossing the fixed-rate value of duration (1000 milliseconds). This option is good for the execution of the task independently. But note here, scheduled tasks don’t run in parallel by default. If you want to make parallel then you have to add the @Async annotation as below:
@EnableAsync
public class ScheduledFixedRateExample {
@Async
@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTaskAsync() throws InterruptedException {
//Scheduled work here
}
}
Now this asynchronous task will be invoked each second, even if the previous task isn’t done.
There are the following key differences between the fixedDelay and fixedRate attributes of @Scheduled annotation in Spring Boot.
Next, let’s schedule a task with a delay:
@Scheduled(fixedDelay = 1000, initialDelay = 1000)
public void scheduleFixedRateWithInitialDelayTask() {
//Scheduled work here
}
Note how we’re using both fixedDelay as well as initialDelay in this example. The task will be executed the first time after the initialDelay value, and it will continue to be executed according to the fixedDelay.
Sometimes we want to schedule a task for a particular time of every day or every month etc. In this case, delays and rates are not enough. Spring provide support to use cron expression to control the schedule of our tasks:
@Scheduled(cron = "0 45 23 * * *")
public void scheduleTaskUsingCronExpression() {
//Scheduled work here
}
In this example, we are scheduling a task to be executed at 11:45 PM every day.
In this article, we explored Spring scheduler using the @Scheduled annotation in the Spring Boot application. It is a simple way to configure and schedule a task that you want to run as per your need automatically.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore a simple Spring Boot application to implement a…