概述
项目需要定时器的调度管理,原来使用Spring Boot自带的定时器,但是不能后台动态的操作暂停、启动以及新增任务等操作,维护起来相对麻烦;最近研究了Quartz的框架,觉得还算不错,整理了一下代码,整合到现有系统里面,整体效果如下图所示,记录操作流程如下文。
数据库
将Quartz依赖的数据库脚本提交现有项目数据库,脚本如下:
DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;
DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE;
DROP TABLE IF EXISTS QRTZ_LOCKS;
DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_JOB_DETAILS;
DROP TABLE IF EXISTS QRTZ_CALENDARS; CREATE TABLE QRTZ_JOB_DETAILS
(
SCHED_NAME VARCHAR() NOT NULL,
JOB_NAME VARCHAR() NOT NULL,
JOB_GROUP VARCHAR() NOT NULL,
DESCRIPTION VARCHAR() NULL,
JOB_CLASS_NAME VARCHAR() NOT NULL,
IS_DURABLE VARCHAR() NOT NULL,
IS_NONCONCURRENT VARCHAR() NOT NULL,
IS_UPDATE_DATA VARCHAR() NOT NULL,
REQUESTS_RECOVERY VARCHAR() NOT NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
); CREATE TABLE QRTZ_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
JOB_NAME VARCHAR() NOT NULL,
JOB_GROUP VARCHAR() NOT NULL,
DESCRIPTION VARCHAR() NULL,
NEXT_FIRE_TIME BIGINT() NULL,
PREV_FIRE_TIME BIGINT() NULL,
PRIORITY INTEGER NULL,
TRIGGER_STATE VARCHAR() NOT NULL,
TRIGGER_TYPE VARCHAR() NOT NULL,
START_TIME BIGINT() NOT NULL,
END_TIME BIGINT() NULL,
CALENDAR_NAME VARCHAR() NULL,
MISFIRE_INSTR SMALLINT() NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)
); CREATE TABLE QRTZ_SIMPLE_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
REPEAT_COUNT BIGINT() NOT NULL,
REPEAT_INTERVAL BIGINT() NOT NULL,
TIMES_TRIGGERED BIGINT() NOT NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
); CREATE TABLE QRTZ_CRON_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
CRON_EXPRESSION VARCHAR() NOT NULL,
TIME_ZONE_ID VARCHAR(),
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
); CREATE TABLE QRTZ_SIMPROP_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
STR_PROP_1 VARCHAR() NULL,
STR_PROP_2 VARCHAR() NULL,
STR_PROP_3 VARCHAR() NULL,
INT_PROP_1 INT NULL,
INT_PROP_2 INT NULL,
LONG_PROP_1 BIGINT NULL,
LONG_PROP_2 BIGINT NULL,
DEC_PROP_1 NUMERIC(,) NULL,
DEC_PROP_2 NUMERIC(,) NULL,
BOOL_PROP_1 VARCHAR() NULL,
BOOL_PROP_2 VARCHAR() NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
); CREATE TABLE QRTZ_BLOB_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
BLOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
); CREATE TABLE QRTZ_CALENDARS
(
SCHED_NAME VARCHAR() NOT NULL,
CALENDAR_NAME VARCHAR() NOT NULL,
CALENDAR BLOB NOT NULL,
PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)
); CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS
(
SCHED_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)
); CREATE TABLE QRTZ_FIRED_TRIGGERS
(
SCHED_NAME VARCHAR() NOT NULL,
ENTRY_ID VARCHAR() NOT NULL,
TRIGGER_NAME VARCHAR() NOT NULL,
TRIGGER_GROUP VARCHAR() NOT NULL,
INSTANCE_NAME VARCHAR() NOT NULL,
FIRED_TIME BIGINT() NOT NULL,
SCHED_TIME BIGINT() NOT NULL,
PRIORITY INTEGER NOT NULL,
STATE VARCHAR() NOT NULL,
JOB_NAME VARCHAR() NULL,
JOB_GROUP VARCHAR() NULL,
IS_NONCONCURRENT VARCHAR() NULL,
REQUESTS_RECOVERY VARCHAR() NULL,
PRIMARY KEY (SCHED_NAME,ENTRY_ID)
); CREATE TABLE QRTZ_SCHEDULER_STATE
(
SCHED_NAME VARCHAR() NOT NULL,
INSTANCE_NAME VARCHAR() NOT NULL,
LAST_CHECKIN_TIME BIGINT() NOT NULL,
CHECKIN_INTERVAL BIGINT() NOT NULL,
PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)
); CREATE TABLE QRTZ_LOCKS
(
SCHED_NAME VARCHAR() NOT NULL,
LOCK_NAME VARCHAR() NOT NULL,
PRIMARY KEY (SCHED_NAME,LOCK_NAME)
); commit;
代码编写
添加Maven依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>RELEASE</version>
</dependency>
添加Quartz配置类
1>、QuartzJobFactory.JAVA(注册JOB工厂类,主要是为了集成Sping Boot的注解管理类使用)
package com.dbgo.quartzdemo.config; import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component; @Component
public class QuartzJobFactory extends AdaptableJobFactory { /**
* AutowireCapableBeanFactory接口是BeanFactory的子类
* 可以连接和填充那些生命周期不被Spring管理的已存在的bean实例
* 具体请参考:http://blog.csdn.net/iycynna_123/article/details/52993542
*/
@Autowired
private AutowireCapableBeanFactory capableBeanFactory; /**
* 创建Job实例
*
* @param bundle
* @return
* @throws Exception
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 实例化对象
Object jobInstance = super.createJobInstance(bundle);
// 进行注入(Spring管理该Bean)
capableBeanFactory.autowireBean(jobInstance);
//返回对象
return jobInstance;
}
}
2>、SchedulerConfig.JAVA(注册调度框架的实例,配置依赖的JobFactory和属性)
package com.dbgo.quartzdemo.config; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import java.io.IOException;
import java.util.Properties; @Configuration
public class SchedulerConfig { @Autowired
private QuartzJobFactory quartzJobFactory; @Bean(name = "quartzScheduler")
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//用于quartz集群,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
//factory.setOverwriteExistingJobs(true);
//QuartzScheduler 延时启动,应用启动完10秒后 QuartzScheduler 再启动
factory.setStartupDelay();
factory.setAutoStartup(true);
factory.setOverwriteExistingJobs(true);
factory.setQuartzProperties(quartzProperties()); //作业及数据源配置信息
// 自定义Job Factory,用于Spring注入 service,bin等 factory.setJobFactory(quartzJobFactory); return factory;
} @Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
}
3>、DruidConnectionProvider.JAVA(配置Quartz的数据库连接,读取项目下的quartz.properties文件属性)
package com.dbgo.quartzdemo.config; import java.sql.Connection;
import java.sql.SQLException;
import org.quartz.SchedulerException;
import org.quartz.utils.ConnectionProvider; import com.alibaba.druid.pool.DruidDataSource; public class DruidConnectionProvider implements ConnectionProvider { //JDBC驱动
public String driver;
//JDBC连接串
public String URL;
//数据库用户名
public String user;
//数据库用户密码
public String password;
//数据库最大连接数
public int maxConnection;
//数据库SQL查询每次连接返回执行到连接池,以确保它仍然是有效的。
public String validationQuery;
private boolean validateOnCheckout;
private int idleConnectionValidationSeconds;
public String maxCachedStatementsPerConnection;
private String discardIdleConnectionsSeconds;
public static final int DEFAULT_DB_MAX_CONNECTIONS = ;
public static final int DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION = ;
//Druid连接池
private DruidDataSource datasource;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 接口实现
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public Connection getConnection() throws SQLException {
return datasource.getConnection();
}
public void shutdown() throws SQLException {
datasource.close();
}
public void initialize() throws SQLException{
if (this.URL == null) {
throw new SQLException("DBPool could not be created: DB URL cannot be null");
}
if (this.driver == null) {
throw new SQLException("DBPool driver could not be created: DB driver class name cannot be null!");
}
if (this.maxConnection < ) {
throw new SQLException("DBPool maxConnectins could not be created: Max connections must be greater than zero!");
}
datasource = new DruidDataSource();
try{
datasource.setDriverClassName(this.driver);
} catch (Exception e) {
try {
throw new SchedulerException("Problem setting driver class name on datasource: " + e.getMessage(), e);
} catch (SchedulerException e1) {
}
}
datasource.setUrl(this.URL);
datasource.setUsername(this.user);
datasource.setPassword(this.password);
datasource.setMaxActive(this.maxConnection);
datasource.setMinIdle();
datasource.setMaxWait();
datasource.setMaxPoolPreparedStatementPerConnectionSize(DEFAULT_DB_MAX_CONNECTIONS);
if (this.validationQuery != null) {
datasource.setValidationQuery(this.validationQuery);
if(!this.validateOnCheckout)
datasource.setTestOnReturn(true);
else
datasource.setTestOnBorrow(true);
datasource.setValidationQueryTimeout(this.idleConnectionValidationSeconds);
}
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 提供get set方法
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMaxConnection() {
return maxConnection;
}
public void setMaxConnection(int maxConnection) {
this.maxConnection = maxConnection;
}
public String getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public boolean isValidateOnCheckout() {
return validateOnCheckout;
}
public void setValidateOnCheckout(boolean validateOnCheckout) {
this.validateOnCheckout = validateOnCheckout;
}
public int getIdleConnectionValidationSeconds() {
return idleConnectionValidationSeconds;
}
public void setIdleConnectionValidationSeconds(int idleConnectionValidationSeconds) {
this.idleConnectionValidationSeconds = idleConnectionValidationSeconds;
}
public DruidDataSource getDatasource() {
return datasource;
}
public void setDatasource(DruidDataSource datasource) {
this.datasource = datasource;
}
public String getDiscardIdleConnectionsSeconds() {
return discardIdleConnectionsSeconds;
}
public void setDiscardIdleConnectionsSeconds(String discardIdleConnectionsSeconds) {
this.discardIdleConnectionsSeconds = discardIdleConnectionsSeconds;
} }
4>、添加定时任务DEMO(这个Demo仅仅模拟正常业务工作的Job,项目写Job的时候可以参考)
package com.dbgo.quartzdemo.job; import java.util.Date; import org.apache.jasper.tagplugins.jstl.core.Catch;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component; @Component
//@DisallowConcurrentExecution //保证上一次任务执行完毕再执行下一任务
public class HelloWorldJob implements Job{ /**
* "0/10 * * * * ?
*/
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try { System.out.println("----START---hello world---" + new Date());
Thread.sleep(*);
System.out.println("----END---hello world---" + new Date());
} catch (Exception ex) {
} } }
添加Quartz服务类
1>、QuartzService.java的接口
package com.dbgo.quartzdemo.domain.api; public interface QuartzService { /**
* addJob(方法描述:添加一个定时任务) <br />
* (方法适用条件描述: – 可选)
*
* @param jobName
* 作业名称
* @param jobGroupName
* 作业组名称
* @param triggerName
* 触发器名称
* @param triggerGroupName
* 触发器组名称
* @param cls
* 定时任务的class
* @param cron
* 时间表达式 void
* @exception
* @since 1.0.0
*/
public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class cls, String cron); /**
*
* @param oldjobName 原job name
* @param oldjobGroup 原job group
* @param oldtriggerName 原 trigger name
* @param oldtriggerGroup 原 trigger group
* @param jobName
* @param jobGroup
* @param triggerName
* @param triggerGroup
* @param cron
*/
public boolean modifyJobTime(String oldjobName, String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup, String triggerName, String triggerGroup, String cron); /**
* 修改触发器调度时间
* @param triggerName 触发器名称
* @param triggerGroupName 触发器组名称
* @param cron cron表达式
*/
public void modifyJobTime(String triggerName,
String triggerGroupName, String cron); /**
* 暂停指定的任务
* @param jobName 任务名称
* @param jobGroupName 任务组名称
* @return
*/
public void pauseJob(String jobName, String jobGroupName); /**
* 恢复指定的任务
* @param jobName 任务名称
* @param jobGroupName 任务组名称
* @return
*/
public void resumeJob(String jobName, String jobGroupName); /**
* 删除指定组任务
* @param jobName 作业名称
* @param jobGroupName 作业组名称
* @param triggerName 触发器名称
* @param triggerGroupName 触发器组名称
*/
public void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName); /**
* 开始所有定时任务。启动调度器
*/
public void startSchedule(); /**
* 关闭调度器
*/
public void shutdownSchedule();
}
2>、QuartzServiceImpl.java的接口实现类
package com.dbgo.quartzdemo.domain.server; import com.dbgo.quartzdemo.domain.api.QuartzService;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service("quartzService")
public class QuartzServiceImpl implements QuartzService { @Autowired
private Scheduler quartzScheduler; @Override
public void addJob(String jobName, String jobGroupName, String triggerName,
String triggerGroupName, Class cls, String cron) {
try {
// 获取调度器
Scheduler sched = quartzScheduler;
// 创建一项作业
JobDetail job = JobBuilder.newJob(cls)
.withIdentity(jobName, jobGroupName).build();
// 创建一个触发器
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerName, triggerGroupName)
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.build(); // 告诉调度器使用该触发器来安排作业
sched.scheduleJob(job, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} /**
* 修改定时器任务信息
*/
@Override
public boolean modifyJobTime(String oldjobName, String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup,
String triggerName, String triggerGroup, String cron) {
try {
Scheduler sched = quartzScheduler;
CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey
.triggerKey(oldtriggerName, oldtriggerGroup));
if (trigger == null) {
return false;
} JobKey jobKey = JobKey.jobKey(oldjobName, oldjobGroup);
TriggerKey triggerKey = TriggerKey.triggerKey(oldtriggerName,
oldtriggerGroup); JobDetail job = sched.getJobDetail(jobKey);
Class jobClass = job.getJobClass();
// 停止触发器
sched.pauseTrigger(triggerKey);
// 移除触发器
sched.unscheduleJob(triggerKey);
// 删除任务
sched.deleteJob(jobKey); addJob(jobName, jobGroup, triggerName, triggerGroup, jobClass,
cron); return true;
} catch (Exception e) {
throw new RuntimeException(e);
} } @Override
public void modifyJobTime(String triggerName, String triggerGroupName,
String time) {
try {
Scheduler sched = quartzScheduler;
CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey
.triggerKey(triggerName, triggerGroupName));
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.getTriggerBuilder()
.withSchedule(CronScheduleBuilder.cronSchedule(time))
.build();
// 重启触发器
sched.resumeTrigger(TriggerKey.triggerKey(triggerName,
triggerGroupName));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = quartzScheduler;
// 停止触发器
sched.pauseTrigger(TriggerKey.triggerKey(triggerName,
triggerGroupName));
// 移除触发器
sched.unscheduleJob(TriggerKey.triggerKey(triggerName,
triggerGroupName));
// 删除任务
sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));
} catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void startSchedule() {
try {
Scheduler sched = quartzScheduler;
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void shutdownSchedule() {
try {
Scheduler sched = quartzScheduler;
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void pauseJob(String jobName, String jobGroupName) {
try {
quartzScheduler.pauseJob( JobKey.jobKey(jobName, jobGroupName));
} catch (SchedulerException e) {
e.printStackTrace();
} } @Override
public void resumeJob(String jobName, String jobGroupName) {
try {
quartzScheduler.resumeJob(JobKey.jobKey(jobName, jobGroupName));
} catch (SchedulerException e) {
e.printStackTrace();
}
} }
添加QuartzController以及WEB就在此忽略了,附件部分是项目的代码,仅供参考和学习;