SpringBoot 2.0集成Quartz
前言
在学习这一部分的时候,在网上大概搜了40-50个博客文章,最终还是成功集成了,特此记下
一、依赖
因我使用的是SpringBoot2.0以上的版本所以依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
如果非2.0及以上版本依赖有所不同,请另行搜索
二、application.yml配置
spring:
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
#调度器实例名称
instanceName: clusteredScheduler
#调度器实例编号自动生成
instanceId: AUTO
jobStore:
#持久化方式配置
class: org.quartz.impl.jdbcjobstore.JobStoreTX
#持久化方式配置数据驱动,MySQL数据库
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#quartz相关数据表前缀名
tablePrefix: QRTZ_
#开启分布式部署
isClustered: false
#分布式节点有效性检查时间间隔,单位:毫秒
# clusterCheckinInterval: 10000
#配置是否使用
useProperties: false
threadPool:
#线程池实现类
class: org.quartz.simpl.SimpleThreadPool
#执行最大并发线程数量
threadCount: 10
#线程优先级
threadPriority: 5
#配置是否启动自动加载数据库内的定时任务,默认true
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构
#jdbc:
#initialize-schema: never
application.properties请自行转换
三、SchedulerConfig配置
package com.zm.conf;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class SchedulerConfig implements SchedulerFactoryBeanCustomizer {
@Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
// 延时启动,应用启动2秒后 ,定时器才开始启动
schedulerFactoryBean.setStartupDelay(2);
//设置是否在初始化后自动启动调度程序
schedulerFactoryBean.setAutoStartup(true);
//设置是否任意一个已定义的Job会覆盖现在的Job。默