关于springboot:Spring-Boot整合Quartz实现定时任务

42次阅读

共计 15212 个字符,预计需要花费 39 分钟才能阅读完成。

原文地址:Spring Boot 整合 Quartz 实现定时工作

源码已上传至 Github:spring-boot-quartz

增加依赖

编辑文件 pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

增加配置

编辑配置文件 application.yml,增加以下配置:

spring:
  quartz:
    # 采纳数据库存储形式
    job-store-type: jdbc
    jdbc:
      # 不从新创立数据表
      initialize-schema: never
    properties:
      org:
        quartz:
          scheduler:
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            # 数据表前缀
            tablePrefix: qrtz_
          # 连接池
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true

创立数据表供 Quartz 应用

sql 语句可在 jdbcjobstore 找到,Quartz 提供了 tables_mysql_innodb.sql

#
# 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(190) NOT NULL,
JOB_GROUP VARCHAR(190) 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(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
JOB_NAME VARCHAR(190) NOT NULL,
JOB_GROUP VARCHAR(190) 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(190) 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(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
    TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) 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(190) 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(190) 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(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
INSTANCE_NAME VARCHAR(190) 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(190) NULL,
JOB_GROUP VARCHAR(190) 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(190) 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);

执行完 sql 语句能够在数据库中看到以下 11 个表:

应用

创立 ScheduleJobDTO

新建类 ScheduleJobDTO,用于封装工作信息返回到前端。

@Data
@Accessors(chain = true)
public class ScheduleJobDTO {

    private String jobName;
    private String jobGroup;
    private String jobDescription;
    private Integer triggerStatus;
    private String triggerStatusName;

    /**
     * 额定的数据
     */
    private List<Map<String, Object>> jobMapData;
}

创立 ScheduleJobParam

新建类 ScheduleJobParam,封装申请参数,用于接管前端传递过去的参数。

@Getter
@Setter
@ToString
@Accessors(chain = true)
public class ScheduleJobParam {@NotBlank(message = "工作名称不能为空")
    private String jobName;

    @NotBlank(message = "工作分组不能为空")
    private String jobGroup;

    @NotBlank(message = "执行类名不能为空")
    private String jobClass;

    @NotBlank(message = "cron 表达式不能为空")
    private String cronExpression;

    private String jobDescription;

    /**
     * 额定的数据
     */
    private List<Map<String, Object>> jobMapData;
}

创立 IJobService

新建类 IJobService,用于定义工作的获取、新增、进行、复原、删除等接口。

IJobService.java 内容如下:

public interface IJobService {

    /**
     * 查问所有工作列表
     * @return
     */
    List<ScheduleJobDTO> listAllJob();

    /**
     * 获取正在运行的工作列表
     * @return
     */
    List<ScheduleJobDTO> listRunningJob();

    /**
     * 新增工作
     * @param job job
     * @return
     */
    boolean addJob(ScheduleJobParam job);

    /**
     * 执行 job
     * @param jobName jobName
     * @param jobGroupName jobGroupName
     * @return
     */
    boolean triggerJob(String jobName, String jobGroupName);

    /**
     * 启动所有定时工作
     * @return
     */
    boolean startJobs();

    /**
     * 删除工作
     * @param jobName jobName
     * @param jobGroupName jobGroupName
     * @return
     */
    boolean deleteJob(String jobName, String jobGroupName);

    /**
     * 暂停工作
     * @param jobName jobName
     * @param jobGroupName jobGroupName
     * @return
     */
    boolean pauseJob(String jobName, String jobGroupName);

    /**
     * 复原工作
     * @param jobName jobName
     * @param jobGroupName jobGroupName
     * @return
     */
    boolean resumeJob(String jobName, String jobGroupName);
}

创立 JobServiceImpl

新建类 JobServiceImpl,用于实现 IJobService 接口,实现工作的获取、新增、进行、复原、删除等性能。

JobServiceImpl.java 内容如下:

@Service("jobService")
@Slf4j
public class JobServiceImpl implements IJobService {

    public static final String TRIGGER_IDENTITY_PREFIX = "trigger_";

    /**
     * 调度器
     */
    private final Scheduler scheduler;

    @Autowired
    public JobServiceImpl(Scheduler scheduler) {this.scheduler = scheduler;}

    @Override
    public List<ScheduleJobDTO> listAllJob() {List<ScheduleJobDTO> result = new ArrayList<>();
        try {GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
            Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
            for (JobKey jobKey : jobKeys) {List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
                for (Trigger trigger : triggers) {ScheduleJobDTO scheduleJob = new ScheduleJobDTO();
                    scheduleJob.setJobName(jobKey.getName())
                            .setJobGroup(jobKey.getGroup())
                            .setJobDescription(trigger.getDescription())
                            .setTriggerStatus(scheduler.getTriggerState(trigger.getKey()).ordinal())
                            .setTriggerStatusName(scheduler.getTriggerState(trigger.getKey()).name());

                    result.add(scheduleJob);
                }
            }
        } catch (SchedulerException e) {log.error("获取所有工作列表失败,谬误:{}", e.getMessage());
        }
        return result;
    }

    @Override
    public List<ScheduleJobDTO> listRunningJob() {List<ScheduleJobDTO> result = new ArrayList<>();
        try {
            // 获取列表
            List<JobExecutionContext> executingJobs =  scheduler.getCurrentlyExecutingJobs();

            result = getJobListData(executingJobs);

        } catch (SchedulerException e) {log.error("获取运行工作列表失败,谬误:{}", e.getMessage());
        }
        return result;
    }

    @Override
    public boolean addJob(ScheduleJobParam jobParam) {
        try {
            // 加载执行类
            Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(jobParam.getJobClass());
            clazz.newInstance();

            // 1. 创立 job
            JobDetail job = JobBuilder.newJob(clazz)
                    .withIdentity(jobParam.getJobName(), jobParam.getJobGroup())
                    .withDescription(jobParam.getJobDescription())
                    .build();

            JobDataMap jobDataMap = job.getJobDataMap();

            List<Map<String, Object>> data = jobParam.getJobMapData();
            if (data != null && data.size() > 0) {
                data.forEach(jobDataItem -> {jobDataItem.keySet().forEach((key) -> {jobDataMap.put(key, jobDataItem.get(key));
                    });
                });
            }

            // 配置 cron 运行规定,即执行工夫
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(jobParam.getCronExpression());

            // 2. 创立 Trigger
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(TRIGGER_IDENTITY_PREFIX + jobParam.getJobName(), jobParam.getJobGroup())
                    .startNow()
                    .withSchedule(cronScheduleBuilder)
                    .build();

            // 3. 注册工作和定时器
            scheduler.scheduleJob(job, trigger);
            scheduler.start();} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SchedulerException e) {log.error("增加工作 {} 失败,谬误:{}", jobParam.getJobName(), e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean triggerJob(String jobName, String jobGroupName) {JobKey key = new JobKey(jobName, jobGroupName);
        try {scheduler.triggerJob(key);
        } catch (SchedulerException e) {log.error("工作 {} 触发失败", jobName);
            return false;
        }
        return true;
    }

    @Override
    public boolean startJobs() {
        try {scheduler.start();
        } catch (SchedulerException e) {log.error("启动所有工作失败:", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteJob(String jobName, String jobGroupName) {TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
        try {
            // 进行触发器
            scheduler.pauseTrigger(triggerKey);
            // 删除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除工作
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
        } catch (SchedulerException e) {log.error("删除工作 {} 失败,谬误:{}", jobName, e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean pauseJob(String jobName, String jobGroupName) {JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
        try {scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {log.error("进行工作 {} 失败,谬误:{}", jobName, e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean resumeJob(String jobName, String jobGroupName) {JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
        try {scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {log.error("复原工作 {} 失败,谬误:{}", jobName, e.getMessage());
            return false;
        }
        return true;
    }

    private List<ScheduleJobDTO> getJobListData(List<JobExecutionContext> executingJobs) {List<ScheduleJobDTO> result = new ArrayList<>();
        try {for (JobExecutionContext jobItem : executingJobs) {ScheduleJobDTO  scheduleJobItem = getJobDataByJobExecutionContext(jobItem);
                result.add(scheduleJobItem);
            }
        } catch (SchedulerException e) {log.error("获取工作状态失败,谬误:{}", e.getMessage());
        }
        return result;
    }

    /**
     * 从 JobExecutionContext 中解析获取数据
     * @param jobContext JobExecutionContext
     * @return
     * @throws SchedulerException
     */
    private ScheduleJobDTO getJobDataByJobExecutionContext(JobExecutionContext jobContext) throws SchedulerException {JobDetail jobDetail = jobContext.getJobDetail();
        JobKey jobKey = jobDetail.getKey();
        Trigger trigger = jobContext.getTrigger();
        // 封装 ScheduleJobDTO 返回,想要获取更多的数据(如 JobDataMap)可自行添加
        ScheduleJobDTO scheduleJob = new ScheduleJobDTO();
        scheduleJob.setJobName(jobKey.getName())
                .setJobGroup(jobKey.getGroup())
                .setJobDescription(trigger.getDescription())
                .setTriggerStatus(scheduler.getTriggerState(trigger.getKey()).ordinal())
                .setTriggerStatusName(scheduler.getTriggerState(trigger.getKey()).name());
        return scheduleJob;
    }
}

创立工作类

新建两个工作类(实现 org.quartz.Job 接口),用于定时工作执行,我这建了 EatJobDrinkJob

EatJob 内容如下:

public class EatJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        // 通过 jobExecutionContext 可获取到工作的相干信息
        // JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()) + "正在吃饭......");
    }
}

DrinkJob 内容如下:

public class DrinkJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()) + "正在喝货色......");
    }
}

创立 controller

新建 controller

@RestController
@RequestMapping("/jobs")
public class MainController {

    private final IJobService jobService;

    @Autowired
    public MainController(IJobService jobService) {this.jobService = jobService;}

    @GetMapping("/all")
    public JsonResult<List<ScheduleJobDTO>> queryAllJobs() {List<ScheduleJobDTO> list = jobService.listAllJob();
        return JsonResult.ok(list);
    }

    @GetMapping("/running")
    public JsonResult<List<ScheduleJobDTO>> queryRunningJobs() {List<ScheduleJobDTO> list = jobService.listRunningJob();
        return JsonResult.ok(list);
    }

    @PostMapping("")
    public JsonResult<String> addJob(@RequestBody @Valid ScheduleJobParam param) {boolean flag = jobService.addJob(param);
        return flag ? JsonResult.ok() : JsonResult.error("增加工作失败");
    }

    @PutMapping("/pause")
    public JsonResult<String> pauseJob(@RequestBody @Valid ScheduleJobParam param) {boolean flag = jobService.pauseJob(param.getJobName(), param.getJobGroup());
        return flag ? JsonResult.ok() : JsonResult.error("进行工作失败");
    }

    @PutMapping("/resume")
    public JsonResult<String> resumeJob(@RequestBody @Valid ScheduleJobParam param) {boolean flag = jobService.resumeJob(param.getJobName(), param.getJobGroup());
        return flag ? JsonResult.ok() : JsonResult.error("进行工作失败");
    }

    @DeleteMapping("")
    public JsonResult<String> deleteJob(@RequestBody @Valid ScheduleJobParam param) {boolean flag = jobService.deleteJob(param.getJobName(), param.getJobGroup());
        return flag ? JsonResult.ok() : JsonResult.error("删除工作失败");
    }
}

测试

启动我的项目,应用 Postman 发送申请测试:

  1. 获取所有工作列表,申请 http://127.0.0.1:8077/jobs/all,因为没有工作,所以返回为空;

  1. 新增工作,应用 POST 申请 http://127.0.0.1:8077/jobs,申请参数如下;其中 cron 表达式可应用 https://cron.qqe2.com/ 这个工具生成;

此时,再去申请获取工作列表,胜利返回列表:

  1. 进行工作,应用 PUT 申请 http://127.0.0.1:8077/jobs/pause,参数参考增加参数,发现控制台已不输入信息;
  2. 复原工作,应用 PUT 申请 http://127.0.0.1:8077/jobs/resume,参数参考增加参数,发现控制台又从新输入信息,阐明工作已胜利复原执行;
  3. 删除工作,应用 DELETE 申请 http://127.0.0.1:8077/jobs,参数参考增加参数,操作实现后控制台不再输入信息,再次申请获取工作列表,发现返回空列表了,阐明删除工作胜利。

至此,Spring Boot 整合 Quartz 实现。

Github:spring-boot-quartz

正文完
 0