SpringBoot中實現(xiàn)定時任務的幾種方式
一、基于注解
這種方式很簡單,主要就是先@EnableScheduling開啟定時任務功能,然后在相應的方法上添加@Scheduled()中間寫上相應的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 //開啟定時任務
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
public void testScheduleTask() {
System.out.println("執(zhí)行定時任務" + 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)了基本的定時任務功能,但是如果我們修改了cron表達式,需要重啟整個應用才能生效,不是很方便。想要實現(xiàn)修改cron表達式就生效就需要用到接口的方式來實現(xiàn)定時任務。
二、基于接口
接口的方式是我們把定時任務的信息放在數(shù)據(jù)庫中,程序從數(shù)據(jù)庫去拉取定時任務的信息如cron表達式來實現(xiàn)實時修改生效等功能。
1. 首先在數(shù)據(jù)庫中創(chuàng)建一張用來記錄定時任務的表
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, '定時任務1', '0/6 * * * * ?')
2. 在項目中引入mabatis-plus和mysql相應的依賴包
<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ù)庫相應配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&serverTimeZone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver4. 定義查詢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)定時任務
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 //開啟定時任務
public class ScheduleTask implements SchedulingConfigurer {
@Autowired
private CronMapper cronMapper;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(
//添加任務內(nèi)容
() -> process(),
//設置執(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("基于接口的定時任務");
}
}這種方式需要去實現(xiàn)SchedulingConfigurer接口并重寫configureTasks方法,然后設置任務內(nèi)容和執(zhí)行周期等,啟動測試就實現(xiàn)了基于接口的定時任務,此時我們改動數(shù)據(jù)庫里的cron表達式也會實時生效

三、多線程定時任務
但上面的方法定義的定時任務會有個問題,就是如果我一個定時任務里面執(zhí)行了復雜邏輯,導致本身執(zhí)行花的時間就已經(jīng)超過了定時任務間隔的時間怎么辦呢?這時候定時任務的執(zhí)行就會出現(xiàn)一定的問題,具體如下,我用線程睡眠的方式模擬處理復雜邏輯
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling //開啟定時任務
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
public void testScheduleTask1() throws InterruptedException {
System.out.println("執(zhí)行定時任務1 " + LocalDateTime.now());
Thread.sleep(10 * 1000);
}
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
public void testScheduleTask2() {
System.out.println("執(zhí)行定時任務2 " + LocalDateTime.now());
}
}
可以看到兩個任務的執(zhí)行時間都被影響了,和我們設置的5秒不對應。此時就可以使用多線程定時任務
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 //開啟定時任務
@EnableAsync //開啟多線程
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
@Async
public void testScheduleTask1() throws InterruptedException {
System.out.println("執(zhí)行定時任務1 " + LocalDateTime.now());
Thread.sleep(10 * 1000);
}
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
@Async
public void testScheduleTask2() {
System.out.println("執(zhí)行定時任務2 " + LocalDateTime.now());
}
}
這樣多線程的定時任務就實現(xiàn)了,每個定時任務之間不會互相影響,定時任務執(zhí)行時間太長也不會影響。
這就是定時任務實現(xiàn)的幾種方式,對大家有幫助的話多多
點贊、收藏哦,感謝!
以上就是SpringBoot中實現(xiàn)定時任務的幾種方式的詳細內(nèi)容,更多關于SpringBoot 定時任務的資料請關注腳本之家其它相關文章!
相關文章
Spring中@DependsOn注解的作用及實現(xiàn)原理解析
這篇文章主要介紹了Spring中@DependsOn注解的作用及實現(xiàn)原理解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解
這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
Spring Security的持久化用戶和授權實現(xiàn)方式
文章介紹了如何使用JdbcUserDetailsManager實現(xiàn)數(shù)據(jù)庫讀取用戶,并展示了如何配置SpringSecurity進行授權管理,通過創(chuàng)建數(shù)據(jù)庫表、配置數(shù)據(jù)庫連接和修改SecurityConfig,實現(xiàn)了用戶權限的控制2025-02-02
java中catalina.home與catalina.base區(qū)別點整理
在本篇文章里小編給大家整理的是關于java項目中catalina.home與catalina.base區(qū)別點,需要的朋友們可以學習下。2020-02-02
Java實現(xiàn)在線預覽的示例代碼(openOffice實現(xiàn))
本篇文章主要介紹了Java實現(xiàn)在線預覽的示例代碼(openOffice實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

