springboot通過注解、接口創(chuàng)建定時(shí)任務(wù)詳解
項(xiàng)目中經(jīng)常會(huì)用到定時(shí)任務(wù),有的人在用quartz,有的人可能自己搭建了一套調(diào)度平臺,springboot對于定任務(wù)的支持,讓定時(shí)任務(wù)的創(chuàng)建變得簡單,今天來說說springboot中定時(shí)任務(wù)的創(chuàng)建。
springboot中定時(shí)任務(wù)的創(chuàng)建
springboot定時(shí)任務(wù)的創(chuàng)建,這里就主要說兩種方式
- 通過注解創(chuàng)建
- 通過springboot中提供的接口實(shí)現(xiàn)
springboot通過注解創(chuàng)建定時(shí)任務(wù)
首先引入pom
在類上主要用到了@EnableScheduling注解,都在org.springframework:spring-context這個(gè)包下
就引入org.springframework:spring-context這個(gè)包就可以使用了
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
直接上代碼來一個(gè)栗子
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * @createdTime: 2020/4/7 16:00. * @version: 1.0 . */ //在類型使用@EnableScheduling來開啟定時(shí)任務(wù) @Component @EnableScheduling public class TestTask { private static ThreadLocal<SimpleDateFormat> dateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); //在方法上使用@Scheduled注解來創(chuàng)建具體的定時(shí)任務(wù) @Scheduled(cron = "0/10 * * * * ?") private void task1() { System.err.println("執(zhí)行定時(shí)任務(wù)了,執(zhí)行時(shí)間為:" + dateFormat.get().format(new Date())); } }
看下執(zhí)行結(jié)果:
在類上使用了@EnableScheduling來開啟定時(shí)任務(wù),使用了@Component是為了注入到spring容器中,這里不用@Component會(huì)不會(huì)注入我倒沒有試過,有試過的小伙伴可以說一下。
在具體需要定時(shí)執(zhí)行的方法上,使用 @Scheduled注解,這個(gè)注解里面的參數(shù)有很多種,我這用了cron表達(dá)式,這里介紹下這個(gè)注解的參數(shù)吧
@Scheduled注解的各個(gè)參數(shù)
- cron
使用方式:@Scheduled(cron = "0/10 * * * * ?")
源碼定義:String cron() default "";
說明:cron表達(dá)式,就是我們?nèi)粘S玫腸ron,具體的就不貼出來了
- zone
使用方式:@Scheduled(zone = "GMT+08:00")
源碼定義:String zone() default "";
說明:時(shí)區(qū),cron表達(dá)式會(huì)基于這個(gè)時(shí)區(qū)解析,默認(rèn)為空,會(huì)取應(yīng)用所在服務(wù)器的時(shí)區(qū),一般不填就可以了,和jdk中TimeZone用的是統(tǒng)一體系,就不具體說了
- fixedDelay
使用方式:@Scheduled(fixedDelay = 1)
源碼定義:long fixedDelay() default -1;
說明:上次執(zhí)行完了,相隔多長時(shí)間以后再執(zhí)行,單位是毫秒
- fixedDelayString
使用方式:
@Scheduled(fixedDelayString = "1")
@Scheduled(fixedDelayString = "${配置文件里面的值}")
源碼定義:String fixedDelayString() default "";
說明:和fixedDelay一樣,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣
- fixedRate
使用方式:@Scheduled(fixedRate = 1)
源碼定義:long fixedRate() default -1;
說明:上次執(zhí)行開始后,相隔多長時(shí)間以后再執(zhí)行,單位是毫秒
- fixedRateString
使用方式:
@Scheduled(fixedRateString = "1")
@Scheduled(fixedRateString = "${配置文件里面的值}")
源碼定義:String fixedRateString() default "";
說明:和fixedRate一樣,,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣
- initialDelay
使用方式:@Scheduled(initialDelay = 1)
源碼定義:long initialDelay() default -1;
說明:上第一次執(zhí)行后,相隔多長時(shí)間以后再執(zhí)行,單位是毫秒
- initialDelayString
使用方式:
@Scheduled(initialDelayString = "1")
@Scheduled(initialDelayString = "${配置文件里面的值}")
源碼定義:String initialDelayString() default "";
說明:和initialDelay一樣,,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣
springboot通過注接口創(chuàng)建定時(shí)任務(wù)
通過接口創(chuàng)建定時(shí),就會(huì)比較靈活,定時(shí)cron表達(dá)式就不用寫死在代碼的注解上了,可以通過存儲到數(shù)據(jù)庫等存儲系統(tǒng)中,在接口中來獲取這個(gè)配置的表達(dá)式,這樣可以實(shí)現(xiàn)一個(gè)簡易的任務(wù)調(diào)度平臺,通過數(shù)據(jù)庫配置就可以管理定時(shí)任務(wù)的執(zhí)行
實(shí)現(xiàn)接口SchedulingConfigurer
主要用的是這個(gè)接口SchedulingConfigurer,他是org.springframework.scheduling.annotation.SchedulingConfigurer這個(gè)包路徑,其實(shí)也是都在org.springframework:spring-context這個(gè)包下
就引入org.springframework:spring-context這個(gè)包就可以使用了
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
主要方法
復(fù)寫configureTasks方法,這個(gè)方法通過ScheduledTaskRegistrar來添加定時(shí)任務(wù),大致看方法,入?yún)⒒臼且粋€(gè)線程對象,后面那個(gè)參數(shù)和注解里面一樣,主要有cron表達(dá)式,delay上次執(zhí)行完了,相隔多長時(shí)間以后再執(zhí)行,initial什么的就不一一贅述了
直接上代碼來一個(gè)栗子
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.Task; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * @createdTime: 2020/4/7 18:33. * @version: 1.0 . */ @Component @EnableScheduling public class TestTask2 implements SchedulingConfigurer { private static ThreadLocal<SimpleDateFormat> dateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); /** * Callback allowing a {@link TaskScheduler * TaskScheduler} and specific {@link Task Task} * instances to be registered against the given the {@link ScheduledTaskRegistrar}. * * @param taskRegistrar the registrar to be configured. */ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { System.err.println("假裝從數(shù)據(jù)庫中獲取到了配置好的定時(shí)任務(wù)執(zhí)行計(jì)劃"); String cron = "0/10 * * * * ?"; taskRegistrar.addCronTask(() -> { System.err.println("接口定時(shí)任務(wù)執(zhí)行定時(shí)任務(wù)了,執(zhí)行時(shí)間為:" + dateFormat.get().format(new Date())); },cron); } }
這里通過重寫configureTasks方法,使用ScheduledTaskRegistrar對象來創(chuàng)建定時(shí)任務(wù),然后表達(dá)式可以從數(shù)據(jù)庫等地方讀取,演示時(shí)候就不寫那塊代碼了,這樣可以很簡單的實(shí)現(xiàn)出來一個(gè)簡單的任務(wù)調(diào)度平臺
看下執(zhí)行結(jié)果:
springboot創(chuàng)建定時(shí)任務(wù)就為大家說到這里,歡迎大家來交流,指出文中一些說錯(cuò)的地方,讓我加深認(rèn)識。
總結(jié)
到此這篇關(guān)于springboot通過注解、接口創(chuàng)建定時(shí)任務(wù)的文章就介紹到這了,更多相關(guān)springboot創(chuàng)建定時(shí)任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven中optional和scope元素的使用弄明白了嗎
這篇文章主要介紹了Maven中optional和scope元素的使用弄明白了嗎,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot集成RocketMQ實(shí)現(xiàn)消息發(fā)送的三種方式
RocketMQ?支持3?種消息發(fā)送方式:?同步?(sync)、異步(async)、單向(oneway),本文就將給大家介紹一下SpringBoot集成RocketMQ實(shí)現(xiàn)消息發(fā)送的三種方式文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09springboot+thymeleaf+shiro標(biāo)簽的實(shí)例
這篇文章主要介紹了springboot+thymeleaf+shiro標(biāo)簽的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java基礎(chǔ)教程之類數(shù)據(jù)與類方法
這篇文章主要介紹了Java基礎(chǔ)教程之類數(shù)據(jù)與類方法,本文是對類的深入探討,類數(shù)據(jù)指類的一些屬性、參數(shù)等,類方法就是類包含的功能方法,需要的朋友可以參考下2014-08-08論Java Web應(yīng)用中調(diào)優(yōu)線程池的重要性
這篇文章主要論述Java Web應(yīng)用中調(diào)優(yōu)線程池的重要性,通過了解應(yīng)用的需求,組合最大線程數(shù)和平均響應(yīng)時(shí)間,得出一個(gè)合適的線程池配置2016-04-04SpringBoot整合Mybatis簡單實(shí)現(xiàn)增刪改查
這篇文章主要介紹了SpringBoot整合Mybatis簡單實(shí)現(xiàn)增刪改查,文章為圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08