Schedule tasks in Spring Boot with @Scheduled annotation

Spring Boot is a popular framework that provides an easy way to create web applications. It offers a lot of features that make development easier and faster. One of these features is the ability to schedule tasks using the @Scheduled annotation. In this article, we will explore this feature and provide some examples. See spring docs for more information on this

What is the @Scheduled annotation?

The @Scheduled annotation is used to schedule tasks in Spring Boot. It is a part of the Spring Framework’s Task Execution and Scheduling module. With this annotation, you can schedule tasks to run at specific intervals, or at specific times. The tasks can be run in a separate thread or in the same thread as the main application.

Enable Scheduling Annotations

To enable support for @Scheduled annotation, you can add @EnableScheduling to one of your @Configuration classes, as the following example shows:

@Configuration
@EnableScheduling
public class AppConfig {
}

How to use the @Scheduled annotation?

To use the @Scheduled annotation, you need to add it to a method in your Spring Boot application.

  1. The method should have a void return type and no parameters.
  2. You also need to specify when the task should be executed using one of the available options for scheduling.

Here’s an example:

@Servicepublic class MyScheduledService {
    @Scheduled(fixedRate = 1000)
    public void runTask() {
        // Your task logic goes here
    }
}

In the example above, we have a class called MyScheduledService which is annotated with @Service. This tells Spring Boot that this class should be created as a bean and managed by the Spring context. The class also has a method called runTask() which is annotated with @Scheduled. This tells Spring Boot that this method should be executed according to the scheduling options specified.

In this example, we have used the fixedRate option to schedule the task to run every 1000 milliseconds (i.e. 1 second). This means that the task will run every second, regardless of how long the previous execution took.

Available Scheduling Options

There are several options available for scheduling tasks using the @Scheduled annotation. These options are as follows:

  1. fixedDelay: This option specifies the delay between the completion of the last execution and the start of the next execution. For example, if you set the delay to 1000 milliseconds, the task will be executed every 1000 milliseconds after the previous execution completes.
  2. fixedRate: This option specifies the interval between the start time of each execution. For example, if you set the rate to 1000 milliseconds, the task will be executed every 1000 milliseconds, regardless of how long the previous execution took.
  3. initialDelay: This option specifies the delay between the startup of the application and the first execution of the task. For example, if you set the initial delay to 5000 milliseconds, the task will start executing after 5 seconds from the start of the application.
  4. cron: This option allows you to schedule tasks using a cron expression. This provides more fine-grained control over the schedule, allowing you to specify specific dates, times, and intervals for the task.

Here’s an example using the cron option:

@Service
public class MyScheduledService {
 
    @Scheduled(cron = "0 0 12 * * ?")
    public void runTask() {
        // Your task logic goes here
    }
}

In the example above, we have scheduled the task to run every day at 12:00 PM. The cron expression used here is "0 0 12 * * ?". This expression specifies that the task should run at the 0th second, 0th minute, and 12th hour of every day.

Conclusion

In this article, we have explored the @Scheduled annotation in Spring Boot. We have learned how to use it.


Happy Coding 🙂

Pavan Kumar Jadda
Pavan Kumar Jadda
Articles: 36

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.