本文转载自: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();
&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);
checkJobExist(jobKey);
Class&amp;lt;? &lt;span class=&quot;hljs-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Job&lt;/span&gt;&amp;gt; jobClass = &lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;;
&lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
jobClass = (Class&amp;lt;? &lt;span class=&quot;hljs-keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Job&lt;/span&gt;&amp;gt;) Class.forName(clazzName);
} &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (ClassNotFoundException e) {
&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; + clazzName);
}
&lt;span class=&quot;hljs-type&quot;&gt;JobDataMap&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;jobDataMap&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;JobDataMap&lt;/span&gt;();
&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (param.getJobDataMap() != &lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;) {
param.getJobDataMap().forEach(jobDataMap::put);
}
&lt;span class=&quot;hljs-type&quot;&gt;Scheduler&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;scheduler&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; schedulerFactoryBean.getScheduler();
&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; JobBuilder.newJob(jobClass)
.withIdentity(jobName, jobGroup)
.usingJobData(jobDataMap)
.build();
&lt;span class=&quot;hljs-type&quot;&gt;CronScheduleBuilder&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;scheduleBuilder&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; CronScheduleBuilder.cronSchedule(cron);
&lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;triggerId&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; jobKey.getGroup() + jobKey.getName();
&lt;span class=&quot;hljs-type&quot;&gt;Trigger&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;trigger&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; TriggerBuilder.newTrigger()
.withSchedule(scheduleBuilder)
.withIdentity(triggerId)
.withDescription(description)
.build();
scheduler.scheduleJob(jobDetail, trigger);
&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (!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();
&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-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;triggerId&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; jobKey.getGroup() + jobKey.getName();
checkJobExist(jobKey);
&lt;span class=&quot;hljs-type&quot;&gt;TriggerKey&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;triggerKey&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; TriggerKey.triggerKey(triggerId);
&lt;span class=&quot;hljs-type&quot;&gt;CronScheduleBuilder&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;scheduleBuilder&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; CronScheduleBuilder.cronSchedule(cron);
TriggerBuilder&amp;lt;?&amp;gt; triggerBuilder = TriggerBuilder.newTrigger()
.withSchedule(scheduleBuilder)
.withIdentity(triggerId);
&lt;span class=&quot;hljs-type&quot;&gt;Trigger&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;trigger&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; 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();
&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);
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();
&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);
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();
&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);
checkJobExist(jobKey);
&lt;span class=&quot;hljs-comment&quot;&gt;// 先暂停再删除&lt;/span&gt;
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);
&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; &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;QuartzJobDetailDto&lt;/span&gt;();
jobDto.setJobClazz(jobDetail.getJobClass().toString());
jobDto.setJobName(jobKey.getName());
jobDto.setJobGroup(jobKey.getGroup());
jobDto.setJobDataMap(jobDetail.getJobDataMap());
List&amp;lt;QuartzTriggerDetailDto&amp;gt; triggerDtoList = &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; (Trigger trigger : triggerList) {
&lt;span class=&quot;hljs-type&quot;&gt;QuartzTriggerDetailDto&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;triggerDto&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;QuartzTriggerDetailDto&lt;/span&gt;();
triggerDto.setTriggerName(trigger.getKey().getName());
triggerDto.setTriggerGroup(trigger.getKey().getGroup());
triggerDto.setDescription(trigger.getDescription());
&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (trigger &lt;span class=&quot;hljs-keyword&quot;&gt;instanceof&lt;/span&gt; CronTriggerImpl) {
&lt;span class=&quot;hljs-type&quot;&gt;CronTriggerImpl&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;cronTriggerImpl&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; (CronTriggerImpl) trigger;
&lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;cronExpression&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; cronTriggerImpl.getCronExpression();
triggerDto.setCron(cronExpression);
&lt;span class=&quot;hljs-comment&quot;&gt;// 最近10次的触发时间&lt;/span&gt;
List&amp;lt;Date&amp;gt; dates = TriggerUtils.computeFireTimes(cronTriggerImpl, &lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;);
triggerDto.setRecentFireTimeList(dates);
}
Trigger.&lt;span class=&quot;hljs-type&quot;&gt;TriggerState&lt;/span&gt; &lt;span class=&quot;hljs-variable&quot;&gt;triggerState&lt;/span&gt; &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; scheduler.getTriggerState(trigger.getKey());
triggerDto.setTriggerState(triggerState.toString());
triggerDtoList.add(triggerDto);
}
jobDto.setTriggerDetailDtoList(triggerDtoList);
&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; 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
}