SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
前言
最近在項(xiàng)目中使用到定時(shí)任務(wù),之前一直都是使用Quartz 來(lái)實(shí)現(xiàn),最近看Spring 基礎(chǔ)發(fā)現(xiàn)其實(shí)Spring 提供 Spring Schedule 可以幫助我們實(shí)現(xiàn)簡(jiǎn)單的定時(shí)任務(wù)功能。
下面說(shuō)一下兩種方式在Spring Boot 項(xiàng)目中的使用。
Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)
Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)有兩種方式 1. 使用XML配置定時(shí)任務(wù), 2. 使用 @Scheduled 注解。 因?yàn)槭荢pring Boot 項(xiàng)目 可能盡量避免使用XML配置的形式,主要說(shuō)注解的形式.
Spring Schedule 提供三種形式的定時(shí)任務(wù):
固定等待時(shí)間 @Scheduled(fixedDelay = 時(shí)間間隔 )
@Component
public class ScheduleJobs {
public final static long SECOND = 1 * 1000;
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
@Scheduled(fixedDelay = SECOND * 2)
public void fixedDelayJob() throws InterruptedException {
TimeUnit.SECONDS.sleep(2);
System.out.println("[FixedDelayJob Execute]"+fdf.format(new Date()));
}
}
固定間隔時(shí)間 @Scheduled(fixedRate = 時(shí)間間隔 )
@Component
public class ScheduleJobs {
public final static long SECOND = 1 * 1000;
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
@Scheduled(fixedRate = SECOND * 4)
public void fixedRateJob() {
System.out.println("[FixedRateJob Execute]"+fdf.format(new Date()));
}
}
Corn表達(dá)式 @Scheduled(cron = Corn表達(dá)式)
@Component
public class ScheduleJobs {
public final static long SECOND = 1 * 1000;
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
@Scheduled(cron = "0/4 * * * * ?")
public void cronJob() {
System.out.println("[CronJob Execute]"+fdf.format(new Date()));
}
}
Spring Boot 整合 Quartz 實(shí)現(xiàn)定時(shí)任務(wù)
添加Maven依賴
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
Spring Boot 整合 Quartz
Spring 項(xiàng)目整合 Quartz 主要依靠添加 SchedulerFactoryBean 這個(gè) FactoryBean ,所以在maven 依賴中添加 spring-context-support 。
首先添加 QuartzConfig 類 來(lái)聲明相關(guān)Bean
@Configuration
public class QuartzConfig {
@Autowired
private SpringJobFactory springJobFactory;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(springJobFactory);
return schedulerFactoryBean;
}
@Bean
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}
這里我們需要注意 我注入了一個(gè) 自定義的JobFactory ,然后 把其設(shè)置為SchedulerFactoryBean 的 JobFactory。其目的是因?yàn)槲以诰唧w的Job 中 需要Spring 注入一些Service。
所以我們要自定義一個(gè)jobfactory, 讓其在具體job 類實(shí)例化時(shí) 使用Spring 的API 來(lái)進(jìn)行依賴注入。
SpringJobFactory 具體實(shí)現(xiàn):
@Component
public class SpringJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
具體使用 (摘取自項(xiàng)目代碼):
@Service
public class QuartzEventServiceImpl implements QuartzEventService {
private static final String JOB_GROUP = "event_job_group";
private static final String TRIGGER_GROUP = "event_trigger_group";
@Autowired
private Scheduler scheduler;
@Override
public void addQuartz(Event event) throws SchedulerException {
JSONObject eventData = JSONObject.parseObject(event.getEventData());
Date triggerDate = eventData.getDate("date");
JobDetail job = JobBuilder.newJob(EventJob.class).withIdentity(event.getId().toString(), JOB_GROUP).usingJobData(buildJobDateMap(event)).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(event.getId().toString(), TRIGGER_GROUP).startAt(triggerDate).build();
scheduler.scheduleJob(job, trigger);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的4種方式詳解
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式
- SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式
- SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)
- SpringBoot實(shí)現(xiàn)固定和動(dòng)態(tài)定時(shí)任務(wù)的三種方法
- Springboot實(shí)現(xiàn)定時(shí)任務(wù)的4種方式舉例詳解
- SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)
- SpringBoot最新定時(shí)任務(wù)的7種實(shí)現(xiàn)方案
相關(guān)文章
java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法示例
這篇文章主要介紹了java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法,涉及java針對(duì)字符的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,落子動(dòng)作的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
spring cloud gateway中如何讀取請(qǐng)求參數(shù)
這篇文章主要介紹了spring cloud gateway中如何讀取請(qǐng)求參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解spring cloud ouath2中的資源服務(wù)器
這篇文章主要介紹了spring cloud ouath2中的資源服務(wù)器的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
SpringCloud使用feign調(diào)用錯(cuò)誤的問題
這篇文章主要介紹了SpringCloud使用feign調(diào)用錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Springboot手動(dòng)連接庫(kù)并獲取指定表結(jié)構(gòu)的示例代碼
這篇文章主要介紹了Springboot手動(dòng)連接庫(kù)并獲取指定表結(jié)構(gòu)的示例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java根據(jù)開始時(shí)間和結(jié)束時(shí)間及周幾計(jì)算日期的示例代碼
在Java 7中,java.time包不存在,所以我們需要使用java.util.Calendar和java.util.Date類來(lái)實(shí)現(xiàn)類似的功能,這篇文章主要介紹了Java根據(jù)開始時(shí)間和結(jié)束時(shí)間及周幾計(jì)算出日期的示例代碼,需要的朋友可以參考下2024-06-06
SpringBoot頂層接口實(shí)現(xiàn)類注入項(xiàng)目的方法示例
本文主要介紹了SpringBoot頂層接口實(shí)現(xiàn)類注入項(xiàng)目的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Servlet和Spring?MVC的區(qū)別及使用說(shuō)明
這篇文章詳細(xì)介紹了Servlet和SpringMVC的基本概念、工作原理、功能對(duì)比和應(yīng)用場(chǎng)景,Servlet是JavaWeb開發(fā)的基礎(chǔ),而SpringMVC是一個(gè)基于Servlet的高級(jí)框架,提供了更強(qiáng)大的功能和易用性,文章通過(guò)定義、原理和示例代碼,幫助讀者理解這兩個(gè)技術(shù)的區(qū)別與聯(lián)系2025-01-01

