首先非常感谢原作者恒宇少年 ,写了一遍好文章。找了很多文章终于找到比较有用有价值的高质量博文。
原贴地址:https://www.jianshu.com/p/056281e057b3
以下引用原文章。
在新版本的SpringBoot2.0
发布后,急迫尝鲜的我将相关的项目已经更换为最新版本,在SpringBoot
源码GitHub
看到更新日志,表明了针对Quartz
新版本进行了 AutoConfiguration
自动化配置,省去了很多繁琐的配置。
官网更新日志
Auto-configuration support is now include for the Quartz Scheduler. We’ve also added a new
spring-boot-starter-quartz
starter POM.
You can use in-memoryJobStores
, or a full JDBC-based store. AllJobDetail
,Calendar
andTrigger
beans from your Spring application context will be automatically registered with theScheduler
.
For more details read the new "Quartz Scheduler" section of the reference documentation.
SpringBoot2.0
版本集成了Quartz2.3.0
官网最新版本。
本章目标
使用SpringBoot2.0
新特性完成Quartz
自动化配置。
SpringBoot 企业级核心技术学习专题
专题 | 专题名称 | 专题描述 |
---|---|---|
001 | Spring Boot 核心技术 | 讲解SpringBoot一些企业级层面的核心组件 |
002 | Spring Boot 核心技术章节源码 | Spring Boot 核心技术简书每一篇文章码云对应源码 |
003 | Spring Cloud 核心技术 | 对Spring Cloud核心技术全面讲解 |
004 | Spring Cloud 核心技术章节源码 | Spring Cloud 核心技术简书每一篇文章对应源码 |
005 | QueryDSL 核心技术 | 全面讲解QueryDSL核心技术以及基于SpringBoot整合SpringDataJPA |
006 | SpringDataJPA 核心技术 | 全面讲解SpringDataJPA核心技术 |
007 | SpringBoot核心技术学习目录 | SpringBoot系统的学习目录,敬请关注点赞!!! |
构建项目
在前面章节第四十章:基于SpringBoot & Quartz完成定时任务分布式多节点负载持久化内我们已经通过添加配置的方式完成集成,为了本章的方便直接复制之前的项目,在基础上进行修改。
打开pom.xml
配置文件,SpringBoot
为我们提供了对应的依赖,我们将之前的quartz
相关依赖删除,替换为spring-boot-starter-quartz
,如下所示:
<!--quartz相关依赖-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency>
>>>>替换为:>>>>
<!--quartz依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
......
删除QuartzConfiguration配置类
在之前章节我们使用QuartzConfiguration
配置类来完成了Quartz
需要的一系列配置,如:JobFactory
、SchedulerFactoryBean
等,在我们添加spring-boot-starter-quartz
依赖后就不需要主动声明工厂类,因为spring-boot-starter-quartz
已经为我们自动化配置好了。
自动化配置源码
我们找到Idea
的External Libraries
并且展开spring-boot-autoconfigure-2.0.0.RELEASE.jar
,找到org.springframework.boot.autoconfigure.quartz
,该目录就是SpringBoot
为我们提供的Quartz
自动化配置源码实现,在该目录下有如下所示几个类:
AutowireCapableBeanJobFactory
该类替代了我们之前在QuartzConfiguration
配置类的AutowiringSpringBeanJobFactory
内部类实现,主要作用是我们自定义的QuartzJobBean
子类被Spring IOC
进行托管,可以在定时任务类内使用注入任意被Spring IOC
托管的类。JobStoreType
该类是一个枚举类型,定义了对应application.yml
、application.properties
文件内spring.quartz.job-store-type
配置,其目的是配置quartz
任务的数据存储方式,分别为:MEMORY(内存方式:默认
)、JDBC(数据库方式)。QuartzAutoConfiguration
该类是自动配置的主类,内部配置了SchedulerFactoryBean
以及JdbcStoreTypeConfiguration
,使用QuartzProperties
作为属性自动化配置条件。QuartzDataSourceInitializer
该类主要用于数据源初始化后的一些操作,根据不同平台类型的数据库进行选择不同的数据库脚本。QuartzProperties
该类对应了spring.quartz
在application.yml
、application.properties
文件内开头的相关配置。SchedulerFactoryBeanCustomizer
这是一个接口,我们实现该接口后并且将实现类使用Spring IOC
托管,可以完成SchedulerFactoryBean
的个性化设置,这里的设置完全可以对SchedulerFactoryBean
做出全部的设置变更。
spring.quartz配置
看到QuartzAutoConfiguration
类源码,我们知道了,想要使用自动化配置,需要满足QuartzProperties
属性配置类的初始化,所以我们需要再application.yml
、application.properties
配置文件内添加对应的配置信息,如下所示:
spring:
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
instanceName: clusteredScheduler
instanceId: AUTO
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构
#jdbc:
#initialize-schema: never
spring.quartz.properties
该配置其实代替了之前的quartz.properties
,我们把之前quartz.properties
配置文件内的所有配置转换成YUML
风格,对应的添加在该配置下即可,在QuartzAutoConfiguration
类内,会自动调用SchedulerFactoryBean
的setQuartzProperties
方法,把sp