十、springboot整合quartz
- 前面的是原生的quartz,为了让了解原理。
- quartz整合到springboot上就可以轻量级(就不用new很多的调度器实例)
1 准备相应的集群和一些配置文件
- springboot中的节点是不通信的,是通过数据库来记录集群的信息,不同的集群来访问同一个数据库。
1.1 quartz官方提供了一张表,导入即可
- 资源在博主中心
1.2 设置springboot的配置文件
application.properties
server.context-path=/quartz-cluster2
#\u7AEF\u53E3\u53F7
server.port=8082
# \u6570\u636E\u6E90\u914D\u7F6E
spring.datasource.url=jdbc:mysql://localhost:3306/tuling?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-active=1000
spring.datasource.max-idle=20
spring.datasource.min-idle=5
spring.datasource.initial-size=10
1.3 设置quartz本身的配置文件
spring-quartz.properties
#============================================================================
# 配置JobStore
#============================================================================
# JobDataMaps是否都为String类型,默认false
org.quartz.jobStore.useProperties=false
# 表的前缀,默认QRTZ_
org.quartz.jobStore.tablePrefix = QRTZ_
# 是否加入集群
org.quartz.jobStore.isClustered = true
# 调度实例失效的检查时间间隔 ms
org.quartz.jobStore.clusterCheckinInterval = 5000
# 当设置为“true”时,此属性告诉Quartz 在非托管JDBC连接上调用setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)。
org.quartz.jobStore.txIsolationLevelReadCommitted = true
# 数据保存方式为数据库持久化
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
# 数据库代理类,一般org.quartz.impl.jdbcjobstore.StdJDBCDelegate可以满足大部分数据库
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#============================================================================
# Scheduler 调度器属性配置
#============================================================================
# 调度标识名 集群中每一个实例都必须使用相同的名称
org.quartz.scheduler.instanceName = ClusterQuartz
# ID设置为自动获取 每一个必须不同
org.quartz.scheduler.instanceId= AUTO
#============================================================================
# 配置ThreadPool
#============================================================================
# 线程池的实现类(一般使用SimpleThreadPool即可满足几乎所有用户的需求)
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
# 指定线程数,一般设置为1-100直接的整数,根据系统资源配置
org.quartz.threadPool.threadCount = 5
# 设置线程的优先级(可以是Thread.MIN_PRIORITY(即1)和Thread.MAX_PRIORITY(这是10)之间的任何int 。默认值为Thread.NORM_PRIORITY(5)。)
org.quartz.threadPool.threadPriority = 5
2 创建任务类
2.1 创建QuartzJob类
- 继承springboot封装好的QuartzJobBean或者实现Job接口
- 这里我们用【继承springboot封装好的QuartzJobBean】
package com.tuling.bootquartz;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class QuartzJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
Thread.sleep(2000);
System.out.println(context.getScheduler().getSchedulerInstanceId());
System.out.println("taskname="+context.getJobDetail().getKey().getName());
System.out.println("执行时间="+new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 定义调度器实例
- 由于调度器每次用的都是一个,所以通过IOC,将调度器配置成一个Bean
3.1 创建SchedulerConfig配置类
- 加@configuration注解
- @configuration用于定义配置类
- 创建出Scheduler对象,用来配置,这里配置了一个名字
3. 注入数据源(因为调度器之间的节点不通信),通过数据库来保持这些节点的通信。
4. 读配置文件,设置线程池
- 通过一个方法来读取properties来返回一个properties
- 通过PropertiesFactoryBean来获取
- setLocation:把配置文件的路径给过去
- 调取afterPropertiesSet方法:才能读取文件
- 通过PropertiesFactoryBean来获取
5. 线程池配置
- 也是通过定义一个@Bean的方式,将线程池定义出来
- 设置调度器延迟执行还是立即执行
- 就可以调用schedulerFactoryBean()这个方法来获取Scheduler实例了。