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)測試
啟動(dòng)項(xiàng)目,定時(shí)器任務(wù)在項(xiàng)目啟動(dòng)時(shí)執(zhí)行一次,之后每隔10秒自動(dòng)執(zhí)行一次。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密2. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站3. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)4. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】5. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送6. Python編寫nmap掃描工具7. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. ASP.NET MVC前臺動(dòng)態(tài)添加文本框并在后臺使用FormCollection接收值10. php5.6不能擴(kuò)展redis.so的解決方法
