• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Timing tasks in springboot

King Wang

1 月 3, 2022

Personal blog address :http://alexaccele.github.io/

stay springboot Let the task be executed regularly

First add… To the entry class @EnableScheduling annotation

@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Add… To the specified task method to be executed regularly @Scheduled annotation

@Service
public class ScheduledService {
/* second branch when Japan month Zhou
* corn An example of an expression in :0 * * * * *
* It means that every day, every hour, every minute of every month 0 In seconds
* */
@Scheduled(cron = "0-50 * * * * *")
public void scheduledHello(){
System.out.println("hello...");
}
}

Write… In the notes cron The expression can meet the corresponding time requirements , For example, the example code indicates that for the first time in any minute 0 Second to the first 50 Every second will output “hello…”

 

cron expression

Cron expression Is a string , character string With 5 or 6 Space between , It is divided into 6 or 7 Domains , Each domain represents a meaning ,Cron There are two grammatical formats as follows :(1) second   branch   when   date   month   The week of the week year

(2) second   branch   when   date   month   The week of the week

Field permitted Special characters allowed
second (Seconds) 0~59 The integer of ,  –   *   /
branch (Minutes 0~59 The integer of ,  –   *   /
Hours (Hours 0~53 The integer of ,  –   *   /
date (DayofMonth 1~31 The integer of ( Pay attention to the days of the month ) ,  –   *   /  ? L W C
month (Month) 1~12 The integer of ,  –   *   /
week (DayofWeek 0-7 or SUN-SAT,0 and 7 yes SUN ,  –   *   /  ? L C  #
Special characters meaning
,( English comma ) enumeration
-( minus sign ) Section
* arbitrarily
/ Interval step size
Japan / Week conflict match
L Last
W Working day
C and calendar Calculated value after contact
# week ,4#2 It means the next Thursday

 

发表回复