SpringBoot实现定时任务,使用自带的定时任务以及调度框架quartz的配置使用
文章目录
- SpringBoot实现定时任务,使用自带的定时任务以及调度框架quartz的配置使用
- 一. 使用SpringBoot自带的定时任务(适用于小型应用)
- 二. 使用调度框架quartz(适用于中大型应用)
- 三、数据库模式quartz的使用
- 1. 创建数据库表
- 2. 制定一个定时任务
- 3. 写配置文件
- 4. 创建接收和相应实体类
- 5. 增删改查的controller层接口
- 6. 编写接口测试代码
一. 使用SpringBoot自带的定时任务(适用于小型应用)
创建SpringBoot
项目,创建SpringBootTestJob
类,作为定时任务类
@Component
@EnableScheduling
public class SpringBootTestJob {
@Scheduled(cron = "0/5 * * * * ?") // 每五秒执行一次
private void test() {
System.out.println("SpringBootTestJob TEST");
}
}
- 适用性: 适合单体应用,不适合集群,没法实时更改定时任务状态和策略
启动后,可以看到,每五秒打印内容:
二. 使用调度框架quartz(适用于中大型应用)
- 增加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
- 创建定时任务类
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author SaoE
* @date 2025/1/19 20:06
*/
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("TestJob TEST");
}
}
- 增加quartz配置类QuartzConfig
@Configuration
public class QuartzConfig {
/**
* 声明一个任务
* @return
*/
@Bean
public JobDetail jobDetail() {
return JobBuilder.newJob(TestJob.class)
.withIdentity("TestJob", "test")
.storeDurably()
.build();
}
/**
* 声明一个触发器,什么时候触发这个任务
* @return
*/
@Bean
public Trigger trigger() {
return TriggerBuilder.newTrigger()
.forJob(jobDetail())
.withIdentity("trigger", "trigger")
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?"))
.build();
}
}
- 启动主类
可以看到控制台每两秒打印信息:
- 禁止任务并发执行
任务类增加注解@DisallowConcurrentExecution
,修改如下:
@DisallowConcurrentExecution
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("TestJob TEST开始");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("TestJob TEST结束");
}
}
此时打印输出为上一个任务结束后,下一个任务立即开始。如果没有这个注解,则会并发执行。
三、数据库模式quartz的使用
1. 创建数据库表
-- #
-- # Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar
-- #
-- # PLEASE consider using mysql with innodb tables to avoid locking issues
-- #
-- # In your Quartz properties file, you'll need to set
-- # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
-- #
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 comment '定时任务名称',
JOB_NAME VARCHAR(200) NOT NULL comment 'job名称',
JOB_GROUP VARCHAR(200) NOT NULL comment 'job组',
DESCRIPTION VARCHAR(250) NULL comment '描述',
JOB_CLASS_NAME VARCHAR(250) NOT NULL comment 'job类名',
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 NULL comment 'job数据',
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
);
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名称',
JOB_GROUP VARCHAR(200) NOT NULL comment 'job组',
DESCRIPTION VARCHAR(250) NULL comment '描述',
NEXT_FIRE_TIME BIGINT(13) NULL comment '下一次触发时间',
PREV_FIRE_TIME BIGINT(13) NULL comment '前一次触发时间',
PRIORITY INTEGER 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) NULL comment '结束时间',
CALENDAR_NAME VARCHAR(200) NULL comment '日程名称',
MISFIRE_INSTR SMALLINT(2) NULL comment '未触发实例',
JOB_DATA BLOB NULL comment 'job数据',
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)
);
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),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
);
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(200) NOT NULL comment 'cron表达式',
TIME_ZONE_ID VARCHAR(80) comment '时区',
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)
);
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) NULL comment '开始配置1',
STR_PROP_2 VARCHAR(512) NULL comment '开始配置2',
STR_PROP_3 VARCHAR(512) NULL comment '开始配置3',
INT_PROP_1 INT NULL comment 'int配置1',
INT_PROP_2 INT NULL comment 'int配置2',
LONG_PROP_1 BIGINT NULL comment 'long配置1',
LONG_PROP_2 BIGINT NULL comment 'long配置2',
DEC_PROP_1 NUMERIC(13,4) NULL comment '配置描述1',
DEC_PROP_2 NUMERIC(13,4) NULL comment '配置描述2',
BOOL_PROP_1 VARCHAR(1) NULL comment 'bool配置1',
BOOL_PROP_2 VARCHAR(1) NULL comment 'bool配置2',
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)
);
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 NULL comment '数据',
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)
);
CREATE TABLE QRTZ_CALENDARS
(
SCHED_NAME VARCHAR(120) NOT NULL comment '定时任务名称',
CALENDAR_NAME VARCHAR(200) NOT NULL comment '日程名称',
CALENDAR BLOB NOT NULL comment '日程数据',
PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)
);
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)
);
CREATE TABLE QRTZ_FIRED_TRIGGERS
(
SCHED_NAME VARCHAR(120) NOT NULL comment '定时任务名称',
ENTRY_ID VARCHAR(95) NOT NULL comment 'entryId',
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 INTEGER NOT NULL comment '等级',
STATE VARCHAR(16) NOT NULL comment '状态',
JOB_NAME VARCHAR(200) NULL comment 'job名称',
JOB_GROUP VARCHAR(200) NULL comment 'job组',
IS_NONCONCURRENT VARCHAR(1) NULL comment '是否异步',
REQUESTS_RECOVERY VARCHAR(1) NULL comment '是否请求覆盖',
PRIMARY KEY (SCHED_NAME,ENTRY_ID)
);
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)
);
CREATE TABLE QRTZ_LOCKS
(
SCHED_NAME VARCHAR(120) NOT NULL comment '定时任务名称',
LOCK_NAME VARCHAR(40) NOT NULL comment 'lock名称',
PRIMARY KEY (SCHED_NAME,LOCK_NAME)
);
2. 制定一个定时任务
和上面一样的TestJob
@DisallowConcurrentExecution
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("TestJob TEST开始");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("TestJob TEST结束");
}
}
3. 写配置文件
MyJobFactory.java:
@Component
public class MyJobFactory extends SpringBeanJobFactory {
@Resource
private AutowireCapableBeanFactory beanFactory;
/**
* 这里覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
beanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
SchedulerConfig.java:
@Configuration
public class SchedulerConfig {
@Resource
private MyJobFactory myJobFactory;
@Bean
public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setDataSource(dataSource);
factory.setJobFactory(myJobFactory);
factory.setStartupDelay(2);
return factory;
}
}
4. 创建接收和相应实体类
接收实体类CronJobReq.java:
public class CronJobReq {
private String group;
private String name;
private String description;
private String cronExpression;
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("CronJobDto{");
sb.append("cronExpression='").append(cronExpression).append('\'');
sb.append(", group='").append(group).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", description='").append(description).append('\'');
sb.append('}');
return sb.toString();
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
响应实体类CronJobResp.java:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class CronJobResp {
private String group;
private String name;
private String description;
private String state;
private String cronExpression;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date nextFireTime;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date preFireTime;
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("CronJobDto{");
sb.append("cronExpression='").append(cronExpression).append('\'');
sb.append(", group='").append(group).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", description='").append(description).append('\'');
sb.append(", state='").append(state).append('\'');
sb.append(", nextFireTime=").append(nextFireTime);
sb.append(", preFireTime=").append(preFireTime);
sb.append('}');
return sb.toString();
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getNextFireTime() {
return nextFireTime;
}
public void setNextFireTime(Date nextFireTime) {
this.nextFireTime = nextFireTime;
}
public Date getPreFireTime() {
return preFireTime;
}
public void setPreFireTime(Date preFireTime) {
this.preFireTime = preFireTime;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
5. 增删改查的controller层接口
任务和quartz
通过接口来关联。CronJobReq
类的name
属性会告诉quartz
操作哪个定时任务。
JobController.java:
@RestController
@RequestMapping(value = "/admin/job")
public class JobController {
private static Logger LOG = LoggerFactory.getLogger(JobController.class);
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@RequestMapping(value = "/run")
public CommonResp<Object> run(@RequestBody CronJobReq cronJobReq) throws SchedulerException {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
LOG.info("手动执行任务开始:{}, {}", jobClassName, jobGroupName);
schedulerFactoryBean.getScheduler().triggerJob(JobKey.jobKey(jobClassName, jobGroupName));
return new CommonResp<>();
}
@RequestMapping(value = "/add")
public CommonResp add(@RequestBody CronJobReq cronJobReq) {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
String cronExpression = cronJobReq.getCronExpression();
String description = cronJobReq.getDescription();
LOG.info("创建定时任务开始:{},{},{},{}", jobClassName, jobGroupName, cronExpression, description);
CommonResp commonResp = new CommonResp();
try {
// 通过SchedulerFactory获取一个调度器实例
Scheduler sched = schedulerFactoryBean.getScheduler();
// 启动调度器
sched.start();
//构建job信息
JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(jobClassName)).withIdentity(jobClassName, jobGroupName).build();
//表达式调度构建器(即任务执行的时间)
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName).withDescription(description).withSchedule(scheduleBuilder).build();
sched.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
LOG.error("创建定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("创建定时任务失败:调度异常");
} catch (ClassNotFoundException e) {
LOG.error("创建定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("创建定时任务失败:任务类不存在");
}
LOG.info("创建定时任务结束:{}", commonResp);
return commonResp;
}
@RequestMapping(value = "/pause")
public CommonResp pause(@RequestBody CronJobReq cronJobReq) {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
LOG.info("暂停定时任务开始:{},{}", jobClassName, jobGroupName);
CommonResp commonResp = new CommonResp();
try {
Scheduler sched = schedulerFactoryBean.getScheduler();
sched.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
} catch (SchedulerException e) {
LOG.error("暂停定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("暂停定时任务失败:调度异常");
}
LOG.info("暂停定时任务结束:{}", commonResp);
return commonResp;
}
@RequestMapping(value = "/resume")
public CommonResp resume(@RequestBody CronJobReq cronJobReq) {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
LOG.info("重启定时任务开始:{},{}", jobClassName, jobGroupName);
CommonResp commonResp = new CommonResp();
try {
Scheduler sched = schedulerFactoryBean.getScheduler();
sched.resumeJob(JobKey.jobKey(jobClassName, jobGroupName));
} catch (SchedulerException e) {
LOG.error("重启定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("重启定时任务失败:调度异常");
}
LOG.info("重启定时任务结束:{}", commonResp);
return commonResp;
}
@RequestMapping(value = "/reschedule")
public CommonResp reschedule(@RequestBody CronJobReq cronJobReq) {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
String cronExpression = cronJobReq.getCronExpression();
String description = cronJobReq.getDescription();
LOG.info("更新定时任务开始:{},{},{},{}", jobClassName, jobGroupName, cronExpression, description);
CommonResp commonResp = new CommonResp();
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
CronTriggerImpl trigger1 = (CronTriggerImpl) scheduler.getTrigger(triggerKey);
trigger1.setStartTime(new Date()); // 重新设置开始时间
CronTrigger trigger = trigger1;
// 按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withDescription(description).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
} catch (Exception e) {
LOG.error("更新定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("更新定时任务失败:调度异常");
}
LOG.info("更新定时任务结束:{}", commonResp);
return commonResp;
}
@RequestMapping(value = "/delete")
public CommonResp delete(@RequestBody CronJobReq cronJobReq) {
String jobClassName = cronJobReq.getName();
String jobGroupName = cronJobReq.getGroup();
LOG.info("删除定时任务开始:{},{}", jobClassName, jobGroupName);
CommonResp commonResp = new CommonResp();
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
} catch (SchedulerException e) {
LOG.error("删除定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("删除定时任务失败:调度异常");
}
LOG.info("删除定时任务结束:{}", commonResp);
return commonResp;
}
@RequestMapping(value="/query")
public CommonResp query() {
LOG.info("查看所有定时任务开始");
CommonResp commonResp = new CommonResp();
List<CronJobResp> cronJobDtoList = new ArrayList();
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
for (String groupName : scheduler.getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
CronJobResp cronJobResp = new CronJobResp();
cronJobResp.setName(jobKey.getName());
cronJobResp.setGroup(jobKey.getGroup());
//get job's trigger
List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
CronTrigger cronTrigger = (CronTrigger) triggers.get(0);
cronJobResp.setNextFireTime(cronTrigger.getNextFireTime());
cronJobResp.setPreFireTime(cronTrigger.getPreviousFireTime());
cronJobResp.setCronExpression(cronTrigger.getCronExpression());
cronJobResp.setDescription(cronTrigger.getDescription());
Trigger.TriggerState triggerState = scheduler.getTriggerState(cronTrigger.getKey());
cronJobResp.setState(triggerState.name());
cronJobDtoList.add(cronJobResp);
}
}
} catch (SchedulerException e) {
LOG.error("查看定时任务失败:" + e);
commonResp.setSuccess(false);
commonResp.setMessage("查看定时任务失败:调度异常");
}
commonResp.setContent(cronJobDtoList);
LOG.info("查看定时任务结束:{}", commonResp);
return commonResp;
}
}
6. 编写接口测试代码
test.http:
POST http://localhost:8000/batch/admin/job/add
Content-Type: application/json
{
"name": "com.mystudy.train.batch.job.TestJob",
"jobGroupName": "default",
"cronExpression": "*/2 * * * * ?",
"desc": "test job"
}
###
GET http://localhost:8000/batch/admin/job/query
###
POST http://localhost:8000/batch/admin/job/pause
Content-Type: application/json
{
"name": "com.mystudy.train.batch.job.TestJob",
"jobGroupName": "default"
}
###
POST http://localhost:8000/batch/admin/job/resume
Content-Type: application/json
{
"name": "com.mystudy.train.batch.job.TestJob",
"jobGroupName": "default"
}
###
POST http://localhost:8000/batch/admin/job/reschedule
Content-Type: application/json
{
"name": "com.mystudy.train.batch.job.TestJob",
"jobGroupName": "default",
"cronExpression": "*/5 * * * * ?",
"desc": "test job"
}
###
POST http://localhost:8000/batch/admin/job/delete
Content-Type: application/json
{
"name": "com.mystudy.train.batch.job.TestJob",
"jobGroupName": "default"
}
###