SpringBoot定時(shí)任務(wù)不執(zhí)行的幾個(gè)可能原因及解決方法
首先在主Application上加上 @EnableScheduling 注解 表明,
本app有定時(shí)任務(wù).需要掃描定時(shí)任務(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); } }
然后在主定時(shí)任務(wù)類上加上
@Component
@EnableScheduling
@EnableAsync
前兩個(gè)注解, 第三個(gè)@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運(yùn)行"+ System.currentTimeMillis()); } }
看似簡(jiǎn)單,卻隱藏著很多坑,一不小心就掉進(jìn)去了,比如:
(1)此方法不能有參數(shù)
(2)此方法不能有返回值
(3)此類中不能包含其他帶任何注解的方法
(4)此類必須跟主Application同一個(gè)包. 如下圖.
以上就是SpringBoot定時(shí)任務(wù)不執(zhí)行的幾個(gè)可能原因及解決方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot定時(shí)任務(wù)不執(zhí)行的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AsyncHttpClient的TimeoutTimerTask連接池異步超時(shí)
這篇文章主要為大家介紹了AsyncHttpClient的TimeoutTimerTask連接池異步超時(shí)源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12使用@PathVariable接收兩個(gè)參數(shù)
這篇文章主要介紹了使用@PathVariable接收兩個(gè)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java中實(shí)現(xiàn)分頁的幾種常見方式總結(jié)
在項(xiàng)目中經(jīng)常會(huì)查詢大量數(shù)據(jù),這就要用到分頁展示,下面這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)分頁的幾種常見方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12springboot前后臺(tái)數(shù)據(jù)交互的示例代碼
這篇文章主要介紹了springboot前后臺(tái)數(shù)據(jù)交互的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Java基于Socket實(shí)現(xiàn)多人聊天室
這篇文章主要為大家詳細(xì)介紹了Java基于Socket實(shí)現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09