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秒鐘自動(dòng)執(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;}
啟動(dòng)測(cè)試
啟動(dòng)項(xiàng)目,定時(shí)器任務(wù)在項(xiàng)目啟動(dòng)時(shí)執(zhí)行一次,之后每隔10秒自動(dòng)執(zhí)行一次。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 告別AJAX實(shí)現(xiàn)無刷新提交表單2. 博客日志摘要暨RSS技術(shù)3. JSP的Cookie在登錄中的使用4. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟5. ASP常用日期格式化函數(shù) FormatDate()6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. SSM框架整合JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解8. 解析原生JS getComputedStyle9. 使用XSL將XML文檔中的CDATA注釋輸出為HTML文本10. XML解析錯(cuò)誤:未組織好 的解決辦法
