This is the 28th day of my participation in the August Challenge

We more or less use scheduled tasks in the development of actual projects. If it is an e-commerce project, the scheduled tasks related to orders and payments can be seen everywhere, and they may be executed every day, every hour and every moment. In order to be flexible and convenient, we add visual operations for scheduled tasks, and introduce a monitoring system for scheduled tasks to observe the execution results of scheduled tasks. There is still a place for a scheduled task in a project, and today we will learn how to create a scheduled task in a SpringBoot project.

Implement Scheduled tasks using the @scheduled annotation

@ EnableScheduling annotations

When implementing Scheduled tasks using the @Scheduled annotations, you first add an @enablesCheduling annotation to the startup class to enable Scheduled tasks and find the @scheduled annotation when the project is executed.

@ Scheduled annotations

Once Scheduled tasks are enabled, you need to create a Scheduled task class, hand it over to the Spring container for management using the @Component annotation, define the methods in the class and annotate them with the @Scheduled annotation, and set the Scheduled execution rules for the methods.

@Component
public class TestTask {
    @Scheduled(fixedRate = 2000)
    public void printHello(a){
        System.out.println(System.currentTimeMillis() + ":" + "hello!"); }}Copy the code

After the configuration is complete, the SpringBoot project is started and scheduled tasks are automatically executed based on the preset rules

For the @Scheduled annotation, we can take a look at the definition

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
    String CRON_DISABLED = "-";
    String cron(a) default "";
    String zone(a) default "";
    long fixedDelay(a) default -1L;
    String fixedDelayString(a) default "";
    long fixedRate(a) default -1L;
    String fixedRateString(a) default "";
    long initialDelay(a) default -1L;
    String initialDelayString(a) default "";
}
Copy the code

The time rules that can be used in annotations are:

  • @Scheduled(fixedRate = 2000): The interval is 2 seconds
  • @Scheduled(fixedDelay = 2000): Execute the command again at an interval of 2 seconds
  • @Scheduled(initialDelay = 2000, fixedRate = 3000): The delay for the first execution is 2 seconds and the execution is performed every 3 seconds
  • @Scheduled(cron = "* * * * * *"): cron expression, which represents (every minute, every day, every week, every month, every year) to be executed once every second

Cron expression

The cron expression can be used to set timing rules for scheduled tasks. The cron expression can be used as follows:Second minute hour day month week year (may be omitted), the value range and available symbols for each position are:For the special symbols, their meanings are:

  • .: The character can specify multiple values, such as"5,10 * * * * *"A scheduled task is executed in seconds 5 and 10 per minute
  • -: This character can specify a range value, such as"5-8 * * * * *"Indicates that scheduled tasks are executed in 5, 6, 7, or 8 seconds every minute
  • /: this character can specify an initial value and an incremental step size, as in"0/5 * * * * *"That is, a scheduled task is executed every five seconds
  • *: indicates all values, such as"* * * * * *"Indicates that scheduled tasks are executed every second
  • ?The: symbol is used only for days and weeks to match any value"* * *? *?"To match tasks executed per second
  • LFor example, L indicates the last day of the month. 2L indicates the last Tuesday of the month
  • W: indicates a valid working day (Monday to Friday). For example, 8W If the 8th day of this month falls on a Saturday, the command is executed on Friday, the latest working day
  • #For example, 2#4 indicates the second Thursday of this month

The last

That’s how you implement Scheduled tasks in a SpringBoot project using the @scheduled annotation, and how cron expressions are used in annotations. The @scheduled annotation is just one way to implement Scheduled tasks in the SpringBoot project, but there are other ways we’ll learn more about later.