正好最近做了这个功能,在网上看了好多人写的博客,很多都是重复的,也有一些内容太冗余,所以自己整理了一下,更简便易懂的,适用于初学者,当然我也是!
不多说 上代码!
1.首先创建数据库表, 这里只说mysql, 其他数据库可以去网上搜搜,
#
# In your Quartz properties file, you’ll need to set
# org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#
#
# By: Ron Cordell – roncordell
# I didn’t see this anywhere, so I thought I’d post it here. This is the script from Quartz to create the tables in a MySQL database, modified to use INNODB instead of MYISAM.
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;
将这段sql粘贴到mysql 命令界面运行就好 其他的就不用管了
1.1 springboot项目pom.xml 引入quartz依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
1.2 applicaton.yml配置
#quartz 任务调度框架
quartz:
job-store-type: jdbc #数据库方式
jdbc:
initialize-schema: never #不初始化表结构
properties:
org:
quartz:
scheduler:
instanceId: AUTO #默认主机名和时间戳生成实例ID,可以是任何字符串,但对于所有调度程序来说,必须是唯一的 对应qrtz_scheduler_state INSTANCE_NAME字段
instanceName: clusteredScheduler #quartzScheduler
jobStore:
acquireTriggersWithinLock: true #在锁定状态下获取触发器
class: org.quartz.impl.jdbcjobstore.JobStoreTX #持久化配置
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate #我们仅为数据库制作了特定于数据库的代理
useProperties: true #以指示JDBCJobStore将JobDataMaps中的所有值都作为字符串,因此可以作为名称 - 值对存储而不是在BLOB列中以其序列化形式存储更多复杂的对象。从长远来看,这是更安全的,因为您避免了将非String类序列化为BLOB的类版本问题。
tablePrefix: QRTZ_ #数据库表前缀
misfireThreshold: 6000 #在被认为“失火”之前,调度程序将“容忍”一个Triggers将其下一个启动时间通过的毫秒数。默认值(如果您在配置中未输入此属性)为60000(60秒)。
clusterCheckinInterval: 5000 #设置此实例“检入”*与群集的其他实例的频率(以毫秒为单位)。影响检测失败实例的速度。
isClustered: false #打开群集功能,集群模式需要在多台服务器上做时间同步或者使用zookeeper去解决
threadPool: #连接池
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10 #最大线程个数
threadPriority: 5 #线程优先级
threadsInheritContextClassLoaderOfInitializingThread: true
startup-delay: 30
overwrite-existing-jobs: true
2.java代码部分 定义job任务
这里呢 先写我们要定时运行的业务,
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Annotion: 执行job 示例 只要让类集成 QuartzJobBean 实现里边的一个抽象方法就ok了
*/
@Slf4j
public class MyJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
log.debug(“JOB运行了一次 :”+sdf.format(new Date()));
}
}
3. 对job进行操作
3.1 先定义个实体类, 用来放job一些设置信息
package com.troila.unicorn.task.quartz;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @Annotion: quartz Job实体类
* @ClassName: ScheduleJob
*/
@Component
@Data
@ConfigurationProperties
public class ScheduleJob {
/**
* 正在运行
*/
public static final String STATUS_RUNNING = “1”;
/**
*不在运行
*/
public static final String STATUS_NOT_RUNNING = “0”;
/** 序列化id*/
private static final long serialVersionUID = -7100490638092503733L;
/**
* 任务名称
*/
private String jobName;
/**
* 任务分组
*/
private String jobGroup;
/**
* 任务状态 是否启动任务
*/
private String jobStatus;
/**
* cron表达式
*/
private String cronExpression;
/**
* spring bean 任务所在 类名
*/
private String jobClass;
/**
* 任务调用的方法传入的参数,统一使用String,
*/
private Map<String,Object> parameters;
/**
* 任务是否有状态 是否支持并行
*/
private boolean isConcurrent;
/**
* 任务描述
*/
private String description;
/**
* 是否更新
*/
private boolean isUpdateData;
/**
* 是否要求唤醒
*/
private boolean requestsRecovery;
/**
* 是否持久化
*/
private boolean isDruable;
}
3.2 对job进行增删改查的一些接口
package com.troila.unicorn.task.quartz;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
/**
* @Annotion: 编写quartz添加job调度类
* @ClassName: ScheduleJobFactory
*/
@Component
@Slf4j
public class ScheduleJobFactory {
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
/**
* 初始化任务
*
* @param job
*/
public void initJob(ScheduleJob job) throws Exception {
if (job == null) {
return;
}
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
if (!ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) {
this.deleteJob(scheduler, triggerKey);
log.info(“删除任务{}”, job.getJobName());
} else {
addJob(scheduler, triggerKey, job);
log.info(scheduler + “添加任务:{}”, JSONObject.toJSONString(job));
}
}
/**
* 增加定时任务job
*
* @param scheduler 调度器
* @param triggerKey 触发器key
* @param job 任务
* @throws Exception
*/
private void addJob(Scheduler scheduler, TriggerKey triggerKey, ScheduleJob job) throws Exception {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (null == trigger) {
/*不存在,创建一个*/
setJobExistTrigger(scheduler, trigger, triggerKey, job);
} else {
/*存在,直接用*/
setJobNoExistTrigger(scheduler, trigger, triggerKey, job);
}
}
/**
* 如果触发器key不存在,设置job
*
* @param scheduler 调度器
* @param trigger 触发器
* @param triggerKey 触发器key
* @param job 任务
* @throws Exception
*/
private void setJobNoExistTrigger(Scheduler scheduler, CronTrigger trigger, TriggerKey triggerKey, ScheduleJob job)
throws Exception {
/*Trigger已存在,那么更新相应的定时设置*/
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
/* 按新的cronExpression表达式重新构建trigger*/
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
/*按新的trigger重新设置job执行*/
scheduler.rescheduleJob(triggerKey, trigger);
}
/**
* 如果触发器key存在,设置job
*
* @param scheduler 调度器
* @param trigger 触发器
* @param triggerKey 触发器key
* @param job 任务
* @throws Exception
*/
public void setJobExistTrigger(Scheduler scheduler, CronTrigger trigger,
TriggerKey triggerKey, ScheduleJob job) throws Exception {
Class clazz = Class.forName(job.getJobClass());
/*设定job详情和属性*/
JobDetailImpl tail = new JobDetailImpl();
tail.setDescription(job.getDescription());
tail.setGroup(job.getJobGroup());
tail.setJobClass(clazz);
tail.setJobDataMap(new JobDataMap());
tail.setKey(JobKey.jobKey(job.getJobGroup(),job.getJobName()));
tail.setRequestsRecovery(job.isRequestsRecovery());
/*设置持久*/
tail.setDurability(job.isDruable());
JobDetail jobDetail = tail;
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
.cronSchedule(job.getCronExpression());
trigger = TriggerBuilder.newTrigger()
.withIdentity(job.getJobName(), job.getJobGroup())
.withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
}
/**
* 删除job
*
* @param scheduler
* @param triggerKey
* @throws Exception
*/
private void deleteJob(Scheduler scheduler, TriggerKey triggerKey) {
if (null != triggerKey) {
try {
scheduler.unscheduleJob(triggerKey);
} catch (SchedulerException e) {
log.error(“删除job异常”);
e.printStackTrace();
}
} else {
log.warn(“删除job无效,没有指定对应的Trigger”);
}
}
/**
* 停止job
*
* @param name 名称
* @param group 分组
*/
public void stopJob(String name, String group) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(name, group);
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
log.error(“停止job异常”);
e.printStackTrace();
}
}
/**
* 添加job
*
* @param job
*/
public void addJob(ScheduleJob job) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
Class clazz = null;
try {
clazz = Class.forName(job.getJobClass());
JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build();
try {
scheduler.addJob(jobDetail, true);
} catch (SchedulerException e) {
log.error(“添加job出现异常”);
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 停止所有的job
*/
public void pauseAll() {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
scheduler.pauseAll();
} catch (SchedulerException e) {
log.error(“暂停所有job出现异常”);
e.printStackTrace();
}
}
/**
* 关闭所有job
*/
public void shutdown(){
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
scheduler.shutdown();
} catch (SchedulerException e) {
log.error(“关闭所有Job出现异常!”);
e.printStackTrace();
}
}
/**
* 恢复指定任务
*
* @param name
* @param group
*/
public void restartJob(String name, String group) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(name, group);
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
log.error(“恢复任务异常”);
e.printStackTrace();
}
}
/**
* 恢复所有job
*/
public void restartAll() {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
scheduler.resumeAll();
} catch (SchedulerException e) {
log.error(“恢复所有job异常”);
e.printStackTrace();
}
}
/**
* 删除job
* @param jobName job名称
* @param jobGroup job 分组
*/
public void deleteJob(String jobName, String jobGroup) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
try {
/*scheduler.deleteJob(JobKey.jobKey(jobName,jobGroup));*/
scheduler.unscheduleJob(triggerKey);
} catch (SchedulerException e) {
log.error(“删除job”+jobGroup+”.”+jobName+”出现异常”);
e.printStackTrace();
}
}
}
4. 控制层
通过请求可以对job进行一些操作,
package com.troila.unicorn.task.quartz.controller;
import com.troila.unicorn.task.quartz.ScheduleJob;
import com.troila.unicorn.task.quartz.ScheduleJobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Annotion: 对job进行动态增删改查接口示例
* @ClassName: QuartzController
*/
@Controller
@RequestMapping(“/quartz”)
public class QuartzController {
@Autowired
private ScheduleJobFactory scheduleJobFactory;
@RequestMapping(“/addjob”)
public void addJob(){
ScheduleJob j = new ScheduleJob();
j.setConcurrent(false);
j.setCronExpression(“0/4 * * * * ?”);
j.setJobName(“MyJob”);
j.setJobGroup(“MyJob_Group”);
j.setJobStatus(“1”);
j.setParameters(null);
j.setJobClass(“com.troila.unicorn.task.quartz.MyJob”);
j.setRequestsRecovery(true);
j.setDruable(true);
j.setUpdateData(true);
j.setDescription(“54645654646464.”);
try {
scheduleJobFactory.initJob(j);
System.out.println(“添加job成功!”);
} catch (Exception e) {
System.out.println(“添加job异常!”);
e.printStackTrace();
}
}
@RequestMapping(“/stopjobbyname”)
public void stopJobByName(){
scheduleJobFactory.stopJob(“testJob”, “DEFAULT”);
System.out.println(“成功停止job”);
return;
}
@RequestMapping(“/restartjob”)
public void restartJob(){
scheduleJobFactory.restartJob(“testJob”, “DEFAULT”);
System.out.println(“恢复job”);
return;
}
@RequestMapping(“/deletejob”)
public void deleteJob(){
scheduleJobFactory.deleteJob(“MyJob”,”MyJob_Group”);
}
/**
* 暂停所有job
*/
@RequestMapping(“/pauseall”)
public void stopAll(){
scheduleJobFactory.pauseAll();
System.out.println(“暂停所有job,重启项目不启动”);
return;
}
@RequestMapping(“/shutdownall”)
public void shutdownAll(){
scheduleJobFactory.shutdown();
System.out.println(“已经关闭所有job,重启项目会启动”);
return;
}
/**
* 恢复所有job
*/
@RequestMapping(“/stratall”)
public void restartAll(){
scheduleJobFactory.restartAll();
System.out.println(“恢复所有job”);
return;
}
}
还有啥问题 直接评论区留言,我会尽快回复 ,
————————————————
版权声明:本文为CSDN博主「Beautiful菜园子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lfy609225258/article/details/96105538