51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

SpringBoot整合Quartz实现动态配置

本文转载自:Spring Boot整合Quartz实现动态配置

概述 {#概述}

本文介绍如何把Quartz定时任务做成接口,实现以下功能的动态配置:

  • 添加任务
  • 修改任务
  • 暂停任务
  • 恢复任务
  • 删除任务
  • 任务列表
  • 任务详情

注:添加任务接口仍然需要开发者提前准备好任务类,接口的目的是实现定时任务的动态调整,按需进行开关和修改,请注意这点。

Spring Boot整合Quartz {#spring-boot整合quartz}

简单说下Quartz的整合,做一下准备工作。

导入Quartz依赖 {#导入quartz依赖}

<!-- Quartz定时任务 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

配置文件中增加Quartz的支持 {#配置文件中增加quartz的支持}

spring:
 datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: xxx
    username: xxx
    password: xxx
  quartz:
    job-store-type: jdbc # 定时任务的数据保存到jdbc即数据库中
    jdbc:
      # embedded:默认
      # always:启动的时候初始化表,我们只在第一次启动的时候用它来自动创建表,然后改回embedded即可,不然数据每次都会被清空
      # never:启动的时候不初始化表,也不知道和embedded有什么不同
      initialize-schema: embedded

第一次启动的时候请把上面的initialize-schema设置为always,这会在数据库里面自动建表,然后第二次启动时改回embedded即可。

如果不需要定时任务的持久化就可以不管。

写一个测试用的任务类 {#写一个测试用的任务类}

import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component`
`public` `class` `QuartzTestJob` `extends` `QuartzJobBean` {
`@Override`
`protected` `void` `executeInternal(org.quartz.JobExecutionContext jobExecutionContext)` `throws` JobExecutionException {
System.out.println(`"Quartz Test Job"`);
}
}
`

为这个任务类写一个配置类 {#为这个任务类写一个配置类}


import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzTestJobConfig {
@Bean
public JobDetail quartzTestJobDetail() {
return JobBuilder.newJob(QuartzTestJob.class)
.withIdentity(QuartzTestJob.class.getSimpleName())
.storeDurably()
.usingJobData("data", "test")
.build();
}


    <span class="hljs-meta">@Bean</span>
    <span class="hljs-keyword">public</span> Trigger <span class="hljs-title function_">quartzTestJobTrigger</span><span class="hljs-params">()</span> {
        <span class="hljs-comment">// 0/1 * * * * ?</span>
        <span class="hljs-keyword">return</span> TriggerBuilder.newTrigger()
                .forJob(QuartzTestJob.class.getSimpleName())
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(<span class="hljs-number">1</span>).repeatForever())
                .build();
    }



`}
`

结论 {#结论}

以上是使用Quartz写一个定时任务的步骤,很简单,问题是配置写死了,没有办法动态调整,所以我们开始写接口,把上面这个任务配置类去掉。

定时任务动态配置实现 {#定时任务动态配置实现}

我们还是用上面的任务类QuartzTestJob 做测试,这里再说明一次,我们需要有一个任务类作为基础,本文的目的只是去掉上面的QuartzTestJobConfig

剩下的内容没有什么需要多说明的,我直接贴代码了。

业务层 {#业务层}

public interface QuartzService {
    /**
     * 添加定时任务
     */
    void addJob(QuartzCreateParam param) throws SchedulerException;

    <span class="hljs-comment">/**
     * 修改定时任务
     */</span>
    <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateJob</span><span class="hljs-params">(QuartzUpdateParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException;

    <span class="hljs-comment">/**
     * 暂停定时任务
     */</span>
    <span class="hljs-keyword">void</span> <span class="hljs-title function_">pauseJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException;

    <span class="hljs-comment">/**
     * 恢复定时任务
     */</span>
    <span class="hljs-keyword">void</span> <span class="hljs-title function_">resumeJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException;

    <span class="hljs-comment">/**
     * 删除定时任务
     */</span>
    <span class="hljs-keyword">void</span> <span class="hljs-title function_">deleteJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException;

    <span class="hljs-comment">/**
     * 定时任务列表
     * <span class="hljs-doctag">@return</span>
     */</span>
    List&lt;QuartzJobDetailDto&gt; <span class="hljs-title function_">jobList</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> SchedulerException;

    <span class="hljs-comment">/**
     * 定时任务详情
     */</span>
    QuartzJobDetailDto <span class="hljs-title function_">jobDetail</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException;



`}
`

业务层实现 {#业务层实现}

@Service
public class QuartzServiceImpl implements QuartzService {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addJob</span><span class="hljs-params">(QuartzCreateParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">clazzName</span> <span class="hljs-operator">=</span> param.getJobClazz();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();
        <span class="hljs-type">String</span> <span class="hljs-variable">cron</span> <span class="hljs-operator">=</span> param.getCron();
        <span class="hljs-type">String</span> <span class="hljs-variable">description</span> <span class="hljs-operator">=</span> param.getDescription();

        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        checkJobExist(jobKey);

        Class&lt;? <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Job</span>&gt; jobClass = <span class="hljs-literal">null</span>;
        <span class="hljs-keyword">try</span> {
            jobClass = (Class&lt;? <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Job</span>&gt;) Class.forName(clazzName);
        } <span class="hljs-keyword">catch</span> (ClassNotFoundException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BaseException</span>(<span class="hljs-string">"找不到任务类:"</span> + clazzName);
        }
        <span class="hljs-type">JobDataMap</span> <span class="hljs-variable">jobDataMap</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JobDataMap</span>();
        <span class="hljs-keyword">if</span> (param.getJobDataMap() != <span class="hljs-literal">null</span>) {
            param.getJobDataMap().forEach(jobDataMap::put);
        }

        <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> schedulerFactoryBean.getScheduler();
        <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(jobClass)
                .withIdentity(jobName, jobGroup)
                .usingJobData(jobDataMap)
                .build();

        <span class="hljs-type">CronScheduleBuilder</span> <span class="hljs-variable">scheduleBuilder</span> <span class="hljs-operator">=</span> CronScheduleBuilder.cronSchedule(cron);
        <span class="hljs-type">String</span> <span class="hljs-variable">triggerId</span> <span class="hljs-operator">=</span> jobKey.getGroup() + jobKey.getName();
        <span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()
                .withSchedule(scheduleBuilder)
                .withIdentity(triggerId)
                .withDescription(description)
                .build();
        scheduler.scheduleJob(jobDetail, trigger);

        <span class="hljs-keyword">if</span> (!scheduler.isShutdown()) {
            scheduler.start();
        }
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateJob</span><span class="hljs-params">(QuartzUpdateParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();
        <span class="hljs-type">String</span> <span class="hljs-variable">cron</span> <span class="hljs-operator">=</span> param.getCron();

        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        <span class="hljs-type">String</span> <span class="hljs-variable">triggerId</span> <span class="hljs-operator">=</span> jobKey.getGroup() + jobKey.getName();

        checkJobExist(jobKey);

        <span class="hljs-type">TriggerKey</span> <span class="hljs-variable">triggerKey</span> <span class="hljs-operator">=</span> TriggerKey.triggerKey(triggerId);
        <span class="hljs-type">CronScheduleBuilder</span> <span class="hljs-variable">scheduleBuilder</span> <span class="hljs-operator">=</span> CronScheduleBuilder.cronSchedule(cron);
        TriggerBuilder&lt;?&gt; triggerBuilder = TriggerBuilder.newTrigger()
                .withSchedule(scheduleBuilder)
                .withIdentity(triggerId);

        <span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> triggerBuilder.build();
        scheduler.rescheduleJob(triggerKey, trigger);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">pauseJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();

        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        checkJobExist(jobKey);

        scheduler.pauseJob(jobKey);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">resumeJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();

        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        checkJobExist(jobKey);

        scheduler.resumeJob(jobKey);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">deleteJob</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();

        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        checkJobExist(jobKey);
        
        <span class="hljs-comment">// 先暂停再删除</span>
        scheduler.pauseJob(jobKey);
        scheduler.deleteJob(jobKey);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> List&lt;QuartzJobDetailDto&gt; <span class="hljs-title function_">jobList</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> SchedulerException {
        GroupMatcher&lt;JobKey&gt; matcher = GroupMatcher.anyJobGroup();
        List&lt;QuartzJobDetailDto&gt; jobDtoList = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();
        <span class="hljs-keyword">for</span> (JobKey jobKey : scheduler.getJobKeys(matcher)) {
            <span class="hljs-type">QuartzJobDetailDto</span> <span class="hljs-variable">jobDto</span> <span class="hljs-operator">=</span> getJobDtoByJobKey(jobKey);
            jobDtoList.add(jobDto);
        }
        <span class="hljs-keyword">return</span> jobDtoList;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> QuartzJobDetailDto <span class="hljs-title function_">jobDetail</span><span class="hljs-params">(QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> param.getJobName();
        <span class="hljs-type">String</span> <span class="hljs-variable">jobGroup</span> <span class="hljs-operator">=</span> param.getJobGroup();
        <span class="hljs-type">JobKey</span> <span class="hljs-variable">jobKey</span> <span class="hljs-operator">=</span> JobKey.jobKey(jobName, jobGroup);
        <span class="hljs-keyword">return</span> getJobDtoByJobKey(jobKey);
    }

    <span class="hljs-comment">/*************** 私有方法 ***************/</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">checkJobExist</span><span class="hljs-params">(JobKey jobKey)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-keyword">if</span> (!scheduler.checkExists(jobKey)) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BaseException</span>(<span class="hljs-string">"该定时任务不存在:"</span> + jobKey.getName());
        }
    }

    <span class="hljs-keyword">public</span> QuartzJobDetailDto <span class="hljs-title function_">getJobDtoByJobKey</span><span class="hljs-params">(JobKey jobKey)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> scheduler.getJobDetail(jobKey);
        List&lt;Trigger&gt; triggerList = (List&lt;Trigger&gt;) scheduler.getTriggersOfJob(jobKey);

        <span class="hljs-type">QuartzJobDetailDto</span> <span class="hljs-variable">jobDto</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">QuartzJobDetailDto</span>();
        jobDto.setJobClazz(jobDetail.getJobClass().toString());
        jobDto.setJobName(jobKey.getName());
        jobDto.setJobGroup(jobKey.getGroup());
        jobDto.setJobDataMap(jobDetail.getJobDataMap());

        List&lt;QuartzTriggerDetailDto&gt; triggerDtoList = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();
        <span class="hljs-keyword">for</span> (Trigger trigger : triggerList) {
            <span class="hljs-type">QuartzTriggerDetailDto</span> <span class="hljs-variable">triggerDto</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">QuartzTriggerDetailDto</span>();
            triggerDto.setTriggerName(trigger.getKey().getName());
            triggerDto.setTriggerGroup(trigger.getKey().getGroup());
            triggerDto.setDescription(trigger.getDescription());

            <span class="hljs-keyword">if</span> (trigger <span class="hljs-keyword">instanceof</span> CronTriggerImpl) {
                <span class="hljs-type">CronTriggerImpl</span> <span class="hljs-variable">cronTriggerImpl</span> <span class="hljs-operator">=</span> (CronTriggerImpl) trigger;
                <span class="hljs-type">String</span> <span class="hljs-variable">cronExpression</span> <span class="hljs-operator">=</span> cronTriggerImpl.getCronExpression();
                triggerDto.setCron(cronExpression);

                <span class="hljs-comment">// 最近10次的触发时间</span>
                List&lt;Date&gt; dates = TriggerUtils.computeFireTimes(cronTriggerImpl, <span class="hljs-literal">null</span>, <span class="hljs-number">10</span>);
                triggerDto.setRecentFireTimeList(dates);
            }

            Trigger.<span class="hljs-type">TriggerState</span> <span class="hljs-variable">triggerState</span> <span class="hljs-operator">=</span> scheduler.getTriggerState(trigger.getKey());
            triggerDto.setTriggerState(triggerState.toString());

            triggerDtoList.add(triggerDto);
        }
        jobDto.setTriggerDetailDtoList(triggerDtoList);
        <span class="hljs-keyword">return</span> jobDto;
    }



`}
`

接口层 {#接口层}

@RestController
public class QuartzController {
    @Autowired
    private QuartzServiceImpl quartzService;

    <span class="hljs-meta">@PostMapping("/quartz/addJob")</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addJob</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzCreateParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        quartzService.addJob(param);
    }

    <span class="hljs-meta">@PostMapping("/quartz/updateJob")</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateJob</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzUpdateParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        quartzService.updateJob(param);
    }

    <span class="hljs-meta">@PostMapping("/quartz/pauseJob")</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">pauseJob</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        quartzService.pauseJob(param);
    }

    <span class="hljs-meta">@PostMapping("/quartz/resumeJob")</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">resumeJob</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        quartzService.resumeJob(param);
    }

    <span class="hljs-meta">@PostMapping("/quartz/deleteJob")</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">deleteJob</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        quartzService.deleteJob(param);
    }

    <span class="hljs-meta">@PostMapping("/quartz/jobList")</span>
    <span class="hljs-keyword">public</span> List&lt;QuartzJobDetailDto&gt; <span class="hljs-title function_">jobList</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-keyword">return</span> quartzService.jobList();
    }

    <span class="hljs-meta">@PostMapping("/quartz/jobDetail")</span>
    <span class="hljs-keyword">public</span> QuartzJobDetailDto <span class="hljs-title function_">jobDetail</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> QuartzDetailParam param)</span> <span class="hljs-keyword">throws</span> SchedulerException {
        <span class="hljs-keyword">return</span> quartzService.jobDetail(param);
    }



`}
`

接口请求参数 {#接口请求参数}

@ApiModel(value = "Quartz任务添加请求参数")
public class QuartzCreateParam extends BaseParam {
    @NotBlank(message = "任务类不能为空")
    @ApiModelProperty(value = "任务类路径", required = true)
    private String jobClazz;

    <span class="hljs-meta">@NotBlank(message = "任务类名不能为空")</span>
    <span class="hljs-meta">@ApiModelProperty(value = "任务类名", required = true)</span>
    <span class="hljs-keyword">private</span> String jobName;

    <span class="hljs-comment">/**
     * 组名+任务类key组成唯一标识,所以如果这个参数为空,那么默认以任务类key作为组名
     */</span>
    <span class="hljs-meta">@ApiModelProperty(value = "任务组名,命名空间")</span>
    <span class="hljs-keyword">private</span> String jobGroup;

    <span class="hljs-meta">@ApiModelProperty(value = "任务数据")</span>
    <span class="hljs-keyword">private</span> Map&lt;String, Object&gt; jobDataMap;

    <span class="hljs-meta">@ApiModelProperty(value = "cron表达式")</span>
    <span class="hljs-keyword">private</span> String cron;

    <span class="hljs-meta">@ApiModelProperty(value = "描述")</span>
    <span class="hljs-keyword">private</span> String description;



`}
`

@ApiModel(value = "Quartz任务更新请求参数")
public class QuartzUpdateParam extends BaseParam {
    @NotBlank(message = "任务类名不能为空")
    @ApiModelProperty(value = "任务类名", required = true)
    private String jobName;

    @ApiModelProperty(value = "任务组名,命名空间")
    private String jobGroup;

    @ApiModelProperty(value = "cron表达式")
    private String cron;
}
@ApiModel(value = "Quartz任务详情请求参数")
public class QuartzDetailParam extends BaseParam {
    @NotBlank(message = "任务类名不能为空")
    @ApiModelProperty(value = "任务类名", required = true)
    private String jobName;

    @ApiModelProperty(value = "任务组名,命名空间")
    private String jobGroup;
}

接口返回结果类 {#接口返回结果类}

@ApiModel(value = "Quartz定时任务详情类")
public class QuartzJobDetailDto {
    @ApiModelProperty(value = "任务类路径")
    private String jobClazz;

    <span class="hljs-meta">@ApiModelProperty(value = "任务类名")</span>
    <span class="hljs-keyword">private</span> String jobName;

    <span class="hljs-meta">@ApiModelProperty(value = "任务组名,命名空间")</span>
    <span class="hljs-keyword">private</span> String jobGroup;

    <span class="hljs-meta">@ApiModelProperty(value = "任务数据")</span>
    <span class="hljs-keyword">private</span> Map&lt;String, Object&gt; jobDataMap;

    <span class="hljs-meta">@ApiModelProperty(value = "触发器列表")</span>
    <span class="hljs-keyword">private</span> List&lt;QuartzTriggerDetailDto&gt; triggerDetailDtoList;



`}
`

@ApiModel(value = "Quartz定时任务触发器详情类")
public class QuartzTriggerDetailDto {
    private String triggerName;
    private String triggerGroup;
    private String cron;
    private String description;

    private String triggerState;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private List<Date> recentFireTimeList;
}

调用接口进行测试 {#调用接口进行测试}

写完接口代码后,我们来测试一下

添加任务接口:/quartz/addJob {#添加任务接口quartzaddjob}

{
    "jobClazz": "com.cc.job.QuartzTestJob",
    "jobName": "QuartzTestJob",
    "cron": "1/2 * * * * ? ",
    "description": "测试定时任务",
    "jobDataMap": {
        "key": "value"
    }
}

修改任务接口:/quartz/updateJob {#修改任务接口quartzupdatejob}

{
    "jobName": "QuartzTestJob",
    "cron": "0/2 * * * * ?"
}

修改任务只能修改cron时间,如果想要修改其他内容,只能删除任务后重新添加。

删除任务接口:/quartz/updateJob {#删除任务接口quartzupdatejob}

{
    "jobName": "QuartzTestJob"
}

暂停、恢复、详情接口同删除任务接口的请求参数,就不赘述了。

任务列表:/quartz/jobList {#任务列表quartzjoblist}

{}

返回结果:

{
    "code": 10000,
    "msg": "请求成功",
    "data": [
        {
            "jobClazz": "class com.cc.job.QuartzTestJob",
            "jobName": "QuartzTestJob",
            "jobGroup": "DEFAULT",
            "jobDataMap": {
                "key": "value"
            },
            "triggerDetailDtoList": [
                {
                    "triggerName": "DEFAULTQuartzTestJob",
                    "triggerGroup": "DEFAULT",
                    "cron": "0/2 * * * * ?",
                    "description": null,
                    "triggerState": "NORMAL",
                    "recentFireTimeList": [
                        "2023-07-19 09:23:16",
                        "2023-07-19 09:23:18",
                        "2023-07-19 09:23:20",
                        "2023-07-19 09:23:22",
                        "2023-07-19 09:23:24",
                        "2023-07-19 09:23:26",
                        "2023-07-19 09:23:28",
                        "2023-07-19 09:23:30",
                        "2023-07-19 09:23:32",
                        "2023-07-19 09:23:34"
                    ]
                }
            ]
        }
    ],
    "traceId": null,
    "success": true
}
赞(1)
未经允许不得转载:工具盒子 » SpringBoot整合Quartz实现动态配置