Spring Boot中定時器的使用

2021-10-10 22:15:36 字數 777 閱讀 1146

因為專案需要定時在後端執行任務重新整理資料,不需要從前端呼叫介面,所以需要使用定時器。基於註解方式@scheduled預設為單執行緒。

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

@enablescheduling

public class testschedule

}

其中ten_seconds表示10秒,定時器任務每10秒鐘自動執行乙個。

各種時間表示如下:

package com.ruanshuai.demo.config;

/** * @author ruanshuai

* @date 2019/10/30

*/public class configconsts

啟動專案,定時器任務在專案啟動時執行一次,之後每隔10秒自動執行一次。

SpringBoot之定時器

當我們需要在程式中定時執行乙個任務的時候,就需要定時器了。同傳統的spring專案相比,在基於springboot實現的專案中新增定時器十分容器,那具體怎麼做呢?2.在我們需要定時執行的方法上新增註解 scheduled 3.例項 import org.springframework.schedul...

springBoot使用定時器

configuration enablescheduling public class quartzconfig bean public trigger uploadtasktrigger component public class taskjob extends quartzjobbean da...

學習 SpringBoot 定時器

在springboot啟動類上 加入註解 enablescheduling 在定時器任務的方法上加上註解 scheduled fixedrate 5000 如果該類未被spring管理 那麼需要加上 component 才能被 componentscan basepackages 掃瞄到 sched...