SoFunction
Updated on 2025-03-09

How to enable timing tasks in Spring Boot

Add @EnableScheduling annotation

Can be added on the Application class

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        (, args);
    }
}

Define timed task classes and methods

The timing task class is to be registered as a bean of the Spring IoC container, or the return value of a method (the return value is a timing task class object) is registered as a Spring IoC bean through @Bean.

package ;
import ;
import ;
@Component
public class ScheduledTask {
    /**
      * Execute at 0 o'clock every day
      */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {
    }
    /**
      * The time interval between the end of this task and the beginning of the next task is 5 seconds
      */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {
    }
    /**
      * The interval between the start of two tasks is 5 seconds
      */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {
    }
    /**
      * The first task starts at 1 second, and the time interval between the end of the subsequent task and the start of the next task is 5 seconds.
      */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {
    }
}

Control whether the timing task is on through configuration items

Method 1 @ConditionalOnProperty

This method controls whether the task class is registered as a Spring IoC bean, and can control whether all the above types of timing tasks are registered.

# Enabled When configured as false, the timing task will not be enabledscheduled-task:
  enabled: true
@Component
@ConditionalOnProperty(name = "", havingValue = "true")
public class ScheduledTask {
    /**
      * Execute at 0 o'clock every day
      */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {
    }
    /**
      * The time interval between the end of this task and the beginning of the next task is 5 seconds
      */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {
    }
    /**
      * The interval between the start of two tasks is 5 seconds
      */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {
    }
    /**
      * The first task starts at 1 second, and the time interval between the end of the subsequent task and the start of the next task is 5 seconds.
      */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {
    }
}

Method 2 Boolean flag bit

Using this method to schedule tasks will still be scheduled to execute, but you will do nothing when the task is executed, and you can also control all the above-mentioned types of timing tasks.

scheduled-task:
  enabled: true
@Component
@ConditionalOnProperty(name = "", havingValue = "true")
public class ScheduledTask {
    @Value("${}")
    private Boolean taskEnabled;
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {
        if (!taskEnabled) {
            return;
        }
        ("start to doFixedDelayTask");
    }
}

Use "-" to close the cron timing task

This method is only suitable for cron-type timing tasks, not other types of timing tasks.

Note: When yml is configured - double quotes or single quotes, otherwise an error will be reported.

Turn off the configuration of the cron timing task:

scheduled-task:
  cron: "-"

Start the configuration of the cron timing task, and configure the cron expression:

scheduled-task:
  cron: 0 0 0 * * ?
@Component
public class ScheduledTask {
    /**
      * Execute at 0 o'clock every day
      */
    @Scheduled (cron = "${}")
    public void doCronTask() {
    }
}

References

  • Michael Pratt:Conditionally Enable Scheduled Jobs in Spring
  • tan Rigong Yibing: @ConditionalOn…annotation, do you know the condition combination?
  • A little rain in Jiangnan: Two ways to implement timing tasks in Spring Boot!

This is the end of this article about how to enable timed tasks in Spring Boot. For more related content on Spring Boot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!