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(); }

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

}


结论 {#结论}

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

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

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

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

业务层 {#业务层}

public interface QuartzService {
    /**
     * 添加定时任务
     */
    void addJob(QuartzCreateParam param) throws SchedulerException;
&lt;span class=&quot;hljs-comment&quot;&gt;/**
 * 修改定时任务
 */&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;updateJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzUpdateParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException;

&lt;span class=&quot;hljs-comment&quot;&gt;/**

  • 暂停定时任务 */&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;pauseJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzDetailParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException;

&lt;span class=&quot;hljs-comment&quot;&gt;/**

  • 恢复定时任务 */&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;resumeJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzDetailParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException;

&lt;span class=&quot;hljs-comment&quot;&gt;/**

  • 删除定时任务 */&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;deleteJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzDetailParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException;

&lt;span class=&quot;hljs-comment&quot;&gt;/**

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

&lt;span class=&quot;hljs-comment&quot;&gt;/**

  • 定时任务详情 */&lt;/span&gt; QuartzJobDetailDto &lt;span class=&quot;hljs-title function_&quot;&gt;jobDetail&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzDetailParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException;

}


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

@Service
public class QuartzServiceImpl implements QuartzService {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
&lt;span class=&quot;hljs-meta&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;addJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(QuartzCreateParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException {
    &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;clazzName&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; param.getJobClazz();
    &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;jobName&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; param.getJobName();
    &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;jobGroup&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; param.getJobGroup();
    &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;cron&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; param.getCron();
    &lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;description&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; param.getDescription();
&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobKey.jobKey(jobName, jobGroup);
checkJobExist(jobKey);

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;Scheduler&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;scheduler&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; schedulerFactoryBean.getScheduler();
&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobDetail&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobDetail&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobBuilder.newJob(jobClass)
        .withIdentity(jobName, jobGroup)
        .usingJobData(jobDataMap)
        .build();

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

&amp;lt;span class=&amp;quot;hljs-keyword&amp;quot;&amp;gt;if&amp;lt;/span&amp;gt; (!scheduler.isShutdown()) {
    scheduler.start();
}

}

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobKey.jobKey(jobName, jobGroup);
&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;String&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;triggerId&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; jobKey.getGroup() + jobKey.getName();

checkJobExist(jobKey);

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;Trigger&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;trigger&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; triggerBuilder.build();
scheduler.rescheduleJob(triggerKey, trigger);

}

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobKey.jobKey(jobName, jobGroup);
checkJobExist(jobKey);

scheduler.pauseJob(jobKey);

}

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobKey.jobKey(jobName, jobGroup);
checkJobExist(jobKey);

scheduler.resumeJob(jobKey);

}

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;JobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobKey&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; JobKey.jobKey(jobName, jobGroup);
checkJobExist(jobKey);

&amp;lt;span class=&amp;quot;hljs-comment&amp;quot;&amp;gt;// 先暂停再删除&amp;lt;/span&amp;gt;
scheduler.pauseJob(jobKey);
scheduler.deleteJob(jobKey);

}

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

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

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

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

&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;QuartzJobDetailDto&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;jobDto&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-keyword&amp;quot;&amp;gt;new&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-title class_&amp;quot;&amp;gt;QuartzJobDetailDto&amp;lt;/span&amp;gt;();
jobDto.setJobClazz(jobDetail.getJobClass().toString());
jobDto.setJobName(jobKey.getName());
jobDto.setJobGroup(jobKey.getGroup());
jobDto.setJobDataMap(jobDetail.getJobDataMap());

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

    &amp;lt;span class=&amp;quot;hljs-keyword&amp;quot;&amp;gt;if&amp;lt;/span&amp;gt; (trigger &amp;lt;span class=&amp;quot;hljs-keyword&amp;quot;&amp;gt;instanceof&amp;lt;/span&amp;gt; CronTriggerImpl) {
        &amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;CronTriggerImpl&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;cronTriggerImpl&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; (CronTriggerImpl) trigger;
        &amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;String&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;cronExpression&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; cronTriggerImpl.getCronExpression();
        triggerDto.setCron(cronExpression);

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

    Trigger.&amp;lt;span class=&amp;quot;hljs-type&amp;quot;&amp;gt;TriggerState&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-variable&amp;quot;&amp;gt;triggerState&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;hljs-operator&amp;quot;&amp;gt;=&amp;lt;/span&amp;gt; scheduler.getTriggerState(trigger.getKey());
    triggerDto.setTriggerState(triggerState.toString());

    triggerDtoList.add(triggerDto);
}
jobDto.setTriggerDetailDtoList(triggerDtoList);
&amp;lt;span class=&amp;quot;hljs-keyword&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; jobDto;

}

}


接口层 {#接口层}

@RestController
public class QuartzController {
    @Autowired
    private QuartzServiceImpl quartzService;
&lt;span class=&quot;hljs-meta&quot;&gt;@PostMapping(&quot;/quartz/addJob&quot;)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;addJob&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(&lt;span class=&quot;hljs-meta&quot;&gt;@RequestBody&lt;/span&gt; QuartzCreateParam param)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;throws&lt;/span&gt; SchedulerException {
    quartzService.addJob(param);
}

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

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

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

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

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

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

}


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

@ApiModel(value = "Quartz任务添加请求参数")
public class QuartzCreateParam extends BaseParam {
    @NotBlank(message = "任务类不能为空")
    @ApiModelProperty(value = "任务类路径", required = true)
    private String jobClazz;
&lt;span class=&quot;hljs-meta&quot;&gt;@NotBlank(message = &quot;任务类名不能为空&quot;)&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;任务类名&quot;, required = true)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; String jobName;

&lt;span class=&quot;hljs-comment&quot;&gt;/**

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

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

&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;cron表达式&quot;)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; String cron;

&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;描述&quot;)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; String description;

}


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

@ApiModelProperty(value = &quot;cron表达式&quot;)
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;
&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;任务类名&quot;)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; String jobName;

&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;任务组名,命名空间&quot;)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; String jobGroup;

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

&lt;span class=&quot;hljs-meta&quot;&gt;@ApiModelProperty(value = &quot;触发器列表&quot;)&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; List&amp;lt;QuartzTriggerDetailDto&amp;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 = &quot;yyyy-MM-dd HH:mm:ss&quot;)
private List&lt;Date&gt; 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
}
赞(4)
未经允许不得转载:工具盒子 » SpringBoot整合Quartz实现动态配置