Quartz将定时任务持久化到数据库

Posted ngulc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz将定时任务持久化到数据库相关的知识,希望对你有一定的参考价值。

  之前的文章所做的demo是将定时任务的信息保存在内存中的,见以下配置

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

  如果,我们需要在系统意外(或非意外)重新启动后,仍保留定时任务信息,可以使用数据库存储定时任务信息。

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

   存储在mysql中:

  由于需要连接MySQL数据库,需要加上数据库的JDBC驱动,这里以pom形式下载,也可以直接引入包

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>

   然后需要在数据库中建一些Quartz的表,MySQL的建、删表脚本如下。如果你使用的是其他数据库,可以下载Quartz的distribution,在\\docs\\dbTables下。

  1 #
  2 # Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar
  3 #
  4 # PLEASE consider using mysql with innodb tables to avoid locking issues
  5 #
  6 # In your Quartz properties file, you\'ll need to set 
  7 # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
  8 #
  9 
 10 DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS;
 11 DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;
 12 DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE;
 13 DROP TABLE IF EXISTS QRTZ_LOCKS;
 14 DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS;
 15 DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS;
 16 DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS;
 17 DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS;
 18 DROP TABLE IF EXISTS QRTZ_TRIGGERS;
 19 DROP TABLE IF EXISTS QRTZ_JOB_DETAILS;
 20 DROP TABLE IF EXISTS QRTZ_CALENDARS;
 21 
 22 
 23 CREATE TABLE QRTZ_JOB_DETAILS
 24   (
 25     SCHED_NAME VARCHAR(120) NOT NULL,
 26     JOB_NAME  VARCHAR(200) NOT NULL,
 27     JOB_GROUP VARCHAR(200) NOT NULL,
 28     DESCRIPTION VARCHAR(250) NULL,
 29     JOB_CLASS_NAME   VARCHAR(250) NOT NULL,
 30     IS_DURABLE VARCHAR(1) NOT NULL,
 31     IS_NONCONCURRENT VARCHAR(1) NOT NULL,
 32     IS_UPDATE_DATA VARCHAR(1) NOT NULL,
 33     REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
 34     JOB_DATA BLOB NULL,
 35     PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
 36 );
 37 
 38 CREATE TABLE QRTZ_TRIGGERS
 39   (
 40     SCHED_NAME VARCHAR(120) NOT NULL,
 41     TRIGGER_NAME VARCHAR(200) NOT NULL,
 42     TRIGGER_GROUP VARCHAR(200) NOT NULL,
 43     JOB_NAME  VARCHAR(200) NOT NULL,
 44     JOB_GROUP VARCHAR(200) NOT NULL,
 45     DESCRIPTION VARCHAR(250) NULL,
 46     NEXT_FIRE_TIME BIGINT(13) NULL,
 47     PREV_FIRE_TIME BIGINT(13) NULL,
 48     PRIORITY INTEGER NULL,
 49     TRIGGER_STATE VARCHAR(16) NOT NULL,
 50     TRIGGER_TYPE VARCHAR(8) NOT NULL,
 51     START_TIME BIGINT(13) NOT NULL,
 52     END_TIME BIGINT(13) NULL,
 53     CALENDAR_NAME VARCHAR(200) NULL,
 54     MISFIRE_INSTR SMALLINT(2) NULL,
 55     JOB_DATA BLOB NULL,
 56     PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
 57     FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
 58         REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)
 59 );
 60 
 61 CREATE TABLE QRTZ_SIMPLE_TRIGGERS
 62   (
 63     SCHED_NAME VARCHAR(120) NOT NULL,
 64     TRIGGER_NAME VARCHAR(200) NOT NULL,
 65     TRIGGER_GROUP VARCHAR(200) NOT NULL,
 66     REPEAT_COUNT BIGINT(7) NOT NULL,
 67     REPEAT_INTERVAL BIGINT(12) NOT NULL,
 68     TIMES_TRIGGERED BIGINT(10) NOT NULL,
 69     PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
 70     FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
 71         REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
 72 );
 73 
 74 CREATE TABLE QRTZ_CRON_TRIGGERS
 75   (
 76     SCHED_NAME VARCHAR(120) NOT NULL,
 77     TRIGGER_NAME VARCHAR(200) NOT NULL,
 78     TRIGGER_GROUP VARCHAR(200) NOT NULL,
 79     CRON_EXPRESSION VARCHAR(200) NOT NULL,
 80     TIME_ZONE_ID VARCHAR(80),
 81     PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
 82     FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
 83         REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
 84 );
 85 
 86 CREATE TABLE QRTZ_SIMPROP_TRIGGERS
 87   (          
 88     SCHED_NAME VARCHAR(120) NOT NULL,
 89     TRIGGER_NAME VARCHAR(200) NOT NULL,
 90     TRIGGER_GROUP VARCHAR(200) NOT NULL,
 91     STR_PROP_1 VARCHAR(512) NULL,
 92     STR_PROP_2 VARCHAR(512) NULL,
 93     STR_PROP_3 VARCHAR(512) NULL,
 94     INT_PROP_1 INT NULL,
 95     INT_PROP_2 INT NULL,
 96     LONG_PROP_1 BIGINT NULL,
 97     LONG_PROP_2 BIGINT NULL,
 98     DEC_PROP_1 NUMERIC(13,4) NULL,
 99     DEC_PROP_2 NUMERIC(13,4) NULL,
100     BOOL_PROP_1 VARCHAR(1) NULL,
101     BOOL_PROP_2 VARCHAR(1) NULL,
102     PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
103     FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) 
104     REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
105 );
106 
107 CREATE TABLE QRTZ_BLOB_TRIGGERS
108   (
109     SCHED_NAME VARCHAR(120) NOT NULL,
110     TRIGGER_NAME VARCHAR(200) NOT NULL,
111     TRIGGER_GROUP VARCHAR(200) NOT NULL,
112     BLOB_DATA BLOB NULL,
113     PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
114     FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
115         REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
116 );
117 
118 CREATE TABLE QRTZ_CALENDARS
119   (
120     SCHED_NAME VARCHAR(120) NOT NULL,
121     CALENDAR_NAME  VARCHAR(200) NOT NULL,
122     CALENDAR BLOB NOT NULL,
123     PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)
124 );
125 
126 CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS
127   (
128     SCHED_NAME VARCHAR(120) NOT NULL,
129     TRIGGER_GROUP  VARCHAR(200) NOT NULL, 
130     PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)
131 );
132 
133 CREATE TABLE QRTZ_FIRED_TRIGGERS
134   (
135     SCHED_NAME VARCHAR(120) NOT NULL,
136     ENTRY_ID VARCHAR(95) NOT NULL,
137     TRIGGER_NAME VARCHAR(200) NOT NULL,
138     TRIGGER_GROUP VARCHAR(200) NOT NULL,
139     INSTANCE_NAME VARCHAR(200) NOT NULL,
140     FIRED_TIME BIGINT(13) NOT NULL,
141     SCHED_TIME BIGINT(13) NOT NULL,
142     PRIORITY INTEGER NOT NULL,
143     STATE VARCHAR(16) NOT NULL,
144     JOB_NAME VARCHAR(200) NULL,
145     JOB_GROUP VARCHAR(200) NULL,
146     IS_NONCONCURRENT VARCHAR(1) NULL,
147     REQUESTS_RECOVERY VARCHAR(1) NULL,
148     PRIMARY KEY (SCHED_NAME,ENTRY_ID)
149 );
150 
151 CREATE TABLE QRTZ_SCHEDULER_STATE
152   (
153     SCHED_NAME VARCHAR(120) NOT NULL,
154     INSTANCE_NAME VARCHAR(200) NOT NULL,
155     LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
156     CHECKIN_INTERVAL BIGINT(13) NOT NULL,
157     PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)
158 );
159 
160 CREATE TABLE QRTZ_LOCKS
161   (
162     SCHED_NAME VARCHAR(120) NOT NULL,
163     LOCK_NAME  VARCHAR(40) NOT NULL, 
164     PRIMARY KEY (SCHED_NAME,LOCK_NAME)
165 );
166 
167 
168 commit;
View Code

  这个脚本除了给出建、删表语句,也给出了org.quartz.jobStore.driverDelegateClass配置,此项配置到quartz.properties中

  注:你可能也注意到了,这些表都是以QRTZ_为前缀的,这是默认的前缀。如果你需要用到其他前缀(个性化需求,或需要配置多个quartz实例),可以在以下项配置(在quartz.properties中)

org.quartz.jobStore.tablePrefix = QRTZ_

 

   最主要的修改是quartz.properties

 1 org.quartz.scheduler.instanceName = MyScheduler
 2 org.quartz.threadPool.threadCount = 3
 3 org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
 4 org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
 5 org.quartz.jobStore.tablePrefix = QRTZ_
 6 org.quartz.jobStore.dataSource = myDS
 7 
 8 org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
 9 org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/ll?characterEncoding=utf-8
10 org.quartz.dataSource.myDS.user = root
11 org.quartz.dataSource.myDS.password = 123456
12 org.quartz.dataSource.myDS.maxConnections = 5

  OK了,然后我们可以启动Bootstrap类,注册、运行定时任务。你可以发现MySQL的表中已经有此定时任务的记录了。

  然后你可以停止Bootstrap类,将Bootstrap类中关于注册、启动定时任务的代码删除或注释,然后启动Bootstrap类,看上一次操作注册的定时任务会不会继续运行。

以上是关于Quartz将定时任务持久化到数据库的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置

SpringBoot整合Quartz定时任务(持久化到数据库)

SpringBoot整合Quartz定时任务(持久化到数据库)

springboot-01整合quartz