quartz的簡單使用、SpringBoot使用和自定義數(shù)據(jù)源集成方式
一、quartz的基本認(rèn)識(shí)
1. 什么是quartz
從quartz的官網(wǎng)可首頁可以看到對(duì)quartz的簡單介紹,下面翻譯成中文,大概就是這樣:
Quartz是一個(gè)功能豐富的開源作業(yè)調(diào)度庫,幾乎可以集成到任何Java應(yīng)用程序中——從最小的獨(dú)立應(yīng)用程序到最大的電子商務(wù)系統(tǒng)。
Quartz可用于創(chuàng)建簡單或復(fù)雜的時(shí)間表,以執(zhí)行數(shù)十個(gè)、數(shù)百個(gè)甚至數(shù)萬個(gè)作業(yè);其任務(wù)被定義為標(biāo)準(zhǔn)Java組件的作業(yè),這些組件可以執(zhí)行幾乎任何您可以對(duì)其進(jìn)行編程的操作。
Quartz調(diào)度器包括許多企業(yè)級(jí)功能,例如支持JTA事務(wù)和集群。
2. quartz的基本組件
從上圖可以看出,quartz主要包含了3個(gè)基本組件:Scheduler、Job和Trigger,其中, Job部分包含了組件Job、JobDetail和JobBuilder,Trigger部分包含了Trigger和TriggerBuilder。
一個(gè)設(shè)計(jì)良好的組件或者框架,必然會(huì)遵守組件的職責(zé)區(qū)分,單一職責(zé)原則是面向?qū)ο蟮囊粋€(gè)重要特性,不僅在編碼中需要注意,在組件或者框架的設(shè)計(jì)中也必然需要考慮到這一點(diǎn)。
首先是Job。
- Job從字面意思上理解,就是一項(xiàng)工作或者任務(wù),也就是定時(shí)任務(wù)的“任務(wù)”具體需要做什么,在quartz中,Job是一個(gè)接口,且只定義了一個(gè)方法execut(),把任務(wù)需要處理的邏輯寫在execute方法中。
- 同時(shí)我們需要注意的一點(diǎn)是,Job是無狀態(tài)的,也就是Job是可以復(fù)用的,那么Job需要的信息從哪里來呢?
- 答案是從JobDetail中獲取,JobDetail有一個(gè)重要的方法getJobDataMap(),調(diào)用這個(gè)方法可以獲取到任務(wù)需要的相關(guān)信息,當(dāng)然這些信息一般是創(chuàng)建Job的時(shí)候設(shè)定的。
- 現(xiàn)在引申出另外一個(gè)問題,那就是如何創(chuàng)建一個(gè)Job?
- 一想到創(chuàng)建,我們肯定會(huì)想到建造者模式,quartz就使用JobBuilder來創(chuàng)建Job,并設(shè)定Job需要的相關(guān)信息。
然后看Trigger。
- 我們定義完任務(wù)之后,那么什么時(shí)候觸發(fā)呢?
- 所以我們需要一個(gè)“任務(wù)觸發(fā)器”,來定時(shí)觸發(fā)Job。
- 比如:每多少分鐘執(zhí)行一次,或者根據(jù)cron表達(dá)式來定時(shí)執(zhí)行等等。
- 那么TriggerBuilder就是用來協(xié)助創(chuàng)建Trigger的類。
最后是Scheduler。
- 現(xiàn)在任務(wù)和觸發(fā)器都有了,那么誰來調(diào)度呢?
- 這時(shí)候就需要一個(gè)調(diào)度器了,調(diào)度器負(fù)責(zé)協(xié)調(diào)任務(wù)和觸發(fā)器,當(dāng)觸發(fā)任務(wù)時(shí),把上下文傳遞給具體的任務(wù),任務(wù)拿到上下文,執(zhí)行任務(wù)。
二、quartz實(shí)戰(zhàn)
1. quartz的基本使用
public class SimpleQuartzTest { public static void main(String[] args) throws Exception{ Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); JobDetail jobDetail = JobBuilder.newJob(MySimpleJob.class) .withIdentity("myJob", "myGroup") .usingJobData("customJobData", "Hello world!") .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "myGroup") .startNow() .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(10)) .build(); scheduler.scheduleJob(jobDetail, trigger); } }
public class MySimpleJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println(">>>>>>>>>>>>>>>>"); System.out.println("當(dāng)前時(shí)間是:" + LocalDateTime.now().toString()); System.out.println("MySimpleJob開始運(yùn)行了"); JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); System.out.println("從上下文中獲取到的數(shù)據(jù):customJobData: " + jobDataMap.getString("customJobData") ); System.out.println(">>>>>>>>>>>>>>>>"); System.out.println(); System.out.println(); } }
quartz的基本使用還是比較簡單的,此時(shí)任務(wù)信息是保存在內(nèi)存中的,如上面截圖所示。在實(shí)際項(xiàng)目中,我們基本不會(huì)使用這種保存在內(nèi)存中的方式,一個(gè)很簡單的原因是:如果服務(wù)down掉,任務(wù)執(zhí)行情況就消失了,這是不允許的。
所以接下來看使用使用數(shù)據(jù)庫保存任務(wù)信息的實(shí)現(xiàn)方式。
2. SpringBoot使用quartz
SpringBoot使用quartz方式實(shí)現(xiàn)也不復(fù)雜,我們使用的SpringBoot版本是2.6.3。
首先引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/quartz_test?useSSL=false&defualtTimeZone=UTC spring.datasource.username=root spring.datasource.password=root spring.quartz.job-store-type=jdbc spring.quartz.auto-startup=true spring.quartz.jdbc.initialize-schema=embedded spring.quartz.properties.org.quartz.scheduler.instanceName=MyScheduler spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate spring.quartz.properties.org.quartz.jobStore.isClustered=true spring.quartz.properties.org.quartz.jobStore.useProperties=false
啟動(dòng)項(xiàng)目,發(fā)現(xiàn)報(bào)錯(cuò):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler' defined in class path resource [org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.class]: Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: DataSource name not set.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:934) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.6.3.jar:2.6.3]
at com.example.springbootdemo.SpringbootdemoApplication.main(SpringbootdemoApplication.java:10) ~[classes/:na]
Caused by: org.quartz.SchedulerConfigException: DataSource name not set.
at org.quartz.impl.jdbcjobstore.JobStoreSupport.initialize(JobStoreSupport.java:643) ~[quartz-2.3.2.jar:na]
at org.quartz.impl.jdbcjobstore.JobStoreTX.initialize(JobStoreTX.java:57) ~[quartz-2.3.2.jar:na]
at org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1368) ~[quartz-2.3.2.jar:na]
at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1579) ~[quartz-2.3.2.jar:na]
at org.springframework.scheduling.quartz.SchedulerFactoryBean.createScheduler(SchedulerFactoryBean.java:679) ~[spring-context-support-5.3.15.jar:5.3.15]
at org.springframework.scheduling.quartz.SchedulerFactoryBean.prepareScheduler(SchedulerFactoryBean.java:616) ~[spring-context-support-5.3.15.jar:5.3.15]
at org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:504) ~[spring-context-support-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
... 16 common frames omitted
實(shí)際上在較舊版本的SpringBoot同樣的配置是不會(huì)出現(xiàn)這樣的問題的,根據(jù)出錯(cuò)信息查看相關(guān)的源碼,發(fā)現(xiàn)問題出在這里:
當(dāng)我們?cè)O(shè)置了屬性org.quartz.jobStore.class之后,即使我們配置了本地?cái)?shù)據(jù)源,也不會(huì)使用本地?cái)?shù)據(jù)源,而是我們之前配置的 org.quartz.impl.jdbcjobstore.JobStoreTX,在舊版本的SpringBoot是直接put,所以配置org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX并不會(huì)產(chǎn)生問題。
那么解決問題就很簡單了,在application.properties文件中把這一行刪除就可以了。
重新啟動(dòng)項(xiàng)目,發(fā)現(xiàn)現(xiàn)在quartz已經(jīng)使用了本地?cái)?shù)據(jù)源了。
3. SpringBoot使用自定義數(shù)據(jù)源時(shí),集成quartz
在實(shí)際項(xiàng)目中,我們很可能是使用自定義的數(shù)據(jù)源,而不是SpringBoot自動(dòng)生成的,那么在這種情況下,我們又應(yīng)該如何實(shí)現(xiàn)呢?
首先,我們不能引入spring-boot-starter-quartz依賴,但是以下依賴我們需要引入:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.3.15</version> </dependency>
接下來就是寫一個(gè)配置類,來生成一個(gè)SchedulerFactoryBean:
@Configuration public class QuartzConfig { @Autowired private DataSource dataSource; @Bean public SchedulerFactoryBean schedulerFactoryBean () { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setDataSource(dataSource); schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties")); return schedulerFactoryBean; } }
劃重點(diǎn):
不要在quartz.properties文件中配置:org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
然后啟動(dòng)項(xiàng)目,就可以啦。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
你必須得會(huì)的SpringBoot全局統(tǒng)一處理異常詳解
程序在運(yùn)行的過程中,不可避免會(huì)產(chǎn)生各種各樣的錯(cuò)誤,這個(gè)時(shí)候就需要進(jìn)行異常處理,本文主要為大家介紹了SpringBoot實(shí)現(xiàn)全局統(tǒng)一處理異常的方法,需要的可以參考一下2023-06-06Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
在使用Java Spring開發(fā)的時(shí)候,Mybatis算是對(duì)數(shù)據(jù)庫操作的利器了。這篇文章主要介紹了Mybatis分頁插件PageHelper的配置和使用方法,需要的朋友可以參考下2017-12-12Spring實(shí)戰(zhàn)之搜索Bean類操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之搜索Bean類操作,結(jié)合實(shí)例形式分析了Spring搜索Bean類的相關(guān)配置、接口實(shí)現(xiàn)與操作技巧,需要的朋友可以參考下2019-12-12java整數(shù)與byte數(shù)組的轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了java整數(shù)與byte數(shù)組的轉(zhuǎn)換實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07spring中bean id相同引發(fā)故障的分析與解決
最近在工作中遇到了關(guān)于bean id相同引發(fā)故障的問題,通過查找相關(guān)資料終于解決了,下面這篇文章主要給大家介紹了因?yàn)閟pring中bean id相同引發(fā)故障的分析與解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09