日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

spring boot 定時(shí)任務(wù)

作者:郭俊強(qiáng) 更新時(shí)間: 2023-07-16 編程語(yǔ)言

啟動(dòng)類上添加@EnableScheduling注解

@EnableScheduling
@SpringBootApplication
public class StartApplication {

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

}

然后想要使用定時(shí)器的方法上加入@Scheduled(initialDelay = 1000 * 2,fixedRate = 1000 * 5)

@Scheduled(initialDelay =  1000 * 2,fixedRate = 1000 * 5)
public void interTime (){
   log.info("我正在執(zhí)行");
}

initialDelay表示什么時(shí)候開(kāi)始執(zhí)行,
fixedRate 表示多久運(yùn)行一次

上面代碼標(biāo)識(shí)兩秒后開(kāi)始執(zhí)行,每五秒執(zhí)行一次

還有另一種寫法?cron表達(dá)式,網(wǎng)上搜索在線cron表達(dá)式,可以輕松的實(shí)現(xiàn)定時(shí)器的時(shí)間設(shè)置

@Scheduled(cron = "5/5 * * * * ?")
public void interTime (){
    log.info("我正在執(zhí)行");
}

上面的代碼配合下圖就可以很輕松的理解出 ,該代碼5秒后開(kāi)始,每5秒執(zhí)行一次

?

原文鏈接:https://blog.csdn.net/qq_37061571/article/details/122028215

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類
最近更新