Spring boot自動任務

2021-10-03 17:55:09 字數 818 閱讀 6450

因為專案需要,需要設定自動任務來監測資料庫工單資訊,然後改變其狀態;

.在啟動類中加入開啟定時任務的註解(@enablescheduling): (

"com.verymro.eam.dao"

)(scanbasepackages =

)@enablescheduling

@enableswagger2

public

class

@bean

public paginationinterceptor paginationinterceptor()

}2.相應service中的方法要注釋(@scheduled):

/**

* 定時(間隔一分鐘)任務(超期未執行)

* @param

* @return

*/@override

@scheduled

(fixeddelay =

60000

)public

void

updatestate()

throws parseexception

}}

補充:

1)首先使用乙個@scheduled註解開啟定時任務;

2)fixedrate表示任務執行之間的時間間隔,具體指兩次任務的開始時間間隔,有可能出現第二次任務開始而第一次任務並未結束的情況;

3)fixeddelay表示任務執行之間的時間間隔,具體指本次任務結束到下次任務開始之間的時間間隔;

4)initialdelay表示首次任務啟動的延遲時間;

5)所有時間的單位都是毫秒。

springboot郵件任務

新增依賴 org.springframework.boot spring boot starter mail 設定郵箱主機 spring.mail.host smtp.qq.com 設定使用者名稱 spring.mail.username qq.com 設定密碼,該處的密碼是qq郵箱開啟smtp的授...

Spring Boot 非同步任務

可以使用非同步的方式載入方法 只需在專案入口上定義 enableasync 開啟非同步註解功能 以及某個需要非同步方法上面定義 async註解 即可1.專案入口上定義 enableasync 開啟非同步註解功能 enableasync 開啟非同步註解功能 public class 2.使用 asyn...

springboot非同步任務

enablerabbit 開啟rabbit enableasync 開啟非同步 public class 新建乙個service,此service休眠3秒 package com.example.vuelog.service.task import org.springframework.sched...