Spring Boot @Scheduled Example

1. Introduction

@Scheduled is a Spring Boot annotation used to mark a method to be scheduled for repeated execution. It offers a way to execute periodic tasks with fixed rate, fixed delay, or cron-like expressions.

Key Points:

1. @Scheduled can be used for creating simple background jobs like database cleanup, sending periodic emails, etc.

2. This annotation supports various scheduling options like fixedRate, fixedDelay, and cron.

3. To activate @Scheduled annotations, @EnableScheduling must be added to the configuration.

2. Implementation Steps

1. Enable scheduling in the Spring Boot application by adding @EnableScheduling to the main class or a configuration class.

2. Create a method in a Spring bean and annotate it with @Scheduled, specifying the desired scheduling options.

3. Run the application and verify that the scheduled method is being executed as configured.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Scheduled annotation:
// Step 1: Enable scheduling in your Spring Boot application
@SpringBootApplication
@EnableScheduling
public class ScheduledTasksApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledTasksApplication.class, args);
    }
}

// Step 2: Create a component with scheduled tasks
@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    // A task scheduled to run after a fixed delay
    @Scheduled(fixedDelay = 5000)
    public void reportCurrentTimeWithFixedDelay() {
        System.out.println("Fixed Delay Task: The time is now " + dateFormat.format(new Date()));
    }

    // A task scheduled to run at a fixed rate
    @Scheduled(fixedRate = 10000)
    public void reportCurrentTimeAtFixedRate() {
        System.out.println("Fixed Rate Task: The time is now " + dateFormat.format(new Date()));
    }

    // A task scheduled to run with a cron expression
    @Scheduled(cron = "0 * * * * ?")
    public void reportCurrentTimeWithCronExpression() {
        System.out.println("Cron Expression Task: The time is now " + dateFormat.format(new Date()));
    }
}

Output:

Run the application and observe the console for the scheduled tasks' output:
// Console output after the application starts running
Fixed Delay Task: The time is now 14:00:00
Fixed Rate Task: The time is now 14:00:10
Cron Expression Task: The time is now 14:01:00
... // continues to run at specified intervals

Explanation:

1. @EnableScheduling in the ScheduledTasksApplication class activates the scheduling functionality.

2. ScheduledTasks is a @Component containing methods with the @Scheduled annotation, each demonstrating a different scheduling configuration.

3. reportCurrentTimeWithFixedDelay is scheduled to run with a fixed delay of 5 seconds after the completion of the last invocation.

4. reportCurrentTimeAtFixedRate is scheduled to run at a fixed rate of 10 seconds, regardless of the completion time of the previous execution.

5. reportCurrentTimeWithCronExpression is scheduled using a cron expression, set to run at the top of every minute.

6. The application outputs the current time to the console whenever each scheduled task is run, demonstrating that the tasks are executing according to their configurations.


Comments