Spring Scheduling Annotations List


After Spring Boot, Spring enhanced with scheduling and Asynchronous annotations which added with methods along with some information about to execute it, and Spring takes care of rest.

These annotations are from the org.springframework.scheduling.annotation package. Below are most common annotations which we use at time of development:

See Also :

Spring Scheduling Annotations

@EnableAsync

@EnableAsync annotation enable asynchronous functionality in Spring. It must use with @Configuration.

@Configuration
@EnableAsync
class VehicleFactoryConfig {}

Now, that we enabled asynchronous calls, we can use @Async to define the methods supporting it.

@EnableScheduling

@EnableScheduling annotation enable scheduling in the application. We should always use it in conjunction with @Configuration:

@Configuration
@EnableScheduling
class VehicleFactoryConfig {}

Now we are enabled with Schedule, we can now run methods periodically with @Scheduled.

@Async

@Async apply on methods which we want to execute on a different thread, hence run them asynchronously.

@Async
void repairCar() {
    // ...
}

If we apply @Aynch annotation to a class, then all methods will be called asynchronously.

Note: Before applying @Async on method first we need to enabled the asynchronous calls for this annotation to work, with @EnableAsync or XML configuration.

@Scheduled

@Scheduled annotation use to execute a method periodically at fixed interval or by Cron like expressions.

For Example : The first execution of the task will be delayed by 5 seconds and then it will be executed normally at a fixed interval of 2 seconds.

@Scheduled(fixedRate = 2000 , initialDelay=5000)
void checkVehicle() {
    // ...
}

After Java 8 with repeating annotations feature, @Scheduled can mark multiple times on a method with different period or Cron expressions.

For Example : The first execution of the task will be delayed by 5 seconds and then it will be executed normally at a fixed interval of 2 seconds and cron expression will execute on Monday to Saturday on 12 PM.

@Scheduled(fixedRate = 2000)
@Scheduled(cron = "0 * * * * MON-SAT")
void checkVehicle() {
    // ...
}

Note:

  • Before applying @Scheduled on method first we need to enabled the scheduling calls by applying @EnableScheduling configuration. that the method annotated with @Scheduled should have a void return type.
  • The method having @Scheduled should have a void return type.

@Schedules

@Schedules annotation use to specify multiple @Scheduled rules.

@Schedules({
  @Scheduled(fixedRate = 2000),
  @Scheduled(cron = "0 * * * * MON-SAT")
})
void checkVehicle() {
    // ...
}

Note: Same feature can achieve  since Java 8 with repeating annotations as described above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s