
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 8, 2024
When single-threaded execution isn’t enough, we can use annotations from the org.springframework.scheduling.annotation package.
In this quick tutorial, we’re going to explore the Spring Scheduling Annotations.
With this annotation, we can enable asynchronous functionality in Spring.
We must use it with @Configuration:
@Configuration
@EnableAsync
class VehicleFactoryConfig {}
Now, that we enabled asynchronous calls, we can use @Async to define the methods supporting it.
With this annotation, we can enable scheduling in the application.
We also have to use it in conjunction with @Configuration:
@Configuration
@EnableScheduling
class VehicleFactoryConfig {}
As a result, we can now run methods periodically with @Scheduled.
We can define methods we want to execute on a different thread, hence run them asynchronously.
To achieve this, we can annotate the method with @Async:
@Async
void repairCar() {
// ...
}
If we apply this annotation to a class, then all methods will be called asynchronously.
Note, that we need to enable the asynchronous calls for this annotation to work, with @EnableAsync or XML configuration.
More information about @Async can be found in this article.
If we need a method to execute periodically, we can use this annotation:
@Scheduled(fixedRate = 10000)
void checkVehicle() {
// ...
}
We can use it to execute a method at fixed intervals, or we can fine-tune it with cron-like expressions.
@Scheduled leverages the Java 8 repeating annotations feature, which means we can mark a method with it multiple times:
@Scheduled(fixedRate = 10000)
@Scheduled(cron = "0 * * * * MON-FRI")
void checkVehicle() {
// ...
}
Note, that the method annotated with @Scheduled should have a void return type.
Moreover, we have to enable scheduling for this annotation to work for example with @EnableScheduling or XML configuration.
For more information about scheduling read this article.
We can use this annotation to specify multiple @Scheduled rules:
@Schedules({
@Scheduled(fixedRate = 10000),
@Scheduled(cron = "0 * * * * MON-FRI")
})
void checkVehicle() {
// ...
}
Note, that since Java 8 we can achieve the same with the repeating annotations feature as described above.
In this article, we saw an overview of the most common Spring scheduling annotations.
Follow the Spring Category