作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数。如果你的作业是30分钟后或每30秒调用,这不是很有用。事实上,作业执行需要非常准确和即时调用在被调度作业上的execute()方法。Quartz通过一个称之为作业存储(JobStore)的概念来做作业存储和管理。Quartz提供两种基本作业存储类型。
RAMJobStore
第一种类型叫做RAMJobStore(默认存储方式),它利用通常的内存来持久化调度程序信息。这种作业存储类型最容易配置、构造和运行。对许多应用来说,这种作业存储已经足够了。
然而因为调度程序信息是存储在被分配给JVM的内存里面,所以当应用程序停止运行时,所有调度信息将被丢失。如果你需要在重新启动之间持久化调度信息,则将需要第二种类型的作业存储。
JDBC作业存储
第二种类型的作业存储实际上提供两种不同的实现,但两种实现一般都称为JDBC作业存储。
两种JDBC作业存储都需要JDBC驱动程序和后台数据库来持久化调度程序信息。这两种类型的不同在于你是否想要控制数据库事务或这释放控制给应用服务器例如BEA's WebLogic或Jboss。
这两种JDBC作业存储是:
· JobStoreTX:当你想要控制事务或工作在非应用服务器环境中是使用
· JobStoreCMT:当你工作在应用服务器环境中和想要容器控制事务时使用。
JDBC作业存储为需要调度程序维护调度信息的用户而设计
----------------------------------------------------------------------我是分割线----------------------------------------------------------------------------
持久化配置
#作业存储配置
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#数据源信息
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
org.quartz.dataSource.myDS.user = test
org.quartz.dataSource.myDS.password = test
org.quartz.dataSource.myDS.maxConnections = 5
spring-scheduler.xml
<!-- 配置scheduler 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="scheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="configLocation" value="classpath:quartz.properties"></property> <property name="schedulerName" value="derella-web-scheduler" /> <!-- 这里配置的数据源会覆盖 quartz.properties文件中的配置--> <property name="dataSource" ref="quartzDataSource"/> <!-- 触发器 --> <property name="triggers"> <list> <!-- <ref bean = "testJobTrigger" /> --> </list> </property> </bean>
spring-datasource.xml数据源配置
<!-- quartz --> <bean id="quartzDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="test" /> <property name="password" value="test" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> </bean>
启动应用,查看quartz数据库QRTZ_SCHEDULER_STATE。 有配置的scheduler
quartz数据表
QRTZ_CALENDARS:以 BLOB 类型存储 Quartz 的 Calendar 信息
QRTZ_CRON_TRIGGERS:存储 Cron Trigger,包括 Cron表达式和时区信息
QRTZ_SIMPLE_TRIGGERS:存储简单的Trigger,包括重复次数,间隔,以及已触的次数
QRTZ_FIRED_TRIGGERS:存储与已触发的 Trigger 相关的状态信息,以及相联 Job的执行信息 QRTZ_PAUSED_TRIGGER_GRPS 存储已暂停的 Trigger 组的信息
QRTZ_BLOG_TRIGGERS:Trigger 作为 Blob 类型存储(用于 Quartz 用户用 JDBC创建他们自己定制的 Trigger 类型,JobStore 并不知道如何存储实例的时候)
QRTZ_TRIGGERS:存储已配置的 Trigger 的信息
QRTZ_SCHEDULER_STATE:存储少量的有关 Scheduler 的状态信息,和别的 Scheduler实例(假如是用于一个集群中)
QRTZ_LOCKS:存储程序的悲观锁的信息(假如使用了悲观锁)
QRTZ_JOB_DETAILS:存储每一个已配置的 Job 的详细信息
QRTZ_JOB_LISTENERS:存储有关已配置的 JobListener 的信息
QRTZ_TRIGGER_LISTENERS:存储已配置的 TriggerListener 的信息
/*Table structure for table `QRTZ_BLOB_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_BLOB_TRIGGERS`;
CREATE TABLE `QRTZ_BLOB_TRIGGERS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT 'Trigger Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
`BLOB_DATA` blob COMMENT 'Trigger存储的数据(java.io.Serializable)',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_TRIGGER_NAME_TRIGGER_GROUP` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_blob_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='[Trigger]存储数据';
/*Table structure for table `QRTZ_CALENDARS` */
DROP TABLE IF EXISTS `QRTZ_CALENDARS`;
CREATE TABLE `QRTZ_CALENDARS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`CALENDAR_NAME` varchar(200) NOT NULL COMMENT 'Calendar Name',
`CALENDAR` blob NOT NULL COMMENT 'Calendar',
PRIMARY KEY (`ID`),
KEY `IDX_SCHED_NAME_CALENDAR_NAME` (`SCHED_NAME`,`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='任务排入的日程表';
/*Table structure for table `QRTZ_CRON_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_CRON_TRIGGERS`;
CREATE TABLE `QRTZ_CRON_TRIGGERS` (
`SCHED_NAME` varchar(120) NOT NULL,
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`CRON_EXPRESSION` varchar(120) NOT NULL,
`TIME_ZONE_ID` varchar(80) DEFAULT NULL,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_cron_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Table structure for table `QRTZ_FIRED_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_FIRED_TRIGGERS`;
CREATE TABLE `QRTZ_FIRED_TRIGGERS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`ENTRY_ID` varchar(95) NOT NULL COMMENT 'Entry Id',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT 'Trigger Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
`INSTANCE_NAME` varchar(200) NOT NULL COMMENT 'Instance Name',
`FIRED_TIME` bigint(13) NOT NULL COMMENT 'Fired Time [触发时间]',
`PRIORITY` int(11) NOT NULL COMMENT 'Priority [Job优先级]',
`STATE` varchar(16) NOT NULL COMMENT 'State',
`JOB_NAME` varchar(200) DEFAULT NULL COMMENT 'Job Name',
`JOB_GROUP` varchar(200) DEFAULT NULL COMMENT 'Job Group',
`IS_NONCONCURRENT` varchar(1) DEFAULT NULL COMMENT 'Is Nonconcurrent [不允许并发]',
`REQUESTS_RECOVERY` varchar(1) DEFAULT NULL COMMENT 'Requests Recovery [任务错误重试机制]',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_ENTRY_ID` (`SCHED_NAME`,`ENTRY_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=511547 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Fired Trigger';
/*Table structure for table `QRTZ_JOB_DETAILS` */
DROP TABLE IF EXISTS `QRTZ_JOB_DETAILS`;
CREATE TABLE `QRTZ_JOB_DETAILS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`JOB_NAME` varchar(200) NOT NULL COMMENT 'Job Name',
`JOB_GROUP` varchar(200) NOT NULL COMMENT 'Job Group',
`DESCRIPTION` varchar(250) DEFAULT NULL COMMENT 'Description [描述]',
`JOB_CLASS_NAME` varchar(250) NOT NULL COMMENT 'Job Class Name [任务Class]',
`IS_DURABLE` varchar(1) NOT NULL COMMENT 'IS DURABLE',
`IS_NONCONCURRENT` varchar(1) NOT NULL COMMENT 'IS NONCONCURRENT',
`IS_UPDATE_DATA` varchar(1) NOT NULL COMMENT 'Is Update Data',
`REQUESTS_RECOVERY` varchar(1) NOT NULL COMMENT 'Requests Recovery',
`JOB_DATA` blob COMMENT 'Job Data [Job存储的数据]',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_JOB_NAME_JOB_GROUP` (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`)
) ENGINE=InnoDB AUTO_INCREMENT=58425 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Job Details';
/*Table structure for table `QRTZ_LOCKS` */
DROP TABLE IF EXISTS `QRTZ_LOCKS`;
CREATE TABLE `QRTZ_LOCKS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`LOCK_NAME` varchar(40) NOT NULL COMMENT 'Lock Name',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_LOCK_NAME` (`SCHED_NAME`,`LOCK_NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='锁表';
/*Table structure for table `QRTZ_PAUSED_TRIGGER_GRPS` */
DROP TABLE IF EXISTS `QRTZ_PAUSED_TRIGGER_GRPS`;
CREATE TABLE `QRTZ_PAUSED_TRIGGER_GRPS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_TRIGGER_GROUP` (`SCHED_NAME`,`TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Paused Trigger';
/*Table structure for table `QRTZ_SCHEDULER_STATE` */
DROP TABLE IF EXISTS `QRTZ_SCHEDULER_STATE`;
CREATE TABLE `QRTZ_SCHEDULER_STATE` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`INSTANCE_NAME` varchar(200) NOT NULL COMMENT 'Instance Name',
`LAST_CHECKIN_TIME` bigint(13) NOT NULL COMMENT 'last Checkin Time',
`CHECKIN_INTERVAL` bigint(13) NOT NULL COMMENT 'Checkin Interval',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_INSTANCE_NAME` (`SCHED_NAME`,`INSTANCE_NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=1215989 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Scheduler State';
/*Table structure for table `QRTZ_SIMPLE_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_SIMPLE_TRIGGERS`;
CREATE TABLE `QRTZ_SIMPLE_TRIGGERS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT 'Trigger Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
`REPEAT_COUNT` bigint(7) NOT NULL COMMENT 'Repeat Count',
`REPEAT_INTERVAL` bigint(12) NOT NULL COMMENT 'Repeat Interval',
`TIMES_TRIGGERED` bigint(10) NOT NULL COMMENT 'Times Triggered',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_TRIGGER_NAME_TRIGGER_GROUP` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_simple_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB AUTO_INCREMENT=58068 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Simple Trigger';
/*Table structure for table `QRTZ_SIMPROP_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_SIMPROP_TRIGGERS`;
CREATE TABLE `QRTZ_SIMPROP_TRIGGERS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT 'Trigger Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
`STR_PROP_1` varchar(512) DEFAULT NULL COMMENT 'String Properties 1',
`STR_PROP_2` varchar(512) DEFAULT NULL COMMENT 'String Properties 2',
`STR_PROP_3` varchar(512) DEFAULT NULL COMMENT 'String Properties 3',
`INT_PROP_1` int(11) DEFAULT NULL COMMENT 'Integer Properties 1',
`INT_PROP_2` int(11) DEFAULT NULL COMMENT 'Integer Properties 2',
`LONG_PROP_1` bigint(20) DEFAULT NULL COMMENT 'Long Properties 1',
`LONG_PROP_2` bigint(20) DEFAULT NULL COMMENT 'Long Properties 2',
`DEC_PROP_1` decimal(13,4) DEFAULT NULL COMMENT 'Decimal Properties 1',
`DEC_PROP_2` decimal(13,4) DEFAULT NULL COMMENT 'Decimal Properties 2',
`BOOL_PROP_1` varchar(1) DEFAULT NULL COMMENT 'Boolean Properties 1',
`BOOL_PROP_2` varchar(1) DEFAULT NULL COMMENT 'Boolean Properties 1',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_TRIGGER_NAME_TRIGGER_GROUP` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_simprop_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Simple Properties Trigger';
/*Table structure for table `QRTZ_TRIGGERS` */
DROP TABLE IF EXISTS `QRTZ_TRIGGERS`;
CREATE TABLE `QRTZ_TRIGGERS` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`SCHED_NAME` varchar(120) NOT NULL COMMENT 'Scheduler Name',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT 'Trigger Name',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT 'Trigger Group',
`JOB_NAME` varchar(200) NOT NULL COMMENT 'Job Name',
`JOB_GROUP` varchar(200) NOT NULL COMMENT 'Job Group',
`DESCRIPTION` varchar(250) DEFAULT NULL COMMENT 'Description [描述]',
`NEXT_FIRE_TIME` bigint(13) DEFAULT NULL COMMENT 'Next Fire Time',
`PREV_FIRE_TIME` bigint(13) DEFAULT NULL COMMENT 'Prev Fire Time',
`PRIORITY` int(11) DEFAULT NULL COMMENT 'Priority',
`TRIGGER_STATE` varchar(16) NOT NULL COMMENT 'Trigger State',
`TRIGGER_TYPE` varchar(8) NOT NULL COMMENT 'Trigger Type',
`START_TIME` bigint(13) NOT NULL COMMENT 'Start Time',
`END_TIME` bigint(13) DEFAULT NULL COMMENT 'End Time',
`CALENDAR_NAME` varchar(200) DEFAULT NULL COMMENT 'Calendar Name',
`MISFIRE_INSTR` smallint(2) DEFAULT NULL COMMENT 'Misfire Instr',
`JOB_DATA` blob COMMENT 'Job Data',
PRIMARY KEY (`ID`),
UNIQUE KEY `IDX_SCHED_NAME_TRIGGER_NAME_TRIGGER_GROUP` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `qrtz_triggers_ibfk_1` (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`),
CONSTRAINT `qrtz_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`) REFERENCES `QRTZ_JOB_DETAILS` (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`)
) ENGINE=InnoDB AUTO_INCREMENT=59174 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='Trigger';