博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3整合Quartz 2实现定时任务:动态添加任务
阅读量:5330 次
发布时间:2019-06-14

本文共 6139 字,大约阅读时间需要 20 分钟。

先展示一下后台管理定时任务效果图:

1、新增任务页面:

 

 

2、列表页(实现任务的禁用启用)

 

 

3、数据库脚本:

-- ----------------------------

-- Table structure for schedule_job
-- ----------------------------
DROP TABLE IF EXISTS `schedule_job`;
CREATE TABLE `schedule_job` (
`jobid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '任务ID',
`jobname` varchar(40) DEFAULT NULL COMMENT '任务名称',
`jobgroup` varchar(40) DEFAULT NULL COMMENT '任务分组',
`jobstatus` char(1) NOT NULL DEFAULT '1' COMMENT '任务状态 0禁用 1启用',
`auditstatus` char(1) NOT NULL DEFAULT '0' COMMENT '审核状态 0 已创建 1 审核通过 2 审核驳回',
`cronexpression` varchar(40) NOT NULL COMMENT '任务运行时间表达式',
`quartzclass` varchar(255) DEFAULT NULL COMMENT '定时任务处理类',
`description` varchar(280) DEFAULT NULL COMMENT '描述信息',
PRIMARY KEY (`jobid`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

 

 

4、定时任务后台处理类(核心):

package com.eb.admin.schedule;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.quartz.CronScheduleBuilder;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
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.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.eb.admin.entity.ScheduleJob;

import com.eb.admin.service.ConfigService;

/**

* 调度工厂类
*
* @author Jason.Yan
* @since 2018/03/28
*
*/
@Configuration
@EnableScheduling
@Component
public class ScheduleFactory {

private static Logger logger = LoggerFactory.getLogger(ScheduleFactory.class);

private Map<String, String> jobUniqueMap = new HashMap<String, String>(); // 当前Trigger使用的
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;

public SchedulerFactoryBean getSchedulerFactoryBean() {

return schedulerFactoryBean;
}

public void setSchedulerFactoryBean(SchedulerFactoryBean schedulerFactoryBean) {

this.schedulerFactoryBean = schedulerFactoryBean;
}

@Autowired

private ConfigService configService;

public ConfigService getConfigService() {

return configService;
}

public void setConfigService(ConfigService configService) {

this.configService = configService;
}

//TODO 此处暂且注释,后续有后台定时任务逻辑 开启

@Scheduled(fixedRate = 5000) // 每隔5s查库,并根据查询结果决定是否重新设置定时任务
public void scheduleUpdateCronTrigger() throws Exception {

try {

// schedulerFactoryBean 由spring创建注入
Scheduler scheduler = schedulerFactoryBean.getScheduler();
List<ScheduleJob> jobList = configService.findLegalJobList();
// 获取最新删除(禁用)任务列表,将其从调度器中删除,并且从jobUniqueMap中删除
List<ScheduleJob> jobDelList = configService.findDelJobList();
for (ScheduleJob delJob : jobDelList) {
JobKey jobKey = JobKey.jobKey(delJob.getJobName(), delJob.getJobGroup());
scheduler.deleteJob(jobKey);
jobUniqueMap.remove(TriggerKey.triggerKey(delJob.getJobName(), delJob.getJobGroup()));
}
for (ScheduleJob job : jobList) {
TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
// 获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
String dbCron = job.getCronExpression(); // 该job数据库中的Trigger表达式
// 不存在,创建一个
if (null == trigger) {
//JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(job.getJobName(), job.getJobGroup()).build();
try{
@SuppressWarnings("unchecked")
Class <? extends Job> clazz = (Class<? extends Job>) Class.forName(job.getQuartzClass());
JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build();
jobDetail.getJobDataMap().put("scheduleJob", job);
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
// 按新的cronExpression表达式构建一个新的trigger
trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
jobUniqueMap.put(triggerKey.toString(), trigger.getCronExpression());
//currentCron = trigger.getCronExpression();
scheduler.scheduleJob(jobDetail, trigger);
}catch(Exception e){
e.printStackTrace();
logger.error(e.getMessage());
}
} else if(!jobUniqueMap.get(triggerKey.toString()).equals(dbCron)){
// Trigger已存在,那么更新相应的定时设置
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(dbCron);
// 按新的cronExpression表达式重新构建trigger
trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
jobUniqueMap.put(triggerKey.toString(), dbCron);
}
}

} catch (Exception e) {

e.printStackTrace();
}
}
}

 

5、后台查询sql:

ConfigService.findLegalJobList=select * from schedule_job where jobstatus = 1 and auditstatus = 1

ConfigService.findDelJobList=select * from schedule_job where jobstatus = 0

 

6、后台查询方法实现类:

package com.eb.admin.service.impl;

import java.util.List;

import com.eb.admin.entity.ScheduleJob;

import com.eb.admin.service.ConfigService;
import com.eb.dataservice.dao.CommonDao;
import com.eb.dataservice.dao.SqlUtils;

public class ConfigServiceImpl implements ConfigService {

@SuppressWarnings("unchecked")
@Override
public List<ScheduleJob> findLegalJobList() throws Exception {
CommonDao dao = CommonDao.getDao(dbkey);
String sql = SqlUtils.getSql("ConfigService.findLegalJobList");
return dao.findBeanList(ScheduleJob.class, sql);
}

@Override

public List<ScheduleJob> findDelJobList() throws Exception {
CommonDao dao = CommonDao.getDao(dbkey);
String sql = SqlUtils.getSql("ConfigService.findDelJobList");
return dao.findBeanList(ScheduleJob.class, sql);
}

}

 

以上为本人根据两篇博文所做的整理修改,原文参见:

https://blog.csdn.net/liuchuanhong1/article/details/60873295#reply

https://www.ktanx.com/blog/p/308

本着尊重原作者的态度,转载请注明出处:

http://www.cnblogs.com/Ivan-j2ee/p/8697054.html

转载于:https://www.cnblogs.com/Ivan-j2ee/p/8697054.html

你可能感兴趣的文章
前后端交互
查看>>
linux下各文件夹的结构说明及用途介绍
查看>>
知晓云,不用申请服务器即可完成小程序后台开发和部署
查看>>
第八次课堂笔记
查看>>
php排序笔记-冒泡-选择-插入-希尔-堆排序
查看>>
6.Type and Member Basics
查看>>
JuneX_13
查看>>
Android4.2 原生bug,Daydream 中 点击DeskClock 中的settings button crash
查看>>
Win7安装.zip(绿色版)MySQL
查看>>
c++ 模板和traits
查看>>
QB资料学习.01
查看>>
高斯消元(浮点数主列法消元,有剪枝细节..
查看>>
Nmap扫描原理(下)
查看>>
Dubbo源码分析(二)-----提供者启动过程分析
查看>>
两个三汇API使用的坑
查看>>
CSS3动画功能
查看>>
Docker - 创建Swarm
查看>>
基础练习 十进制转十六进制
查看>>
selenium上传下载文件
查看>>
Golang 网络爬虫框架gocolly/colly 五 获取动态数据
查看>>