编程知识 cdmana.com

Spring scheduled task cron expression (@ scheduled)

  • annotation @Scheduled Can be added to a method as a trigger source , for example , The following method will take a fixed delay time 5 Call once per second to execute , This cycle is the of the previous call task Completion time Benchmarking , After the last task ,5s Then execute again :
@Scheduled(fixedDelay = 5000)
public void doSomething() { 
   
}
  • If necessary, perform at a fixed rate , Just change the attribute name specified in the annotation to fixedRate that will do , The following method will be at a fixed rate 5s To invoke an execution , This cycle is the above task Starting time Benchmarking , Since the last task 5s Call again :
@Scheduled(fixedRate = 5000)
public void doSomething() { 
   
}

cron expression  @Scheduled(cron = "0 0 2 * * ?")

package com.imwoniu.task;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component
public class TaskDemo {
    
    @Scheduled(cron = "0 0 2 * * ?")  // Every day at 2 a.m 
        void doSomethingWith(){
            //  Timed mission starts ......"
            long begin = System.currentTimeMillis();
        
            // The database operation has been performed ...
        
            long end = System.currentTimeMillis();
            logger.info(" The timed mission ends , Total time consuming :[" + (end-begin) / 1000 + "] second ");
            //new Date() All you do is call System.currentTimeMillis(). If only necessary or             
            // Number of milliseconds , Then you can use System.currentTimeMillis() To replace new Date(), The efficiency will be higher     
            //  spot 
    }
}

One cron The expression has at least 6 individual ( Or maybe 7 individual ) Time elements separated by spaces

 In order 
 second (0~59)
 minute (0~59)
 
 Hours (0~23)
 
 God ( month )(0~31, But you need to think about the number of days in your month )
 
 month (0~11)
 
 God ( week )(1~7 1=SUN  or  SUN,MON,TUE,WED,THU,FRI,SAT)
 
7. year (1970-2099)
 
 Each of these elements can be a value ( Such as 6), A continuous interval (9-12), One interval (8-18/4)(/ Said every 4 Hours ), A list of (1,3,5), wildcard . because " Date in month " and " Date of the week " These two elements are mutually exclusive , You have to set one of these ?.
 
 
 Some subexpressions can contain ranges or lists 
 
 for example : subexpression ( God ( week )) It can be for  “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
 
“*” The character represents all possible values 
 
 therefore ,“*” In subexpression ( month ) Li means every month ,“*” In subexpression ( God ( week )) Every day of the week 
 
 
 
“/” Character to specify the increment of a value 
 
 for example : In subexpression ( minute ) Inside “0/15” Says from the first 0 minutes , Every time 15 minute 
 
          In subexpression ( minute ) Inside “3/20” Says from the first 3 minutes , Every time 20 minute ( It and “3,23,43”) It means the same thing 
 
 
“?” The character is used only for days ( month ) And the day ( week ) Two subexpression , Indicates that no value is specified 
 
 When 2 After one of the subexpression has been specified , In order to avoid conflict , You need to set the value of the other subexpression to “?”
 
 
 
“L”  The character is used only for days ( month ) And the day ( week ) Two subexpression , It is the word “last” Abbreviation 
 
 But its meaning is different in two subexpressions .
 
 In heaven ( month ) In the subexpression ,“L” It means the last day of the month 
 
 In heaven ( week ) From the expression ,“L” The last day of the week , That is to say SAT
 
 If in “L” There is a specific content before , It has other meanings 
 
 for example :“6L” Is the last but one of the month 6 God ,“FRIL” The last Friday of the month 
 
 Be careful : In the use of “L” When parameters are , Do not specify a list or scope , Because it can cause problems 

版权声明
本文为[, Tangcheng]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909120408080j.html

Scroll to Top