Quartz分布式学习指南

405 阅读2分钟

一、简介


任务调度器

二、优势特性


  • 任务持久化:所有调度信息会存储到数据库中,即使应用程序停止运行,而当你重新启动时,调度信息还在,不会丢失
  • 集群+分布式
    • 若一个节点无法完成任务,则该任务会被集群中拥有相同任务属性的节点取代执行
    • 通过触发器的类别来识别不同的任务,在不同的节点定义相同的触发器的类别,这样在集群下能稳定的运行,一个节点无法完成的任务,会被集群中拥有相同的任务的节点取代执行
    • 当相同的任务定时在一个时间点,在那个时间点,不会被两个节点同时执行

三、快速开始


  1. 添加maven依赖

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>2.2.3</version>
    </dependency> 
    
  2. 创建数据库并初始化表结构 www.quartz-scheduler.org/下载最新版Quartz(此处使用2.2.3)后解压,docs/dbTable下找到符合自己需要的脚本(此处使用tables_mysql_innodb.sql

* `QRTZ_JOB_DETAILS:job信息`
* `QRTZ_JOB_LISTENERS :存储有关已配置的 JobListener的信息`
* `QRTZ_TRIGGERS:triggers信息,包括重复次数,间隔,以及已触的次数`
* `QRTZ_CRON_TRIGGERS:cron信息`
* `QRTZ_FIRED_TRIGGERS :存储程序的悲观锁的信息`
* `QRTZ_BLOG_TRIGGERS :Trigger作为Blob类型存储(用于 Quartz 用户用JDBC创建他们自己定制的 Trigger 类型,JobStore并不知道如何存储实例的时候)   `
* `QRTZ_TRIGGER_LISTENERS :存储已配置的 TriggerListener的信息`
* `QRTZ_SCHEDULER_STATE:集群中note信息`
* `QRTZ_CALENDARS:以Blob类型存储Quartz的Calendar信息`
* `QRTZ_LOCKS :存储程序的悲观锁的信息(假如使用了悲观锁)`
  1. 添加配置文件quartz.properties
# Configure Main Scheduler Properties
org.quartz.scheduler.instanceName=SsmScheduler
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

# Configure JobStore
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.misfireThreshold=60000

org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=5000
org.quartz.jobStore.txIsolationLevelReadCommitted=true

# Change this to match your DB vendor
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX     
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate

# Configure Datasource
quartz.dataSource.driver=com.mysql.jdbc.Driver
quartz.dataSource.url=jdbc:mysql://localhost:3306/T_QZ
quartz.dataSource.username=root
quartz.dataSource.password=root
  1. 添加spring配置文件context-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<util:properties id="quartz" location="classpath:quartz.properties"/>
<bean id="qzDatasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="#{quartz['quartz.dataSource.driver']}"/>
<property name="url" value="#{quartz['quartz.dataSource.url']}"/>
<property name="username" value="#{quartz['quartz.dataSource.username']}"/>
<property name="password" value="#{quartz['quartz.dataSource.password']}"/>
</bean>
<bean name="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="15"/>
<property name="maxPoolSize" value="25"/>
<property name="queueCapacity" value="100"/>
</bean>
<bean name="quartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="dataSource" ref="qzDatasource"/>
<property name="taskExecutor" ref="taskExecutor"/>
</bean>
</beans>