本文是基于SpringBoot java代码配置完成的 Quartz持久化是基于Mysql数据库 持久化的原因是防止服务器宕机丢掉所在执行的任务
一:SpringBoot代码配置
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
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;
/**
* @ClassName: QuartzConifg
* @Description: TODO(Quartz定时任务配置)
* @author: mengxr
* @date 2017年5月10日 下午2:08:16
*/
@Configuration
public class QuartzConifgue {
@Bean
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource,
@Qualifier("JobFactory") QuartzSpringJobFactory jobFactory) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// this allows to update triggers in DB when updating settings in config file:
factory.setOverwriteExistingJobs(true);
factory.setJobFactory(jobFactory);
factory.setDataSource(dataSource);
factory.setQuartzProperties(quartzProperties());
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean("JobFactory")
public QuartzSpringJobFactory JobFactory(){
QuartzSpringJobFactory jobFactory = new QuartzSpringJobFactory();
return jobFactory;
}
}
二:把Quartz放入Spring管理容器 (不配置的话在定义的Job任务中不能注入)
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;
/**
* 由于job是在quartz中实例化出来的 ,把Quartz放入Spring管理
* @ClassName: QuartzSpringJobFactory
* @Description: TODO()
* @author: mengxr
* @date 2017年5月11日 下午2:08:48
*/
public class QuartzSpringJobFactory extends AdaptableJobFactory {
//这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入,这属于Spring的技术,不清楚的可以查看Spring的API.
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
三:Quartz配置文件配置
org.quartz.scheduler.instanceName:meipro-notice
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 5
org.quartz.threadPool.threadPriority: 5
org.quartz.jobStore.misfireThreshold: 60000
# 内存配置 org.quartz.simpl.RAMJobStore
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
四:Quartz工具类
import java.util.Date;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @ClassName: QuartzTools
* @Description: TODO(任务调度工具类)
* @author: mengxr
* @date 2017年5月10日 下午5:23:36
*/
@Service
public class QuartzTools {
@Autowired
private Scheduler scheduler;
// 日志处理
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* @throws NoticeExeception
* @Title: addJob
* @Description: TODO(添加一个Job)
* @param @param name
* @param @param group
* @param @param job
* @param @param date
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月10日 下午5:46:06
* @throws
*/
public boolean addJob(String name, String group,
Class<? extends Job> jobclazz, Date date,JobDataMap newJobDataMap) throws NoticeExeception {
boolean flag = false;
try {
JobDetail jobDetail = JobBuilder.newJob(jobclazz).setJobData(newJobDataMap)
.withIdentity(name, group).build(); // 1、创建一个JobDetail实例,指定Quartz
// 创建任务触发器
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(name, group)
.withSchedule(
CronScheduleBuilder.cronSchedule(CronDateUtils
.getCron(date))).build();
scheduler.scheduleJob(jobDetail, trigger);
flag = true;
logger.info("创建定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 成功,定时执行时间:[{}]", date);
} catch (SchedulerException e) {
flag = false;
logger.info("创建定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,定时执行时间:[{}]", date, e);
throw new NoticeExeception("notice.addJob");
}
return flag;
}
/**
* @throws NoticeExeception
* @Title: modifyJobTime
* @Description: TODO(执行时间)
* @param @param name
* @param @param group
* @param @param date
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月10日 下午6:05:23
* @throws
*/
public boolean modifyJobTime(String name, String group, Date date) throws NoticeExeception {
boolean flag = false;
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
// 构造任务触发器
Trigger trg = TriggerBuilder
.newTrigger()
.withIdentity(name, group)
.withSchedule(
CronScheduleBuilder.cronSchedule(CronDateUtils
.getCron(date))).build();
scheduler.rescheduleJob(tk, trg);
flag = true;
logger.info("修改定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 成功,修改执行时间:[{}]", date);
} catch (SchedulerException e) {
flag = false;
logger.info("修改定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,修改执行时间:[{}]", date, e);
throw new NoticeExeception("notice.modifyJobTime");
}
return flag;
}
/**
* @throws NoticeExeception
* @Title: parseJob
* @Description: TODO(暂停任务)
* @param @param name
* @param @param group
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月10日 下午6:14:47
* @throws
*/
public boolean parseJob(String name, String group) throws NoticeExeception {
boolean flag = false;
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.pauseJob(jobKey);
flag = true;
logger.info("暂停定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 成功,暂停执行时间:[{}]", new Date());
} catch (SchedulerException e) {
flag = false;
logger.info("暂停定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,暂停执行时间:[{}]", new Date(),e);
throw new NoticeExeception("notice.parseJob");
}
return flag;
}
/**
* @throws NoticeExeception
* @Title: deleteJob
* @Description: TODO(删除任务)
* @param @param name
* @param @param group
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月11日 下午2:03:46
* @throws
*/
public boolean deleteJob(String name, String group) throws NoticeExeception {
boolean flag = false;
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.deleteJob(jobKey);
flag = true;
logger.info("删除定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 成功,暂停执行时间:[{}]", new Date());
} catch (SchedulerException e) {
flag = false;
logger.info("删除定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,暂停执行时间:[{}]", new Date(),e);
throw new NoticeExeception("notice.deleteJob");
}
return flag;
}
/**
* @throws NoticeExeception
* @Title: resumeJob
* @Description: TODO(恢复任务执行)
* @param @param name
* @param @param group
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月10日 下午6:17:01
* @throws
*/
public boolean resumeJob(String name, String group) throws NoticeExeception {
boolean flag = false;
try {
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.resumeJob(jobKey);
flag = true;
logger.info("恢复定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 成功,恢复执行时间:[{}]", new Date());
} catch (SchedulerException e) {
flag = false;
logger.info("恢复定时任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,恢复执行时间:[{}]", new Date(),e);
throw new NoticeExeception("notice.resumeJob");
}
return flag;
}
/**
* @throws NoticeExeception
* @Title: isExistJob
* @Description: TODO(检测任务是否存在)
* @param @param name
* @param @param group
* @param @return 设定文件
* @return boolean 返回类型
* @author: mengxr
* @date 2017年5月11日 上午10:44:09
* @throws
*/
public boolean isExistJob(String name, String group) throws NoticeExeception{
boolean flag = false;
try {
JobKey jobKey = JobKey.jobKey(name, group);
flag = scheduler.checkExists(jobKey);
} catch (SchedulerException e) {
logger.info("检测任务=> [任务名称:" + name + " 任务组:" + group
+ "] 失败,检测执行时间:[{}]", new Date(),e);
throw new NoticeExeception("notice.isExistJob");
}
return flag;
}
}
五:Quartz Mysql数据库表初始化(基于innodb)
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;
六:创建一个Job任务
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @ClassName: NoticeJob
* @Description: TODO(公告JOB任务 ,添加定时任务)
* @author: mengxr
* @date 2017年5月10日 下午6:58:13
*/
public class NoticeJob implements Job {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private INoticeService iNoticeService;
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
Long noticeId = null;
Short noticeState = null;
try {
//获取JobDataMap
JobDataMap dataMap = context.getJobDetail().
getJobDataMap();
noticeId = (Long) dataMap.get("noticeId"); //获取公告ID
noticeState = (Short) dataMap.get("noticeState"); //获取公告类型
iNoticeService.updateNoticeTargetState(new NoticeVo(noticeId, noticeState)); //修改公告目标
logger.info("定时任务执行成功公告主键[{}],公告类型[{}]修改公告目标",noticeId,noticeState);
} catch (Exception e) {
logger.error("定时任务公告主键[{}],公告类型[{}]修改公告目标失败",noticeId,noticeState,e);
}
}
}
七:调用Job任务(一小段代码)
private void startNoticeJob(NoticeVo notice) throws MeiproException {
Long noticeId = notice.getId();
Short noticeState = notice.getState();
if (State.timeinterval.equals(noticeState)) { // 如果状态为定时发布开启定时任务
Date sendtime = notice.getSendtime();
boolean flag = quartzTools.isExistJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
JobDataMap jobData = new JobDataMap();
jobData.put("noticeId", noticeId);
jobData.put("noticeState", State.publish);
if(flag){
if(null != sendtime){
quartzTools.deleteJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
quartzTools.addJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup, NoticeJob.class,
sendtime, jobData);
}else{
//恢复任务
quartzTools.resumeJob(String.valueOf(noticeId), NoticeConstant.quartzGroup);
}
}else{
quartzTools.addJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup, NoticeJob.class,
sendtime, jobData);
}
} else if (State.publish.equals(noticeState)) {// 如果状态为 发布公告
boolean flag = quartzTools.isExistJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
if(flag){
quartzTools.deleteJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
}
} else if (State.draft.equals(noticeState) ||
State.recall.equals(noticeState) ||
State.delete.equals(noticeState)) {
boolean flag = quartzTools.isExistJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
if(flag){
quartzTools.deleteJob(String.valueOf(noticeId),
NoticeConstant.quartzGroup);
}
}
}