Spring Boot定時(shí)器創(chuàng)建及使用解析
創(chuàng)建定時(shí)器
因?yàn)轫?xiàng)目需要定時(shí)在后端執(zhí)行任務(wù)刷新數(shù)據(jù),不需要從前端調(diào)用接口,所以需要使用定時(shí)器?;谧⒔夥绞紷Scheduled默認(rèn)為單線程。
package com.ruanshuai.demo.util;import com.ruanshuai.demo.config.ConfigConsts;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/** * @author ruanshuai * @date 2019/10/30 */@Component@EnableSchedulingpublic class TestSchedule { @Scheduled(fixedDelay = ConfigConsts.TEN_SECONDS) public void test(){ System.out.println('定時(shí)任務(wù)執(zhí)行開始!'); System.out.println('這是一個(gè)定時(shí)任務(wù)!'); System.out.println('定時(shí)任務(wù)執(zhí)行結(jié)束!'); }}
其中TEN_SECONDS表示10秒,定時(shí)器任務(wù)每10秒鐘自動執(zhí)行一個(gè)。
各種時(shí)間表示如下:
1 * 1000表示1秒; 60 * 1 * 1000表示1分鐘; 60 * 60 * 1 * 1000表示1小時(shí); 24 * 60 * 60 * 1 * 1000表示1天;依此類推
package com.ruanshuai.demo.config;/** * @author ruanshuai * @date 2019/10/30 */public class ConfigConsts { public static final long TEN_SECONDS = 10 * 1 * 1000;}
啟動測試
啟動項(xiàng)目,定時(shí)器任務(wù)在項(xiàng)目啟動時(shí)執(zhí)行一次,之后每隔10秒自動執(zhí)行一次。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法2. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )3. ASP中實(shí)現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)4. django創(chuàng)建css文件夾的具體方法5. 存儲于xml中需要的HTML轉(zhuǎn)義代碼6. Android打包上傳AAR文件到Maven倉庫的示例7. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)8. MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明9. phpstudy apache開啟ssi使用詳解10. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲
