Springboot timed tasks @Scheduled cron expression, fixedRate, fixedDelay, initialDelay and the combination of @EnableAsync, @Async

Several forms of timing tasks

1, cron custom expression


    @Scheduled(cron = "${statistics.ams.day-cron}")
    private void test() {
    
    
        System.out.println("doSomeThing--------");
    }

The default execution method is similar to fixedDelay, and it will start counting according to the end time of the last method .
But it can be changed to multi- threaded asynchronous execution, so that each execution will not affect each other:

  1. Use class or start class to add @EnableAsync annotation
@EnableScheduling
@EnableFeignClients
@EnableAsync
public class Application {
    
    

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

}
  1. Add @Async annotation on Fangfa

    @Scheduled(cron = "${statistics.ams.day-cron}")
    @Async
    private void test() {
    
    
        System.out.println("doSomeThing--------");
    }

2. FixedDelay(fixedDelayString) unit: milliseconds

The @Async annotation cannot be used, and an error will be reported.
initialDelay = 10000 means that after the container is started, the timer
fixedDelay is executed again after a delay of 10 seconds to receive the long integer number.
fixedDelayString receives a string number (usually the value in the configuration file is directly taken).
fixedDelay:

  1. The next time will be executed after the specified time interval after the previous execution.
  2. If there is a block, it will wait until the last execution is completed before executing after the specified time interval.
  3. If the code throws an exception (equivalent to the execution of the method, it will not be caught by the global exception handling (@ControllerAdvice)), continue to the next execution.
    @Scheduled(initialDelay = 0, fixedDelayString = "${statistics.ams.week-during}")
    private void test2() {
    
    
        System.out.println("doSomeThing--------");
    }

3. FixedRate (fixedRateString) unit: milliseconds

The @Async annotation cannot be used, and an error will be reported.
initialDelay = 10000 means that after the container is started, the timer
fixedRate is executed again after a delay of 10 seconds to receive the long integer number (@Scheduled( initialDelay = 0, fixedRate=5000L))//5 seconds.
fixedRateString receives a string number (usually the value in the configuration file is directly taken).
fixedRate:

  1. The next time is counted from the beginning of the previous execution and executed at the specified time interval.
  2. If there is blocking, it will not be executed next time, but the number of times that should be executed during the blocking period will be accumulated. When it is no longer blocked, all of these will be executed at once, and then continue to execute at a fixed rate.
  3. If the code throws an exception (equivalent to the execution of the method, it will not be caught by the global exception handling (@ControllerAdvice)), continue to the next execution.
    @Scheduled(initialDelay = 0, fixedRateString = "${statistics.ams.week-during}")
    private void test2() {
    
    
        System.out.println("doSomeThing--------");
    }

Guess you like

Origin blog.csdn.net/xc_nostalgia/article/details/111406565