Springboot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例
Springboot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例
Quartz是一款功能強(qiáng)大的任務(wù)調(diào)度器,Quartz有兩個(gè)比較核心的組件:Job 和 Trigger。
Quartz的三個(gè)基本要素
- Scheduler:調(diào)度器,所有的調(diào)度都是由它來控制。
- Trigger:觸發(fā)器,決定什么時(shí)候執(zhí)行任務(wù)
- JobDetail & Job:JobDetail定義的是任務(wù)數(shù)據(jù),而真正執(zhí)行邏輯是在Job中。任務(wù)是有可能并發(fā)執(zhí)行的,若Scheduler直接使用Job,就會(huì)存在對(duì)同一個(gè)Job實(shí)例并發(fā)訪問的問題。而JobDetail & Job方式,Scheduler都會(huì)根據(jù)JobDetail創(chuàng)建一個(gè)新的Job實(shí)例,這樣就可以規(guī)避并發(fā)訪問問題
Spring Boot自帶定時(shí)任務(wù),默認(rèn)是單線程
1. 使用spring boot自帶定時(shí)任務(wù)無需添加相關(guān)依賴包
2. 在啟動(dòng)類上添加 @EnableScheduling 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class O2o2Application {
public static void main(String[] args) {
SpringApplication.run(O2o2Application.class, args);
System.out.println("項(xiàng)目啟動(dòng)成功!");
}
}3. 在方法上標(biāo)注 @Scheduled 注解,指定該方法作為定時(shí)任務(wù)運(yùn)行的方法。并通過cron屬性設(shè)置cron表達(dá)式,來指定定時(shí)任務(wù)的執(zhí)行時(shí)間
import com.cd.o2o2.dao.ProductSellDailyDao;
import com.cd.o2o2.service.ProductSellDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service("productSellDailyService")
public class ProductSellDailyServiceImpl implements ProductSellDailyService {
@Override
@Scheduled(cron = "0/2 * * * * ?") //定時(shí)任務(wù)運(yùn)行的方法
public void demo1() {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dataFormat.format(new Date()) + " Quartz Running!");
}
@Override
@Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)運(yùn)行的方法
public void demo2() {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("\n當(dāng)前執(zhí)行時(shí)間: " + dataFormat.format(new Date()) + "\n");
}
}啟動(dòng)SpringBoot,查看控制臺(tái)打印信息! demo1()每2秒定時(shí)運(yùn)行一次,demo2()每5秒定時(shí)運(yùn)行一次 
如果不想使用Spring Boot自帶的定時(shí)任務(wù),則需要添加quartz相關(guān)依賴包
pom.xml
<!--定時(shí)組件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>Quartz配置類(Job的定義有兩種方式,分別為JobDetailFactoryBean和MethodInvokingJobDetailFactoryBean,下面使用MethodInvokingJobDetailFactoryBean類來定義job)
package com.cd.o2o2.config.quartz;
import com.cd.o2o2.service.ProductSellDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration //標(biāo)注@Configuration的類,相當(dāng)于一個(gè)xml配置文件
public class QuartzConfiguration {
@Autowired
private ProductSellDailyService productSellDailyService;
@Autowired
private MethodInvokingJobDetailFactoryBean jobDetailFactory;
@Autowired
private CronTriggerFactoryBean cronTriggerFactory;
/**
* 配置jobDetail作業(yè)類
* @return
*/
@Bean(name = "jobDetailFactory") //標(biāo)注@Bean后表明返回對(duì)象會(huì)被當(dāng)做一個(gè)Bean注冊(cè)到Spring IoC容器
public MethodInvokingJobDetailFactoryBean createJobDetail(){
MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
//指定任務(wù)名稱
jobDetailFactoryBean.setName("product_sell_daily_job");
//指定組名稱
jobDetailFactoryBean.setGroup("job_product_sell_daily_group");
/*
對(duì)于同一個(gè)JobDetail,當(dāng)指定多個(gè)trigger時(shí),很可能第一個(gè)job完成之前,第二個(gè)job就開始了
指定concurrent屬性為false,多個(gè)job就會(huì)串行執(zhí)行,而不會(huì)并發(fā)執(zhí)行,即第一個(gè)job完成前,第二個(gè)job不會(huì)開啟
*/
jobDetailFactoryBean.setConcurrent(false);
//指定具體的job任務(wù)類
jobDetailFactoryBean.setTargetObject(productSellDailyService);
//指定運(yùn)行任務(wù)的方法
jobDetailFactoryBean.setTargetMethod("dailyCalculate");
return jobDetailFactoryBean;
}
/**
* 配置cronTrigger觸發(fā)器(作業(yè)調(diào)度的方式)
* @return
*/
@Bean(name = "cronTriggerFactory")
public CronTriggerFactoryBean createProductSellDailyTrigger(){
CronTriggerFactoryBean triggerFactory = new CronTriggerFactoryBean();
//指定trigger的名稱
triggerFactory.setName("product_sell_daily_trigger");
//指定trigger的組名
triggerFactory.setGroup("job_product_sell_daily_group");
//指定trigger綁定的job
triggerFactory.setJobDetail(jobDetailFactory.getObject());
//設(shè)置cron表達(dá)式,每天凌晨定時(shí)運(yùn)行(通過在線Cron表達(dá)式生成器來生成)
triggerFactory.setCronExpression("0/2 * * * * ?");
return triggerFactory;
}
/**
* 配置scheduler調(diào)度工廠
* @return
*/
@Bean
public SchedulerFactoryBean createScheduler(){
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
//綁定cronTrigger
schedulerFactory.setTriggers(cronTriggerFactory.getObject());
return schedulerFactory;
}
}dailyCalculate()方法,是定時(shí)任務(wù)運(yùn)行的方法
package com.cd.o2o2.service.impl;
import com.cd.o2o2.service.ProductSellDailyService;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service("productSellDailyService")
public class ProductSellDailyServiceImpl implements ProductSellDailyService {
/**
* 定時(shí)任務(wù)運(yùn)行的方法
*/
@Override
public void dailyCalculate() {
System.out.println(new Date()+": Quartz Running!");
}
}啟動(dòng)SpringBoot,查看控制臺(tái)打印信息!dailyCalculate()方法每2秒定時(shí)運(yùn)行一次

Cron表達(dá)式示例

到此這篇關(guān)于Springboot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例的文章就介紹到這了,更多相關(guān)Springboot定時(shí)任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中關(guān)于char類型變量能夠輸出中文的問題
這篇文章主要介紹了Java中關(guān)于char類型變量能夠輸出中文的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理
在本篇文章里小編給各位整理了關(guān)于Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題相關(guān)內(nèi)容,有興趣的朋友們跟著參考學(xué)習(xí)下。2019-07-07
如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean詳解
這篇文章主要給大家介紹了關(guān)于如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
什么情況下會(huì)出現(xiàn)java.io.IOException?:?Broken?pipe這個(gè)錯(cuò)誤以及解決辦法
這篇文章主要介紹了什么情況下會(huì)出現(xiàn)java.io.IOException?:?Broken?pipe這個(gè)錯(cuò)誤以及解決辦法的相關(guān)資料,這個(gè)錯(cuò)誤表示通信另一端已關(guān)閉連接,常發(fā)生在客戶端關(guān)閉連接、網(wǎng)絡(luò)超時(shí)或資源不足等情況,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10

