首先在啟動類上添加注解:@enablescheduling 來開啟定時任務
1
2
3
4
5
6
7
|
@springbootapplication @enablescheduling public class application { public static void main(string[] args) { springapplication.run(application. class , args); } } |
然后新建定時任務類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@component public class quartzservice { /** * 通過時間表達式執行定時任務 */ @scheduled (cron = "0 0/1 * * * ?" ) public void timertonow(){ system.out.println( "now time:" + new simpledateformat( "yyyy-mm-dd hh:mm:ss" ).format( new date())); } /** *啟動時間點之后 x毫秒秒執行一次 */ @scheduled (fixedrate = 5000 ) public void timertozzp(){ system.out.println( "fixedrate:" + new random().nextlong() + new simpledateformat( "hh:mm:ss" ).format( new date())); } /** * 結束時間點之后 每x毫秒執行一次 */ @scheduled (fixeddelay = 10000 ) public void timertoreportcount(){ system.out.println( "fixeddelay:" + new random().nextlong() + new simpledateformat( "hh:mm:ss" ).format( new date())); } /** * 第一次延遲 x毫秒執行,之后按照fixedrate的規則每x毫秒執行 */ @scheduled (initialdelay = 10000 ,fixedrate = 6000 ) public void timertoreport(){ system.out.println( "initialdelay:" + new random().nextlong() + new simpledateformat( "hh:mm:ss" ).format( new date())); } } |
啟動項目,定時任務開始
總結
以上所述是小編給大家介紹的springboot定時任務處理類的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/qq_29884151/article/details/80577195