springBoot-Quartz快速入门

springBoot-Quartz快速入门

开发环境

  • JDK1.8、Maven、Idea

技术栈

  • SpringBoot 2.1.6、quartz 2.3.0等

数据库准备

说明:数据库脚本由Quartz插件包中提供
下载地址:http://www.quartz-scheduler.org/downloads/
image

Quartz配置:spring boot 2.x 已集成Quartz,无需自己配置

spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.scheduler.instanceName=clusteredScheduler
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
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.tablePrefix=QRTZ_
spring.quartz.properties.org.quartz.jobStore.isClustered=true
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=10000
spring.quartz.properties.org.quartz.jobStore.useProperties=false
spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
spring.quartz.properties.org.quartz.threadPool.threadCount=10
spring.quartz.properties.org.quartz.threadPool.threadPriority=5
spring.quartz.properties.org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

集群测试

  • 打开集群配置
spring.quartz.properties.org.quartz.jobStore.isClustered = true
  • 设置集群检查间隔20s
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval = 2000

本地跑两个项目,分别设置不同的端口8081和8082,启动成功以后,会发现只有一个任务在跑,然后杀掉在跑的任务,你会发现另一个项目会检测到集群中的一个任务挂了,然后接管任务。

扩展

  • Quartz API核心接口有:
    • Scheduler – 与scheduler交互的主要API;
    • Job – 你通过scheduler执行任务,你的任务类需要实现的接口;
    • JobDetail – 定义Job的实例;
    • Trigger – 触发Job的执行;
    • JobBuilder – 定义和创建JobDetail实例的接口;
    • TriggerBuilder – 定义和创建Trigger实例的接口;
posted @ 2021-07-09 14:23  Joy丨windy  阅读(162)  评论(0编辑  收藏  举报