spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)相关的知识,希望对你有一定的参考价值。
背景:因为在项目中用到了定时任务,当时想到了spring的quartz,写完发现费了很大功夫,光是整合就花了一上午,其中最大的问题就是版本问题,项目中用的是spring3.2.8的版本,查阅发现,3.0以上的版本需要使用quartz2.X以上版本,我就去官网下载了2.1.7的quartz,结果发现jar包与spring冲突,最后使用了quartz1.6.0版本。
spring与quartz整合第一步需要导jar包,这个在百度搜下quartz的jar,下载一个
第二步:分布式定时任务,是基于数据库的,quartz完成定时任务需要其自身的表结构支撑,所以需要在mysql中创建表,1.X的版本,与2.X的数据库表结构不一样,这些sql脚本可以在你下载的quartz.zip包中doc中含有,下面是我的sql脚本(1.X版本的sql语句)
/*
SQLyog Ultimate v11.24 (32 bit)
MySQL - 5.5.27 : Database - tss_v1
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=‘‘*/;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`tss_v1` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `tss_v1`;
/*Table structure for table `qrtz_blob_triggers` */
DROP TABLE IF EXISTS `qrtz_blob_triggers`;
CREATE TABLE `qrtz_blob_triggers` (
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`BLOB_DATA` blob,
PRIMARY KEY (`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `TRIGGER_NAME` (`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_blob_triggers_ibfk_1` FOREIGN KEY (`TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_blob_triggers` */
/*Table structure for table `qrtz_calendars` */
DROP TABLE IF EXISTS `qrtz_calendars`;
CREATE TABLE `qrtz_calendars` (
`CALENDAR_NAME` varchar(200) NOT NULL,
`CALENDAR` blob NOT NULL,
PRIMARY KEY (`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_calendars` */
/*Table structure for table `qrtz_cron_triggers` */
DROP TABLE IF EXISTS `qrtz_cron_triggers`;
CREATE TABLE `qrtz_cron_triggers` (
`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 (`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `TRIGGER_NAME` (`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_cron_triggers_ibfk_1` FOREIGN KEY (`TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_cron_triggers` */
/*Table structure for table `qrtz_fired_triggers` */
DROP TABLE IF EXISTS `qrtz_fired_triggers`;
CREATE TABLE `qrtz_fired_triggers` (
`ENTRY_ID` varchar(95) NOT NULL,
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`IS_VOLATILE` varchar(1) NOT NULL,
`INSTANCE_NAME` varchar(200) NOT NULL,
`FIRED_TIME` bigint(13) NOT NULL,
`PRIORITY` int(11) NOT NULL,
`STATE` varchar(16) NOT NULL,
`JOB_NAME` varchar(200) DEFAULT NULL,
`JOB_GROUP` varchar(200) DEFAULT NULL,
`IS_STATEFUL` varchar(1) DEFAULT NULL,
`REQUESTS_RECOVERY` varchar(1) DEFAULT NULL,
PRIMARY KEY (`ENTRY_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_fired_triggers` */
/*Table structure for table `qrtz_job_details` */
DROP TABLE IF EXISTS `qrtz_job_details`;
CREATE TABLE `qrtz_job_details` (
`JOB_NAME` varchar(200) NOT NULL,
`JOB_GROUP` varchar(200) NOT NULL,
`DESCRIPTION` varchar(250) DEFAULT NULL,
`JOB_CLASS_NAME` varchar(250) NOT NULL,
`IS_DURABLE` varchar(1) NOT NULL,
`IS_VOLATILE` varchar(1) NOT NULL,
`IS_STATEFUL` varchar(1) NOT NULL,
`REQUESTS_RECOVERY` varchar(1) NOT NULL,
`JOB_DATA` blob,
PRIMARY KEY (`JOB_NAME`,`JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_job_details` */
/*Table structure for table `qrtz_job_listeners` */
DROP TABLE IF EXISTS `qrtz_job_listeners`;
CREATE TABLE `qrtz_job_listeners` (
`JOB_NAME` varchar(200) NOT NULL,
`JOB_GROUP` varchar(200) NOT NULL,
`JOB_LISTENER` varchar(200) NOT NULL,
PRIMARY KEY (`JOB_NAME`,`JOB_GROUP`,`JOB_LISTENER`),
KEY `JOB_NAME` (`JOB_NAME`,`JOB_GROUP`),
CONSTRAINT `qrtz_job_listeners_ibfk_1` FOREIGN KEY (`JOB_NAME`, `JOB_GROUP`) REFERENCES `qrtz_job_details` (`JOB_NAME`, `JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_job_listeners` */
/*Table structure for table `qrtz_locks` */
DROP TABLE IF EXISTS `qrtz_locks`;
CREATE TABLE `qrtz_locks` (
`LOCK_NAME` varchar(40) NOT NULL,
PRIMARY KEY (`LOCK_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_locks` */
insert into `qrtz_locks`(`LOCK_NAME`) values (‘CALENDAR_ACCESS‘),(‘JOB_ACCESS‘),(‘MISFIRE_ACCESS‘),(‘STATE_ACCESS‘),(‘TRIGGER_ACCESS‘);
/*Table structure for table `qrtz_paused_trigger_grps` */
DROP TABLE IF EXISTS `qrtz_paused_trigger_grps`;
CREATE TABLE `qrtz_paused_trigger_grps` (
`TRIGGER_GROUP` varchar(200) NOT NULL,
PRIMARY KEY (`TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_paused_trigger_grps` */
/*Table structure for table `qrtz_scheduler_state` */
DROP TABLE IF EXISTS `qrtz_scheduler_state`;
CREATE TABLE `qrtz_scheduler_state` (
`INSTANCE_NAME` varchar(200) NOT NULL,
`LAST_CHECKIN_TIME` bigint(13) NOT NULL,
`CHECKIN_INTERVAL` bigint(13) NOT NULL,
PRIMARY KEY (`INSTANCE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Table structure for table `qrtz_simple_triggers` */
DROP TABLE IF EXISTS `qrtz_simple_triggers`;
CREATE TABLE `qrtz_simple_triggers` (
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`REPEAT_COUNT` bigint(7) NOT NULL,
`REPEAT_INTERVAL` bigint(12) NOT NULL,
`TIMES_TRIGGERED` bigint(10) NOT NULL,
PRIMARY KEY (`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `TRIGGER_NAME` (`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_simple_triggers_ibfk_1` FOREIGN KEY (`TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_simple_triggers` */
/*Table structure for table `qrtz_trigger_listeners` */
DROP TABLE IF EXISTS `qrtz_trigger_listeners`;
CREATE TABLE `qrtz_trigger_listeners` (
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`TRIGGER_LISTENER` varchar(200) NOT NULL,
PRIMARY KEY (`TRIGGER_NAME`,`TRIGGER_GROUP`,`TRIGGER_LISTENER`),
KEY `TRIGGER_NAME` (`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_trigger_listeners_ibfk_1` FOREIGN KEY (`TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_trigger_listeners` */
/*Table structure for table `qrtz_triggers` */
DROP TABLE IF EXISTS `qrtz_triggers`;
CREATE TABLE `qrtz_triggers` (
`TRIGGER_NAME` varchar(200) NOT NULL,
`TRIGGER_GROUP` varchar(200) NOT NULL,
`JOB_NAME` varchar(200) NOT NULL,
`JOB_GROUP` varchar(200) NOT NULL,
`IS_VOLATILE` varchar(1) NOT NULL,
`DESCRIPTION` varchar(250) DEFAULT NULL,
`NEXT_FIRE_TIME` bigint(13) DEFAULT NULL,
`PREV_FIRE_TIME` bigint(13) DEFAULT NULL,
`PRIORITY` int(11) DEFAULT NULL,
`TRIGGER_STATE` varchar(16) NOT NULL,
`TRIGGER_TYPE` varchar(8) NOT NULL,
`START_TIME` bigint(13) NOT NULL,
`END_TIME` bigint(13) DEFAULT NULL,
`CALENDAR_NAME` varchar(200) DEFAULT NULL,
`MISFIRE_INSTR` smallint(2) DEFAULT NULL,
`JOB_DATA` blob,
PRIMARY KEY (`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `JOB_NAME` (`JOB_NAME`,`JOB_GROUP`),
CONSTRAINT `qrtz_triggers_ibfk_1` FOREIGN KEY (`JOB_NAME`, `JOB_GROUP`) REFERENCES `qrtz_job_details` (`JOB_NAME`, `JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `qrtz_triggers` */
/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;
/*!40014 SET [email protected]_UNIQUE_CHECKS */;
/*!40111 SET [email protected]_SQL_NOTES */;
表创建完成后,第三步就是创建quartz.properties文件
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = ClusteredScheduler #scheduler 名称,用于区分,集群中使用同一个名称
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.skipUpdateCheck = true
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
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.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval =15000
第四步:创建spring-quartz配置文件
<?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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <description>spring任务调度,quartz任务调度,该文件是空配置,确保能启动定时任务,具体 业务配置需在页面进行配置即可,不用描述在该文件中</description> <task:annotation-driven executor="quartzTaskExecutor" /> <task:executor id="quartzTaskExecutor" keep-alive="900" pool-size="10" queue-capacity="20" /> <!-- Quartz集群Schduler --> <bean id="clusterQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- Triggers集成 动态配置 --> <!-- <property name="triggers"> <list> <ref bean="allocationPlanTrigger" /> </list> </property> --> <!-- quartz配置文件路径, 指向cluster配置 --> <property name="configLocation" value="classpath:quartz.properties" /> <!-- 启动时延期2秒开始任务 --> <property name="startupDelay" value="2" /> <!-- 保存Job数据到数据库所需的数据源 --> <property name="dataSource" ref="dataSource" /> <!-- Job接受applicationContext的成员变量名 --> <property name = "schedulerContextAsMap"> <map> <!-- spring管理的服务需要放到这里,才能够注入成功 --> <description> schedulerContextAsMap </description > <entry key = "allocationOrderService" value-ref = "allocationOrderService" /> </map > </property > <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <!-- 修改job时,更新到数据库 --> <property name="overwriteExistingJobs" value="true" /> </bean> <!-- 定时任务 页面动态配置--> <!-- <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myTask" /> 每5秒执行一次 <property name="cronExpression" value="*/5 * * * * ? " /> </bean> <bean id="myTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="durability" value="true" /> <property name="jobClass" value="com.yestae.tss.common.quartz.task.MyTask" /> </bean> --> </beans>
因为是基于页面进行动态配置的,所以只注册scheduler,如果你的自定义任务里需要使用spring的bean,schedulerContextAsMap 可以帮你完成注入,但在你的执行任务里要写get和set方法,不需要使用注解。
第五步:创建我们需要执行的任务类
package com.yestae.tss.common.quartz.task; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean; import com.yestae.tss.allocation.service.IAllocationOrderService; public class AllocationPlanTask extends QuartzJobBean { private IAllocationOrderService allocationOrderService; //注入service,記得生成get和set方法,才能注入成功 @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { //TODO 执行自己的任务 System.out.println(allocationOrderService+"---"+new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss").format(new Date())); } public IAllocationOrderService getAllocationOrderService() { return allocationOrderService; } public void setAllocationOrderService( IAllocationOrderService allocationOrderService) { this.allocationOrderService = allocationOrderService; } }
基本上完成就差不多了,下面来进行定时器的动态管理,直接上代码
接口
package com.yestae.tss.common.quartz.service; import java.text.ParseException; import java.util.List; import org.quartz.SchedulerException; import com.yestae.tss.common.quartz.VO.QuartzJobVo; public interface ISchedulerService { /** * @Description: 添加一个定时任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务 * @param time * 时间设置,参考quartz说明文档 * @throws ParseException * @throws SchedulerException */ Boolean addJod(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) throws ParseException, SchedulerException; /** * @Description: 移除一个任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * */ public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws ParseException, SchedulerException; /** * @Description: 修改一个任务的触发时间 * * @param triggerName * 触发器名称 * @param triggerGroupName * 触发器组名称 * @param time * */ public Boolean modifyJobTime(String triggerName, String triggerGroupName, String time) throws ParseException, SchedulerException; /** * @Description: 修改一个任务的触发时间 * * @param triggerName * 触发器名称 * @param triggerGroupName * 触发器组名称 * */ public List<QuartzJobVo> getTriggerS(String triggerName, String triggerGroupName) throws ParseException, SchedulerException; /** * @Description: 暂停任务,但当前任务会执行完毕,执行完毕则不会继续执行了 * * @param jobName * 任务名称 * @param groupName * 任务组名称 * */ public void pauseJob(String jobName, String groupName) throws SchedulerException; }
package com.yestae.tss.common.quartz.service; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; import com.yestae.tss.common.quartz.VO.QuartzJobVo; @Service public class SchedulerServiceImpl implements ISchedulerService { @Autowired private Scheduler scheduler; @Override public Boolean addJod(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) throws ParseException, SchedulerException { JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类 jobDetail.setDurability(true); //任务完成后任然将任务保存在数据库 // 触发器 CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 scheduler.scheduleJob(jobDetail, trigger); return null; } @Override public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws ParseException, SchedulerException { scheduler.pauseTrigger(triggerName, triggerGroupName);// 停止触发器 scheduler.unscheduleJob(triggerName, triggerGroupName);// 移除触发器 scheduler.deleteJob(jobName, jobGroupName);// 删除任务 return; } @Override public Boolean modifyJobTime(String triggerName, String triggerGroupName, String time) throws ParseException, SchedulerException { CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName,triggerGroupName); if (trigger == null) { return false; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { // 修改时间 trigger.setCronExpression(time); // 重启触发器 scheduler.resumeTrigger(triggerName, triggerGroupName); } return null; } @Override public List<QuartzJobVo> getTriggerS(String triggerName, String triggerGroupName) throws ParseException, SchedulerException { List<QuartzJobVo> list = Lists.newArrayList(); //获取任务名称 for (String groupName : scheduler.getJobGroupNames()) { for (String jobName : scheduler.getJobNames(groupName)) { QuartzJobVo quartzJobVo = new QuartzJobVo(); Trigger[] triggers = scheduler.getTriggersOfJob(jobName,groupName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggers[0].getName(), triggers[0].getGroup()); quartzJobVo.setJobName(jobName); quartzJobVo.setJobGroupName(groupName); quartzJobVo.setTriggerGroupName(triggers[0].getGroup()); quartzJobVo.setTriggerName(triggers[0].getName()); quartzJobVo.setCronExpression(trigger.getCronExpression()); list.add(quartzJobVo); } } return list; } @Override public void pauseJob(String jobName, String groupName) throws SchedulerException { scheduler.pauseJob(jobName, groupName); return; } }
前端通过参数进行添加,删除,修改相关配置就ok了,前端页面目前还没出来,等出来了,给予展示出来。
定时任务结果:
以上是关于spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)的主要内容,如果未能解决你的问题,请参考以下文章
项目ITP spring4.0 整合 Quartz 实现动态任务调度