SpringTask-Timer實現(xiàn)定時任務(wù)的詳細(xì)代碼
更新時間:2024年06月11日 10:24:22 作者:發(fā)飆的蝸牛'
在項目中開發(fā)定時任務(wù)應(yīng)該一種比較常見的需求,今天通過示例代碼給大家講解SpringTask-Timer實現(xiàn)定時任務(wù)的相關(guān)知識,感興趣的朋友一起看看吧
1、Timer 實現(xiàn)定時任務(wù)
1.1、JDK1.3 開始推出定時任務(wù)實現(xiàn)工具。
1.2、API
執(zhí)行代碼
public static void main(String[] args) throws ParseException { Timer timer = new Timer(); String str="2024-06-10 23:24:00"; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("定時任務(wù)執(zhí)行"); System.out.println("定時任務(wù)執(zhí)行時間--》"+new Date()); } },date); }
public static void main(String[] args) throws ParseException { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("定時任務(wù)執(zhí)行"); System.out.println("定時任務(wù)執(zhí)行時間--》"+new Date()); } },0,2000); }
2、使用spring進行整合
//pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring: task: execution: thread-name-prefix: task_ shutdown: await-termination: false await-termination-period: 10s scheduling: pool: size: 10
@Scheduled(cron = "0/3 * * * * ? ") public void test1() { System.out.println("定時任務(wù)執(zhí)行test1"); System.out.println("定時任務(wù)執(zhí)行時間--》"+new Date()); } @Scheduled(cron = "0/3 * * * * ? ") public void test2() { System.out.println("定時任務(wù)執(zhí)行test2"); System.out.println("定時任務(wù)執(zhí)行時間--》"+new Date()); }
到此這篇關(guān)于SpringTask-Timer實現(xiàn)定時任務(wù)的文章就介紹到這了,更多相關(guān)SpringTask定時任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis plus的自動填充與樂觀鎖的實例詳解(springboot)
這篇文章主要介紹了Mybatis plus的自動填充與樂觀鎖的實例詳解(springboot),本文給大家介紹的非常詳細(xì)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學(xué)計數(shù)法的問題
今天小編就為大家分享一篇解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學(xué)計數(shù)法的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07java中break和continue區(qū)別及使用場合分析
本文力圖通過實例加使用場合詳解來引導(dǎo)菜鳥重新認(rèn)識break和continue語句,需要的朋友可以參考下2014-01-01Springboot自動配置原理及DataSource的應(yīng)用方式
這篇文章主要介紹了Springboot自動配置原理及DataSource的應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot集成JWT生成token及校驗方法過程解析
這篇文章主要介紹了SpringBoot集成JWT生成token及校驗方法過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04