Quartz 是 OpenSymphony 开源组织在 Job Scheduling 领域又一个开源项目,是完全由 Java 开发的一个开源任务日程管理系统,“任务进度管理器”就是一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。 Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中,它提供了巨大的灵活性而不牺牲简单性
当定时任务愈加复杂时,使用 Spring 注解 @Schedule 已经不能满足业务需要
在项目开发中,经常需要定时任务来帮助我们来做一些内容,如定时派息、跑批对账、将任务纳入日程或者从日程中取消,开始,停止,暂停日程进度等。SpringBoot 中现在有两种方案可以选择,第一种是 SpringBoot 内置的方式简单注解就可以使用,当然如果需要更复杂的应用场景还是得 Quartz 上场,Quartz 目前是 Java 体系中最完善的定时方案
官方网站:http://quartz-scheduler.org/
当然可以这样快速理解:
Job 为作业的接口,为任务调度的对象;JobDetail 用来描述 Job 的实现类及其他相关的静态信息;Trigger 做为作业的定时管理工具,一个 Trigger 只能对应一个作业实例,而一个作业实例可对应多个触发器;Scheduler 做为定时任务容器,是 Quartz 最上层的东西,它提携了所有触发器和作业,使它们协调工作,每个 Scheduler 都存有 JobDetail 和 Trigger 的注册,一个 Scheduler 中可以注册多个 JobDetail 和多个 Trigger
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for qrtz_blob_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_blob_triggers`;
CREATE TABLE `qrtz_blob_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`BLOB_DATA` blob COMMENT '保存triggers 一些信息',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `SCHED_NAME` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_blob_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='自定义触发器';
-- ----------------------------
-- Table structure for qrtz_calendars
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_calendars`;
CREATE TABLE `qrtz_calendars` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`CALENDAR_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`CALENDAR` blob NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='以 Blob 类型存储 Quartz 的 Calendar 信息';
-- ----------------------------
-- Table structure for qrtz_cron_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_cron_triggers`;
CREATE TABLE `qrtz_cron_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`CRON_EXPRESSION` varchar(120) NOT NULL COMMENT '时间表达式',
`TIME_ZONE_ID` varchar(80) DEFAULT NULL COMMENT '时区ID',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_cron_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储 Cron Trigger,包括Cron表达式和时区信息';
-- ----------------------------
-- Table structure for qrtz_fired_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_fired_triggers`;
CREATE TABLE `qrtz_fired_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`ENTRY_ID` varchar(95) NOT NULL COMMENT '组标识',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`INSTANCE_NAME` varchar(200) NOT NULL COMMENT '当前实例的名称',
`FIRED_TIME` bigint(13) NOT NULL COMMENT '当前执行时间',
`SCHED_TIME` bigint(13) NOT NULL COMMENT '计划时间',
`PRIORITY` int(11) NOT NULL COMMENT '权重',
`STATE` varchar(16) NOT NULL COMMENT '状态',
`JOB_NAME` varchar(200) DEFAULT NULL COMMENT '作业名称',
`JOB_GROUP` varchar(200) DEFAULT NULL COMMENT '作业组',
`IS_NONCONCURRENT` varchar(1) DEFAULT NULL COMMENT '是否并行',
`REQUESTS_RECOVERY` varchar(1) DEFAULT NULL COMMENT '是否要求唤醒',
PRIMARY KEY (`SCHED_NAME`,`ENTRY_ID`),
KEY `IDX_QRTZ_FT_TRIG_INST_NAME` (`SCHED_NAME`,`INSTANCE_NAME`),
KEY `IDX_QRTZ_FT_INST_JOB_REQ_RCVRY` (`SCHED_NAME`,`INSTANCE_NAME`,`REQUESTS_RECOVERY`),
KEY `IDX_QRTZ_FT_J_G` (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`),
KEY `IDX_QRTZ_FT_JG` (`SCHED_NAME`,`JOB_GROUP`),
KEY `IDX_QRTZ_FT_T_G` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `IDX_QRTZ_FT_TG` (`SCHED_NAME`,`TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储与已触发的 Trigger 相关的状态信息,以及相联 Job的执行信息';
-- ----------------------------
-- Table structure for qrtz_job_details
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_job_details`;
CREATE TABLE `qrtz_job_details` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`JOB_NAME` varchar(200) NOT NULL COMMENT '作业名称',
`JOB_GROUP` varchar(200) NOT NULL COMMENT '作业组',
`DESCRIPTION` varchar(250) DEFAULT NULL COMMENT '描述',
`JOB_CLASS_NAME` varchar(250) NOT NULL COMMENT '作业程序集名称',
`IS_DURABLE` varchar(1) NOT NULL COMMENT '是否持久',
`IS_NONCONCURRENT` varchar(1) NOT NULL COMMENT '是否并行',
`IS_UPDATE_DATA` varchar(1) NOT NULL COMMENT '是否更新',
`REQUESTS_RECOVERY` varchar(1) NOT NULL COMMENT '是否要求唤醒',
`JOB_DATA` blob,
PRIMARY KEY (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='自定义触发器';
-- ----------------------------
-- Table structure for qrtz_locks
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_locks`;
CREATE TABLE `qrtz_locks` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`LOCK_NAME` varchar(40) NOT NULL COMMENT '锁名称',
PRIMARY KEY (`SCHED_NAME`,`LOCK_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储程序的悲观锁的信息(假如使用了悲观锁)';
-- ----------------------------
-- Table structure for qrtz_paused_trigger_grps
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_paused_trigger_grps`;
CREATE TABLE `qrtz_paused_trigger_grps` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储已暂停的 Trigger组的信息';
-- ----------------------------
-- Table structure for qrtz_scheduler_state
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_scheduler_state`;
CREATE TABLE `qrtz_scheduler_state` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`INSTANCE_NAME` varchar(200) NOT NULL COMMENT '实例名称',
`LAST_CHECKIN_TIME` bigint(13) NOT NULL COMMENT '最后的检查时间',
`CHECKIN_INTERVAL` bigint(13) NOT NULL COMMENT '检查间隔',
PRIMARY KEY (`SCHED_NAME`,`INSTANCE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储少量的有关 Scheduler 的状态信息,和别的Scheduler实例(假如是用于一个集群中)';
-- ----------------------------
-- Table structure for qrtz_simple_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_simple_triggers`;
CREATE TABLE `qrtz_simple_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`REPEAT_COUNT` bigint(7) NOT NULL COMMENT '重复次数',
`REPEAT_INTERVAL` bigint(12) NOT NULL COMMENT '触发次数',
`TIMES_TRIGGERED` bigint(10) NOT NULL COMMENT '重复间隔',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_simple_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储简单的Trigger,包括重复次数,间隔,以及已触的次数';
-- ----------------------------
-- Table structure for qrtz_simprop_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_simprop_triggers`;
CREATE TABLE `qrtz_simprop_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`STR_PROP_1` varchar(512) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`STR_PROP_2` varchar(512) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`STR_PROP_3` varchar(512) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`INT_PROP_1` int(11) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`INT_PROP_2` int(11) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`LONG_PROP_1` bigint(20) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`LONG_PROP_2` bigint(20) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`DEC_PROP_1` decimal(13,4) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`DEC_PROP_2` decimal(13,4) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`BOOL_PROP_1` varchar(1) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
`BOOL_PROP_2` varchar(1) DEFAULT NULL COMMENT '根据不同的trigger类型存放各自的参数',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `qrtz_simprop_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储CalendarIntervalTrigger和DailyTimeIntervalTrigger两种类型的触发器';
-- ----------------------------
-- Table structure for qrtz_triggers
-- ----------------------------
DROP TABLE IF EXISTS `qrtz_triggers`;
CREATE TABLE `qrtz_triggers` (
`SCHED_NAME` varchar(120) NOT NULL COMMENT '计划名称',
`TRIGGER_NAME` varchar(200) NOT NULL COMMENT '触发器名称',
`TRIGGER_GROUP` varchar(200) NOT NULL COMMENT '触发器组',
`JOB_NAME` varchar(200) NOT NULL COMMENT '作业名称',
`JOB_GROUP` varchar(200) NOT NULL COMMENT '作业组',
`DESCRIPTION` varchar(250) DEFAULT NULL COMMENT '描述',
`NEXT_FIRE_TIME` bigint(13) DEFAULT NULL COMMENT '下次执行时间',
`PREV_FIRE_TIME` bigint(13) DEFAULT NULL COMMENT '前一次执行时间',
`PRIORITY` int(11) DEFAULT NULL COMMENT '优先权',
`TRIGGER_STATE` varchar(16) NOT NULL COMMENT '触发器状态',
`TRIGGER_TYPE` varchar(8) NOT NULL COMMENT '触发器类型',
`START_TIME` bigint(13) NOT NULL COMMENT '开始时间',
`END_TIME` bigint(13) DEFAULT NULL COMMENT '结束时间',
`CALENDAR_NAME` varchar(200) DEFAULT NULL COMMENT '日历名称',
`MISFIRE_INSTR` smallint(2) DEFAULT NULL COMMENT '失败次数',
`JOB_DATA` blob COMMENT '作业数据',
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `IDX_QRTZ_T_J` (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`),
KEY `IDX_QRTZ_T_JG` (`SCHED_NAME`,`JOB_GROUP`),
KEY `IDX_QRTZ_T_C` (`SCHED_NAME`,`CALENDAR_NAME`),
KEY `IDX_QRTZ_T_G` (`SCHED_NAME`,`TRIGGER_GROUP`),
KEY `IDX_QRTZ_T_STATE` (`SCHED_NAME`,`TRIGGER_STATE`),
KEY `IDX_QRTZ_T_N_STATE` (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`,`TRIGGER_STATE`),
KEY `IDX_QRTZ_T_N_G_STATE` (`SCHED_NAME`,`TRIGGER_GROUP`,`TRIGGER_STATE`),
KEY `IDX_QRTZ_T_NEXT_FIRE_TIME` (`SCHED_NAME`,`NEXT_FIRE_TIME`),
KEY `IDX_QRTZ_T_NFT_ST` (`SCHED_NAME`,`TRIGGER_STATE`,`NEXT_FIRE_TIME`),
KEY `IDX_QRTZ_T_NFT_MISFIRE` (`SCHED_NAME`,`MISFIRE_INSTR`,`NEXT_FIRE_TIME`),
KEY `IDX_QRTZ_T_NFT_ST_MISFIRE` (`SCHED_NAME`,`MISFIRE_INSTR`,`NEXT_FIRE_TIME`,`TRIGGER_STATE`),
KEY `IDX_QRTZ_T_NFT_ST_MISFIRE_GRP` (`SCHED_NAME`,`MISFIRE_INSTR`,`NEXT_FIRE_TIME`,`TRIGGER_GROUP`,`TRIGGER_STATE`),
CONSTRAINT `qrtz_triggers_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`) REFERENCES `qrtz_job_details` (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='触发器的基本信息';
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
#如果您希望Quartz Scheduler通过RMI作为服务器导出本身,则将“rmi.export”标志设置为true
#在同一个配置文件中为'org.quartz.scheduler.rmi.export'和'org.quartz.scheduler.rmi.proxy'指定一个'true'值是没有意义的,如果你这样做'export'选项将被忽略
org.quartz.scheduler.rmi.export=false
#如果要连接(使用)远程服务的调度程序,则将“org.quartz.scheduler.rmi.proxy”标志设置为true。您还必须指定RMI注册表进程的主机和端口 - 通常是“localhost”端口1099
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
#实例化ThreadPool时,使用的线程类为SimpleThreadPool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
#threadCount和threadPriority将以setter的形式注入ThreadPool实例
#并发个数 如果你只有几个工作每天触发几次 那么1个线程就可以,如果你有成千上万的工作,每分钟都有很多工作 那么久需要50-100之间.
#只有1到100之间的数字是非常实用的
org.quartz.threadPool.threadCount=5
#优先级 默认值为5
org.quartz.threadPool.threadPriority=5
#可以是“true”或“false”,默认为false
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
#在被认为“misfired”(失火)之前,调度程序将“tolerate(容忍)”一个Triggers(触发器)将其下一个启动时间通过的毫秒数。默认值(如果您在配置中未输入此属性)为60000(60秒)
org.quartz.jobStore.misfireThreshold=5000
# 默认存储在内存中,RAMJobStore快速轻便,但是当进程终止时,所有调度信息都会丢失
#org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
#持久化方式,默认存储在内存中,此处使用数据库方式
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#您需要为JobStore选择一个DriverDelegate才能使用。DriverDelegate负责执行特定数据库可能需要的任何JDBC工作
# StdJDBCDelegate是一个使用“vanilla”JDBC代码(和SQL语句)来执行其工作的委托,用于完全符合JDBC的驱动程序
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#可以将“org.quartz.jobStore.useProperties”配置参数设置为“true”(默认为false),以指示JDBCJobStore将JobDataMaps中的所有值都作为字符串,
#因此可以作为名称 - 值对存储而不是在BLOB列中以其序列化形式存储更多复杂的对象。从长远来看,这是更安全的,因为您避免了将非String类序列化为BLOB的类版本问题
org.quartz.jobStore.useProperties=true
#表前缀
org.quartz.jobStore.tablePrefix=QRTZ_
关于配置详细解释:https://blog.csdn.net/zixiao217/article/details/53091812
也可以查看官网:http://www.quartz-scheduler.org/documentation/2.3.1-SNAPSHOT/
package com.schedule;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
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.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
/**
* 定时任务配置类
*/
@Configuration
public class ScheduleConfig {
@Autowired
private DataSource dataSource;
/*
* 通过SchedulerFactoryBean获取Scheduler的实例
*/
@Bean
public Scheduler scheduler() throws IOException {
return schedulerFactoryBean().getScheduler();
}
/**
* 通过配置文件读取对quartz参数
*/
@Bean
public Properties properties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
// 对quartz.properties文件进行读取
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
// 在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource);
schedulerFactoryBean.setSchedulerName("TestScheduler");
schedulerFactoryBean.setQuartzProperties(properties());
// 延时启动
schedulerFactoryBean.setStartupDelay(30);
schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
return schedulerFactoryBean;
}
}
package com.schedule;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
@Slf4j
public class ScheduleJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("正在执行的定时任务为" + jobExecutionContext.getMergedJobDataMap().get("ScheduleTask"));
String jsonJob = jobExecutionContext.getMergedJobDataMap().getString("ScheduleTask");
ScheduleJobEntity scheduleJob = JSON.parseObject(jsonJob, ScheduleJobEntity.class);
try {
Object bean = SpringContext.getBean(scheduleJob.getJbGroup());
Method declaredMethod = bean.getClass().getDeclaredMethod(scheduleJob.getJbName());
ReflectionUtils.makeAccessible(declaredMethod);
declaredMethod.invoke(bean);
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("定时任务结束");
}
}
package com.schedule.service;
import com.alibaba.fastjson.JSON;
import com.schedule.ScheduleJob;
import com.schedule.ScheduleJobEntity;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SchedulerServiceImpl implements SchedulerService {
@Autowired
private Scheduler scheduler;
/**
* 新增定时任务
*/
@Override
public void addjob(ScheduleJobEntity scheduleJobEntity) {
try {
// 构建JobDetail
JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class)
.withIdentity(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup())
.build();
// 按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup())
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(scheduleJobEntity.getCore()))
.build();
// 放入参数,运行时的方法可以获取
jobDetail.getJobDataMap().put("ScheduleTask", JSON.toJSONString(scheduleJobEntity));
scheduler.start();
// 启动调度器
scheduler.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
log.info("创建定时任务失败" + e);
}
}
// 暂停定时任务运行
@Override
public void pausejob(ScheduleJobEntity scheduleJobEntity) {
try {
scheduler.pauseJob(JobKey.jobKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup()));
} catch (SchedulerException e) {
e.getUnderlyingException();
}
}
// 恢复定时任务运行
@Override
public void resumejob(ScheduleJobEntity scheduleJobEntity) {
try {
scheduler.resumeJob(JobKey.jobKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup()));
} catch (SchedulerException e) {
e.getUnderlyingException();
}
}
// 更新定时任务
@Override
public void rescheduleJob(ScheduleJobEntity scheduleJobEntity) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup());
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJobEntity.getCore());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 参数
trigger.getJobDataMap().put("ScheduleTask", JSON.toJSONString(scheduleJobEntity));
// 按新的trigger重新设置job执行,重启触发器
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
e.getUnderlyingException();
}
}
// 删除定时任务
@Override
public void deletejob(ScheduleJobEntity scheduleJobEntity) {
try {
scheduler.pauseTrigger(TriggerKey.triggerKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup()));
scheduler.unscheduleJob(TriggerKey.triggerKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup()));
scheduler.deleteJob(JobKey.jobKey(scheduleJobEntity.getJbName(), scheduleJobEntity.getJbGroup()));
} catch (SchedulerException e) {
e.getUnderlyingException();
}
}
}
package com.schedule;
import com.schedule.service.SchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@Controller
@RequestMapping(path = "/quartz")
public class SchedulerController {
@Autowired
private SchedulerService quartzService;
/**
* 新增定时任务
*/
@PostMapping(path = "/addjob")
@ResponseBody
public String addjob(@RequestBody ScheduleJobEntity scheduleJobEntity) {
try {
quartzService.addjob(scheduleJobEntity);
return "添加任务成功";
} catch (Exception e) {
e.printStackTrace();
return "添加任务失败";
}
}
/**
* 暂停任务
*/
@PostMapping(path = "/pausejob")
@ResponseBody
public String pausejob(@RequestBody ScheduleJobEntity scheduleJobEntity) {
try {
quartzService.pausejob(scheduleJobEntity);
return "暂停任务成功";
} catch (Exception e) {
e.printStackTrace();
return "暂停任务失败";
}
}
/**
* 恢复任务
*/
@PostMapping(path = "/resumejob")
@ResponseBody
public String resumejob(@RequestBody ScheduleJobEntity scheduleJobEntity) {
try {
quartzService.resumejob(scheduleJobEntity);
return "恢复任务成功";
} catch (Exception e) {
e.printStackTrace();
return "恢复任务失败";
}
}
/**
* 重启任务
*/
@PostMapping(path = "/reschedulejob")
@ResponseBody
public String rescheduleJob(@RequestBody ScheduleJobEntity scheduleJobEntity) {
try {
quartzService.rescheduleJob(scheduleJobEntity);
return "重启任务成功";
} catch (Exception e) {
e.printStackTrace();
return "重启任务失败";
}
}
/**
* 删除任务
*
*/
@PostMapping(path = "/deletejob")
@ResponseBody
public String deletejob(@RequestBody ScheduleJobEntity scheduleJobEntity) {
try {
quartzService.deletejob(scheduleJobEntity);
return "删除任务成功";
} catch (Exception e) {
e.printStackTrace();
return "删除任务失败";
}
}
}
package com.schedule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component("scheduleTest")
@Slf4j
public class ScheduleTest {
public void test() {
log.info("test定时任务开始--");
}
}
package com.schedule;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContext implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param
* @return
*/
public <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param
* @return
*/
public <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}