SpringBoot中實現(xiàn)定時任務(wù)的幾種方式
一、基于注解
這種方式很簡單,主要就是先@EnableScheduling
開啟定時任務(wù)功能,然后在相應(yīng)的方法上添加@Scheduled()
中間寫上相應(yīng)的cron
表達式即可。示例如下:
schedule.ScheduleTask:
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component @EnableScheduling //開啟定時任務(wù) public class ScheduleTask { @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式 public void testScheduleTask() { System.out.println("執(zhí)行定時任務(wù)" + LocalDateTime.now()); } }
Cron表達式參數(shù)參考:
- 秒(0~59) 例如0/5表示每5秒
- 分(0~59)
- 時(0~23)
- 日(0~31)的某天,需計算
- 月(0~11)
- 周幾( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
建議:直接在線生成Cron表達式比較方便:www.matools.com/cron/
@Scheduled
:除了支持靈活的參數(shù)表達式cron
之外,還支持 fixedDelay
,fixedRate
,initialDelay
這些延時性的操作。
啟動測試就實現(xiàn)了基本的定時任務(wù)功能,但是如果我們修改了cron
表達式,需要重啟整個應(yīng)用才能生效,不是很方便。想要實現(xiàn)修改cron
表達式就生效就需要用到接口
的方式來實現(xiàn)定時任務(wù)。
二、基于接口
接口的方式是我們把定時任務(wù)的信息放在數(shù)據(jù)庫中,程序從數(shù)據(jù)庫去拉取定時任務(wù)的信息如cron
表達式來實現(xiàn)實時修改生效等功能。
1. 首先在數(shù)據(jù)庫中創(chuàng)建一張用來記錄定時任務(wù)的表
CREATE TABLE `scheduled`?( ? `id` bigint NOT NULL AUTO_INCREMENT, ? `name` varchar(255) NULL, ? `cron` varchar(255) NULL, ? PRIMARY KEY (`id`) ) INSERT INTO `mydb`.`scheduled` (`id`, `name`, `cron`) VALUES (1, '定時任務(wù)1', '0/6 * * * * ?')
2. 在項目中引入mabatis-plus
和mysql
相應(yīng)的依賴包
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.32</version> </dependency>
3. 在application.yml
中進行連接數(shù)據(jù)庫相應(yīng)配置
spring: datasource: url: jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&serverTimeZone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
4. 定義查詢cron
表達式的mapper
mapper.CronMapper:
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface CronMapper { @Select("select cron from scheduled where id=#{id}") String getCron(Long id); }
5. 實現(xiàn)定時任務(wù)
import com.jk.mapper.CronMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; @Component @EnableScheduling //開啟定時任務(wù) public class ScheduleTask implements SchedulingConfigurer { @Autowired private CronMapper cronMapper; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask( //添加任務(wù)內(nèi)容 () -> process(), //設(shè)置執(zhí)行的周期 triggerContext -> { //查詢cron表達式 String cron = cronMapper.getCron(1L); if (cron.isEmpty()) { System.out.println("cron is null"); } return new CronTrigger(cron).nextExecutionTime(triggerContext); }); } private void process() { System.out.println("基于接口的定時任務(wù)"); } }
這種方式需要去實現(xiàn)SchedulingConfigurer
接口并重寫configureTasks
方法,然后設(shè)置任務(wù)內(nèi)容和執(zhí)行周期等,啟動測試就實現(xiàn)了基于接口的定時任務(wù),此時我們改動數(shù)據(jù)庫里的cron
表達式也會實時生效
三、多線程定時任務(wù)
但上面的方法定義的定時任務(wù)會有個問題,就是如果我一個定時任務(wù)里面執(zhí)行了復(fù)雜邏輯,導致本身執(zhí)行花的時間就已經(jīng)超過了定時任務(wù)間隔的時間怎么辦呢?這時候定時任務(wù)的執(zhí)行就會出現(xiàn)一定的問題,具體如下,我用線程睡眠的方式模擬處理復(fù)雜邏輯
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component @EnableScheduling //開啟定時任務(wù) public class ScheduleTask { @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式 public void testScheduleTask1() throws InterruptedException { System.out.println("執(zhí)行定時任務(wù)1 " + LocalDateTime.now()); Thread.sleep(10 * 1000); } @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式 public void testScheduleTask2() { System.out.println("執(zhí)行定時任務(wù)2 " + LocalDateTime.now()); } }
可以看到兩個任務(wù)的執(zhí)行時間都被影響了,和我們設(shè)置的5秒不對應(yīng)。此時就可以使用多線程定時任務(wù)
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component @EnableScheduling //開啟定時任務(wù) @EnableAsync //開啟多線程 public class ScheduleTask { @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式 @Async public void testScheduleTask1() throws InterruptedException { System.out.println("執(zhí)行定時任務(wù)1 " + LocalDateTime.now()); Thread.sleep(10 * 1000); } @Scheduled(cron = "0/5 * * * * ?") //定時任務(wù)注解+cron表達式 @Async public void testScheduleTask2() { System.out.println("執(zhí)行定時任務(wù)2 " + LocalDateTime.now()); } }
這樣多線程的定時任務(wù)就實現(xiàn)了,每個定時任務(wù)之間不會互相影響,定時任務(wù)執(zhí)行時間太長也不會影響。
這就是定時任務(wù)實現(xiàn)的幾種方式,對大家有幫助的話多多
點贊
、收藏
哦,感謝!
以上就是SpringBoot中實現(xiàn)定時任務(wù)的幾種方式的詳細內(nèi)容,更多關(guān)于SpringBoot 定時任務(wù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot定時任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實現(xiàn)方法
- SpringBoot中實現(xiàn)定時任務(wù)的4種方式詳解
- SpringBoot2 task scheduler 定時任務(wù)調(diào)度器四種方式
- SpringBoot下使用定時任務(wù)的方式全揭秘(6種)
- SpringBoot實現(xiàn)固定和動態(tài)定時任務(wù)的三種方法
- Springboot實現(xiàn)定時任務(wù)的4種方式舉例詳解
- SpringBoot實現(xiàn)定時任務(wù)的三種方式小結(jié)
- SpringBoot最新定時任務(wù)的7種實現(xiàn)方案
相關(guān)文章
Spring中@DependsOn注解的作用及實現(xiàn)原理解析
這篇文章主要介紹了Spring中@DependsOn注解的作用及實現(xiàn)原理解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解
這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09Spring Security的持久化用戶和授權(quán)實現(xiàn)方式
文章介紹了如何使用JdbcUserDetailsManager實現(xiàn)數(shù)據(jù)庫讀取用戶,并展示了如何配置SpringSecurity進行授權(quán)管理,通過創(chuàng)建數(shù)據(jù)庫表、配置數(shù)據(jù)庫連接和修改SecurityConfig,實現(xiàn)了用戶權(quán)限的控制2025-02-02java中catalina.home與catalina.base區(qū)別點整理
在本篇文章里小編給大家整理的是關(guān)于java項目中catalina.home與catalina.base區(qū)別點,需要的朋友們可以學習下。2020-02-02Spring AOP在web應(yīng)用中的使用方法實例
這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring AOP具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn))
本篇文章主要介紹了Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11