一.项目背景
公司某项目使用lvs+keepalived+nginx部署高可用集群,但是定时任务使用的spring schedule,java timer等,导致多台服务器会同时运行定时任务,这是与业务功能不符的。使用quartz调度框架可以解决单机部署问题。
二.quartz基本环境准备
1.数据库导入
quartz集群的实现方式在于11张表,集群节点相互之间不通信,而是通过定时任务持久化加锁的方式来实现集群
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(120) NOT NULL,
JOB_NAME VARCHAR(200) NOT NULL,
JOB_GROUP VARCHAR(200) NOT NULL,
DESCRIPTION VARCHAR(250) NULL,
JOB_CLASS_NAME VARCHAR(250) NOT NULL,
IS_DURABLE VARCHAR(1) NOT NULL,
IS_NONCONCURRENT VARCHAR(1) NOT NULL,
IS_UPDATE_DATA VARCHAR(1) NOT NULL,
REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP))
ENGINE=InnoDB;
CREATE TABLE QRTZ_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
JOB_NAME VARCHAR(200) NOT NULL,
JOB_GROUP VARCHAR(200) NOT NULL,
DESCRIPTION VARCHAR(250) NULL,
NEXT_FIRE_TIME BIGINT(13) NULL,
PREV_FIRE_TIME BIGINT(13) NULL,
PRIORITY INTEGER NULL,
TRIGGER_STATE VARCHAR(16) NOT NULL,
TRIGGER_TYPE VARCHAR(8) NOT NULL,
START_TIME BIGINT(13) NOT NULL,
END_TIME BIGINT(13) NULL,
CALENDAR_NAME VARCHAR(200) NULL,
MISFIRE_INSTR SMALLINT(2) 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))
ENGINE=InnoDB;
CREATE TABLE QRTZ_SIMPLE_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
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 (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;
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),
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))
ENGINE=InnoDB;
CREATE TABLE QRTZ_SIMPROP_TRIGGERS
(
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
STR_PROP_1 VARCHAR(512) NULL,
STR_PROP_2 VARCHAR(512) NULL,
STR_PROP_3 VARCHAR(512) 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(13,4) NULL,
DEC_PROP_2 NUMERIC(13,4) NULL,
BOOL_PROP_1 VARCHAR(1) NULL,
BOOL_PROP_2 VARCHAR(1) 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))
ENGINE=InnoDB;
CREATE TABLE QRTZ_BLOB_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
BLOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
INDEX (SCHED_NAME,TRIGGER_NAME, TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;
CREATE TABLE QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR(200) NOT NULL,
CALENDAR BLOB NOT NULL,
PRIMARY KEY (SCHED_NAME,CALENDAR_NAME))
ENGINE=InnoDB;
CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;
CREATE TABLE QRTZ_FIRED_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
ENTRY_ID VARCHAR(95) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
INSTANCE_NAME VARCHAR(200) NOT NULL,
FIRED_TIME BIGINT(13) NOT NULL,
SCHED_TIME BIGINT(13) NOT NULL,
PRIORITY INTEGER NOT NULL,
STATE VARCHAR(16) NOT NULL,
JOB_NAME VARCHAR(200) NULL,
JOB_GROUP VARCHAR(200) NULL,
IS_NONCONCURRENT VARCHAR(1) NULL,
REQUESTS_RECOVERY VARCHAR(1) NULL,
PRIMARY KEY (SCHED_NAME,ENTRY_ID))
ENGINE=InnoDB;
CREATE TABLE QRTZ_SCHEDULER_STATE (
SCHED_NAME VARCHAR(120) NOT NULL,
INSTANCE_NAME VARCHAR(200) NOT NULL,
LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
CHECKIN_INTERVAL BIGINT(13) NOT NULL,
PRIMARY KEY (SCHED_NAME,INSTANCE_NAME))
ENGINE=InnoDB;
CREATE TABLE QRTZ_LOCKS (
SCHED_NAME VARCHAR(120) NOT NULL,
LOCK_NAME VARCHAR(40) NOT NULL,
PRIMARY KEY (SCHED_NAME,LOCK_NAME))
ENGINE=InnoDB;
CREATE INDEX IDX_QRTZ_J_REQ_RECOVERY ON QRTZ_JOB_DETAILS(SCHED_NAME,REQUESTS_RECOVERY);
CREATE INDEX IDX_QRTZ_J_GRP ON QRTZ_JOB_DETAILS(SCHED_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_T_J ON QRTZ_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_T_JG ON QRTZ_TRIGGERS(SCHED_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_T_C ON QRTZ_TRIGGERS(SCHED_NAME,CALENDAR_NAME);
CREATE INDEX IDX_QRTZ_T_G ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);
CREATE INDEX IDX_QRTZ_T_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_N_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_N_G_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_NEXT_FIRE_TIME ON QRTZ_TRIGGERS(SCHED_NAME,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_ST ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE_GRP ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_GROUP,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_FT_TRIG_INST_NAME ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME);
CREATE INDEX IDX_QRTZ_FT_INST_JOB_REQ_RCVRY ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME,REQUESTS_RECOVERY);
CREATE INDEX IDX_QRTZ_FT_J_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_FT_JG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_FT_T_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);
CREATE INDEX IDX_QRTZ_FT_TG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);
commit;
2.导入maven依赖
org.quartz-scheduler
quartz
org.quartz-scheduler
quartz-jobs
org.springframework
spring-context-support
3.配置文件
#quartz集群配置
#调度标识名 集群中每一个实例都必须使用相同的名称
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
#ID设置为自动获取 每一个必须不同
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.makeSchedulerThreadDaemon=true
#线程池的实现类(一般使用SimpleThreadPool即可满足需求)
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
#指定在线程池里面创建的线程是否是守护线程
org.quartz.threadPool.makeThreadsDaemons=true
#指定线程数,至少为1(无默认值)
org.quartz.threadPool.threadCount:20
#设置线程的优先级(最大为java.lang.Thread.MAX_PRIORITY 10,最小为Thread.MIN_PRIORITY 1,默认为5)
org.quartz.threadPool.threadPriority:5
#数据保存方式为数据库持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#数据库代理类,一般org.quartz.impl.jdbcjobstore.StdJDBCDelegate可以满足大部分数据库
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#表的前缀,默认QRTZ_
org.quartz.jobStore.tablePrefix=QRTZ_
#是否加入集群
org.quartz.jobStore.isClustered=true
# 信息保存时间 默认值60秒
org.quartz.jobStore.misfireThreshold=25000
三.springboot configuration 配置
package com.deepexi.cyj.springbootquartz.config;
import com.deepexi.cyj.springbootquartz.job.HelloJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
@Configuration
public class QuartzConfigure {
// 配置文件路径
private static final String QUARTZ_CONFIG = "/quartz.properties";
@Autowired
@Qualifier(value = "dataSource")
private DataSource dataSource;
//每两秒执行一次
private String cronExpression = "0/2 * * * * ?";
/**
* 从quartz.properties文件中读取Quartz配置属性
* @return
* @throws IOException
*/
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource(QUARTZ_CONFIG));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
/**
* JobFactory与schedulerFactoryBean中的JobFactory相互依赖,注意bean的名称
* 在这里为JobFactory注入了Spring上下文
*
* @param applicationContext
* @return
*/
@Bean
public JobFactory buttonJobFactory(ApplicationContext applicationContext) {
AutoWiredSpringBeanToJobFactory jobFactory = new AutoWiredSpringBeanToJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
/**
*
* @param buttonJobFactory 为SchedulerFactory配置JobFactory
* @return
* @throws IOException
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory buttonJobFactory) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(buttonJobFactory);
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(true); // 设置自行启动
factory.setQuartzProperties(quartzProperties());
//factory.setTriggers(cronJobTrigger);
factory.setDataSource(dataSource);// 使用应用的dataSource替换quartz的dataSource
return factory;
}
/**
* 配置JobDetailFactory
* JobDetailFactoryBean与CronTriggerFactoryBean相互依赖,注意bean的名称
*
* @return
*/
/*@Bean
public JobDetailFactoryBean buttonobDetail() {
//集群模式下必须使用JobDetailFactoryBean,MethodInvokingJobDetailFactoryBean 类中的 methodInvoking 方法,是不支持序列化的
JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
jobDetail.setDurability(true);
jobDetail.setRequestsRecovery(true);
jobDetail.setJobClass(HelloJob.class);
return jobDetail;
}
*//**
* 配置具体执行规则
* @param buttonobDetail
* @return
*//*
@Bean
public CronTriggerFactoryBean cronJobTrigger(JobDetail buttonobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(buttonobDetail);
tigger.setStartDelay(2000); //延迟启动
tigger.setCronExpression(cronExpression); //从application.yml文件读取
return tigger;
}*/
}
为JobFactory注入SpringBean,否则Job无法使用Spring创建的bean
package com.deepexi.cyj.springbootquartz.config;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
*
* @author Button
* 为JobFactory注入SpringBean,否则Job无法使用Spring创建的bean
*/
public class AutoWiredSpringBeanToJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
四、根据业务增删改查调度任务
1.定时任务实体类
package com.deepexi.cyj.springbootquartz.bean;
import com.alibaba.fastjson.JSONObject;
import org.quartz.Job;
import java.io.Serializable;
import java.util.Map;
/**
* className:TaskInfo
* description:
* author:ChenYajun
* date:2019-12-18 11:18
*/
public class TaskInfo implements Serializable {
private static final long serialVersionUID = -8054692082716173379L;
private int id = 0;
/**任务名称*/
private String jobName;
/**任务分组*/
private String jobGroup;
/**任务描述*/
private String jobDescription;
/**任务状态*/
private String jobStatus;
/**任务表达式*/
private String cronExpression;
private String createTime;
//job 类型
private Class extends Job> jobClassName;
private Map paramMap;
public Map getParamMap() {
return paramMap;
}
public void setParamMap(Map paramMap) {
this.paramMap = paramMap;
}
public Class extends Job> getJobClassName() {
return jobClassName;
}
public void setJobClassName(Class extends Job> jobClassName) {
this.jobClassName = jobClassName;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public String getJobGroup() {
return jobGroup;
}
public void setJobGroup(String jobGroup) {
this.jobGroup = jobGroup;
}
public String getJobDescription() {
return jobDescription;
}
public void setJobDescription(String jobDescription) {
this.jobDescription = jobDescription;
}
public String getJobStatus() {
return jobStatus;
}
public void setJobStatus(String jobStatus) {
this.jobStatus = jobStatus;
}
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
2.定时任务操作service
package com.deepexi.cyj.springbootquartz.service;
import com.deepexi.cyj.springbootquartz.bean.TaskInfo;
import org.quartz.SchedulerException;
import java.util.List;
/**
* className:TaskService
* description:
* author:ChenYajun
* date:2019-12-18 11:19
*/
public interface TaskService {
List list();
void addJob(TaskInfo info);
void edit(TaskInfo info);
void delete(String jobName, String jobGroup);
void pause(String jobName, String jobGroup);
void resume(String jobName, String jobGroup);
boolean checkExists(String jobName, String jobGroup) throws SchedulerException, SchedulerException;
}
service实现层
package com.deepexi.cyj.springbootquartz.service.impl;
import com.deepexi.cyj.springbootquartz.bean.TaskInfo;
import com.deepexi.cyj.springbootquartz.service.TaskService;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* className:TaskServiceImpl
* description:
* author:ChenYajun
* date:2019-12-18 11:19
*/
@Service
public class TaskServiceImpl implements TaskService {
private Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
@Autowired(required=false)
private SchedulerFactoryBean schedulerFactoryBean;;
/**
* 所有任务列表
*/
@Override
public List list(){
List list = new ArrayList<>();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
for(String groupJob: scheduler.getJobGroupNames()){
for(JobKey jobKey: scheduler.getJobKeys(GroupMatcher.groupEquals(groupJob))){
List extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
for (Trigger trigger: triggers) {
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
String cronExpression = "", createTime = "";
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
cronExpression = cronTrigger.getCronExpression();
createTime = cronTrigger.getDescription();
}
TaskInfo info = new TaskInfo();
info.setJobName(jobKey.getName());
info.setJobGroup(jobKey.getGroup());
info.setJobDescription(jobDetail.getDescription());
info.setJobStatus(triggerState.name());
info.setCronExpression(cronExpression);
info.setCreateTime(createTime);
info.setJobClassName(jobDetail.getJobClass());
list.add(info);
}
}
}
} catch (SchedulerException e) {
e.printStackTrace();
}
return list;
}
/**
* 保存定时任务
* @param info
*/
@SuppressWarnings("unchecked")
@Override
public void addJob(TaskInfo info) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String jobName = info.getJobName(),
jobGroup = info.getJobGroup(),
cronExpression = info.getCronExpression(),
jobDescription = info.getJobDescription(),
createTime = simpleDateFormat.format(new Date());
try {
if (checkExists(jobName, jobGroup)) {
logger.info("add job fail, job already exist, jobGroup:{}, jobName:{}", jobGroup, jobName);
}
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
CronScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withDescription(createTime).withSchedule(schedBuilder).build();
Class extends Job> clazz = (Class extends Job>)Class.forName(info.getJobClassName().getName());
JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobKey).withDescription(jobDescription).build();
for(Map.Entry entry:info.getParamMap().entrySet())
{
jobDetail.getJobDataMap().put(entry.getKey(),entry.getValue());
}
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException | ClassNotFoundException e) {
logger.error("类名不存在或执行表达式错误,exception:{}",e.getMessage());
}
}
/**
* 修改定时任务
* @param info
*/
@Override
public void edit(TaskInfo info) {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Scheduler scheduler = schedulerFactoryBean.getScheduler();
String jobName = info.getJobName(),
jobGroup = info.getJobGroup(),
cronExpression = info.getCronExpression(),
jobDescription = info.getJobDescription(),
createTime = simpleDateFormat.format(new Date());
try {
if (!checkExists(jobName, jobGroup)) {
logger.info("edit job fail, job is not exist, jobGroup:{}, jobName:{}", jobGroup, jobName);
}
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
JobKey jobKey = new JobKey(jobName, jobGroup);
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withDescription(createTime).withSchedule(cronScheduleBuilder).build();
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
jobDetail.getJobBuilder().withDescription(jobDescription);
HashSet triggerSet = new HashSet<>();
triggerSet.add(cronTrigger);
scheduler.scheduleJob(jobDetail, triggerSet, true);
} catch (SchedulerException e) {
logger.error("类名不存在或执行表达式错误,exception:{}",e.getMessage());
}
}
/**
* 删除定时任务
* @param jobName
* @param jobGroup
*/
@Override
public void delete(String jobName, String jobGroup){
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
try {
if (scheduler.checkExists(jobKey)) {
scheduler.deleteJob(jobKey);
logger.info("delete job,jobGroup:{}, jobName:{}", jobGroup, jobName);
}
} catch (SchedulerException e) {
logger.error(e.getMessage());
}
}
/**
* 暂停定时任务
* @param jobName
* @param jobGroup
*/
@Override
public void pause(String jobName, String jobGroup){
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
try {
if (checkExists(jobName, jobGroup)) {
scheduler.pauseTrigger(triggerKey);
logger.info("pause job success, triggerKey:{},jobGroup:{}, jobName:{}", triggerKey ,jobGroup, jobName);
}
} catch (SchedulerException e) {
logger.error(e.getMessage());
}
}
/**
* 重新开始任务
* @param jobName
* @param jobGroup
*/
@Override
public void resume(String jobName, String jobGroup){
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
if (checkExists(jobName, jobGroup)) {
scheduler.resumeTrigger(triggerKey);
logger.info("resume job success,triggerKey:{},jobGroup:{}, jobName:{}", triggerKey ,jobGroup, jobName);
}
} catch (SchedulerException e) {
logger.error(e.getMessage());
}
}
/**
* 验证是否存在
* @param jobName
* @param jobGroup
* @throws SchedulerException
*/
@Override
public boolean checkExists(String jobName, String jobGroup) throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
return scheduler.checkExists(triggerKey);
}
}
定时任务Job
package com.deepexi.cyj.springbootquartz.job;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* className:HelloJob
* description:
* author:ChenYajun
* date:2019-12-15 22:19
*/
@Slf4j
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//jobExecutionContext.getJobDetail().getJobDataMap()为传入的参数
log.info(">>>>>>hello定时任务执行,消息为:{}<<<<<<",jobExecutionContext.getJobDetail().getJobDataMap().get("msg"));
}
}
五.增删改查定时任务例子
package com.deepexi.cyj.springbootquartz.controller;
import com.alibaba.fastjson.JSONObject;
import com.deepexi.cyj.springbootquartz.bean.TaskInfo;
import com.deepexi.cyj.springbootquartz.job.HelloJob;
import com.deepexi.cyj.springbootquartz.job.WarningJob;
import com.deepexi.cyj.springbootquartz.service.TaskService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* className:DemoController
* description:
* author:ChenYajun
* date:2019-12-15 21:43
*/
@RestController
public class DemoController {
@Autowired
private TaskService taskService;
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
private final String expression = "0/2 * * * * ?";//每2秒钟执行一次
@RequestMapping("/list")
public String getAllJob()
{
List list = taskService.list();
return list.toString();
}
@RequestMapping("/addJob")
public String addJob()
{
TaskInfo taskInfo = new TaskInfo();
taskInfo.setCronExpression("0/2 * * * * ?");
taskInfo.setJobClassName(HelloJob.class);
taskInfo.setJobDescription("HelloJobDesc");
taskInfo.setJobName("test1JobName");
taskInfo.setJobGroup("test1JobGroup");
Map paramMap = new HashMap<>();
paramMap.put("msg","传入的告警信息");
taskInfo.setParamMap(paramMap);
taskService.addJob(taskInfo);
return "success";
}
@RequestMapping("/deleteJob")
public String deleteJob(String jobName,String jobGroup)
{
taskService.delete(jobName,jobGroup);
return "OK";
}
@RequestMapping("/editJob")
public String editJob()
{
TaskInfo taskInfo = new TaskInfo();
taskInfo.setCronExpression("0/4 * * * * ?");
taskInfo.setJobClassName(HelloJob.class);
taskInfo.setJobDescription("HelloJobDesc");
taskInfo.setJobName("test1JobName");
taskInfo.setJobGroup("test1JobGroup");
taskService.edit(taskInfo);
return "OK";
}
@RequestMapping("/pauseJob")
public String pauseJob(String jobName,String jobGroup) {
taskService.pause(jobName,jobGroup);
return "OK";
}
@RequestMapping("/resumeJob")
public String resumeJob(String jobName,String jobGroup) {
taskService.resume(jobName,jobGroup);
return "OK";
}
@RequestMapping("/startJob/{warningId}")
public String startJob(@PathVariable String warningId) throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
if(scheduler.checkExists(new JobKey(warningId, "warningJobGroup")))
{
return "任务已存在";
}
// 3. 使用上文定义的 HelloJob
JobDetail jobDetail = JobBuilder.newJob(WarningJob.class)
//job 的name和group
.withIdentity(warningId, "warningJobGroup")
.build();
jobDetail.getJobDataMap().put("warningId",warningId);
Trigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule(expression)) //每一分钟执行一次
.build();
scheduler.scheduleJob(jobDetail, trigger);
return "任务开启成功";
}
@RequestMapping("/stopJob/{warningId}")
public String stopJob(@PathVariable String warningId) throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
if(scheduler.checkExists(new JobKey(warningId, "warningJobGroup")))
{
scheduler.deleteJob(new JobKey(warningId, "warningJobGroup"));
}
return "任务关闭成功";
}
}
六,工程码云地址
https://gitee.com/cyj306/springboot-quartz