springboot任務之定時任務

2022-06-11 16:36:13 字數 752 閱讀 5128

在啟動入口上加上@enablescheduling ,在需要定時的方法上加上@scheduled註解

比如:

package

com.gong.spingbootes.service;

import

org.springframework.scheduling.annotation.scheduled;

import

org.springframework.stereotype.service;

@service

public

class

scheduledservcie

}

@scheduled註解中主要引數為cron,裡面有六個值,分別對應著注釋中的。上述**意思是:星期一到星期五的整秒執行方法一次。

啟動伺服器,當時間是到13:22:00時,在控制台會輸出:

在比如:

@scheduled(cron="0,1,2,3,4 * * * * mon-fri") :周一到周五的第0,1,2,3,4秒都會執行

@scheduled(cron="0-4 * * * * mon-fri "):周一到周五的第0,1,2,3,4秒都會執行

@scheduled(cron="0/4 * * * * mon-fri"):周一到周五從第0秒開始,每隔4秒執行一次

具體的可以參照上述**。

springboot任務之定時任務

1 service包下新建scheduleservice類 package com.example.springboottask.service import org.springframework.scheduling.annotation.scheduled import org.springf...

springboot之定時任務

pom 包裡面只需要引入 spring boot starter 包即可 org.springframework.boot spring boot starter org.springframework.boot spring boot starter test test 在啟動類上面加上 enab...

SpringBoot之 非同步 定時 郵件任務

2 測試步驟 三 郵件任務 參考資訊 我們在 上傳送郵件,後台在傳送郵件時由於時間過長導致前端響應不動,直到郵件傳送完畢前端才可以響應。對於這種任務,可以採用非同步的方式進行處理。1 建立service包 2 在包中建立乙個asyncservice類,編寫相關方法並在方法上新增 async註解,告訴...