久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Boot定時(shí)器創(chuàng)建及使用解析

瀏覽:42日期:2023-08-28 14:09:16

創(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)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 平邑县| 水城县| 富民县| 积石山| 衡东县| 合江县| 肥城市| 江都市| 崇明县| 喀什市| 通海县| 南康市| 房山区| 洛宁县| 河津市| 桐梓县| 共和县| 高州市| 航空| 开化县| 县级市| 绥中县| 云龙县| 伊通| 安图县| 沈阳市| 南靖县| 舞阳县| 平度市| 周口市| 本溪市| 镇沅| 贵港市| 宾阳县| 砀山县| 隆德县| 桂阳县| 抚宁县| 九江县| 永宁县| 漠河县|