SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法
首先在主Application上加上 @EnableScheduling 注解 表明,
本app有定時任務(wù).需要掃描定時任務(wù)的類.
package com.other; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class OtherApplication { public static void main(String[] args) { SpringApplication.run(OtherApplication.class, args); } }
然后在主定時任務(wù)類上加上
@Component
@EnableScheduling
@EnableAsync
前兩個注解, 第三個@EnableAsync 視情況加
我的代碼如下
package com.other.task; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableAsync @EnableScheduling public class UploadGrade { @Scheduled(fixedRate = 2000) public void task1(){ System.out.println("task1運行"+ System.currentTimeMillis()); } }
看似簡單,卻隱藏著很多坑,一不小心就掉進(jìn)去了,比如:
(1)此方法不能有參數(shù)
(2)此方法不能有返回值
(3)此類中不能包含其他帶任何注解的方法
(4)此類必須跟主Application同一個包. 如下圖.
以上就是SpringBoot定時任務(wù)不執(zhí)行的幾個可能原因及解決方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot定時任務(wù)不執(zhí)行的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AsyncHttpClient的TimeoutTimerTask連接池異步超時
這篇文章主要為大家介紹了AsyncHttpClient的TimeoutTimerTask連接池異步超時源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12springboot前后臺數(shù)據(jù)交互的示例代碼
這篇文章主要介紹了springboot前后臺數(shù)據(jù)交互的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10