引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
代码实现
package net.evecom.iaplatform.backup.schedule;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
/**
* <p>
* <B>Description: 动态增删改定时器</B>
* </P>
* 2024/6/26 Ryan Huang CREATE
*
* @author Ryan Huang
* @version 1.0
*/
public class DynamicQuartzOperations {
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(DynamicQuartzOperations.class);
/**
* 添加定时任务
*
* @param jobName 任务名称
* @param jobGroup 任务组
* @param triggerName 触发器名称
* @param triggerGroup 触发器组
* @param cronExpression cron表达式
* @param jobClass 任务类
*/
public static void addJob(String jobName, String jobGroup, String triggerName,
String triggerGroup, String cronExpression, Class<? extends Job> jobClass) {
try {
// 获取Scheduler实例
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
// 定义JobDetail
JobDetail job = JobBuilder.newJob(jobClass)
.withIdentity(jobName, jobGroup)
.build();
// 定义Trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerName, triggerGroup)
.startNow()
.withSchedule(CronScheduleBuilder
.cronSchedule(cronExpression))
.build();
// 将JobDetail和Trigger注册到Scheduler中
scheduler.scheduleJob(job, trigger);
LOG.info("Job added successfully.");
} catch (SchedulerException e) {
LOG.error("Error adding job: " + e.getMessage());
}
}
/**
* 修改定时任务
*
* @param triggerName 触发器名称
* @param triggerGroup 触发器组
* @param newCronExpression 新cron表达式
*/
public static void modifyCronTrigger(String triggerName, String triggerGroup, String newCronExpression) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
Trigger trigger = scheduler.getTrigger(triggerKey);
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
// 停止触发器
scheduler.pauseTrigger(triggerKey);
// 重建CronTrigger
CronTrigger newCronTrigger = cronTrigger.getTriggerBuilder()
.withIdentity(triggerName, triggerGroup)
.withSchedule(CronScheduleBuilder.cronSchedule(newCronExpression))
.build();
// 更新触发器
scheduler.rescheduleJob(triggerKey, newCronTrigger);
LOG.info("Cron trigger modified successfully.");
} else if (trigger != null) {
LOG.info("Trigger with name '{}' and group '{}' is not a CronTrigger.", triggerName, triggerGroup);
} else {
LOG.info("Trigger not found.");
}
} catch (SchedulerException e) {
LOG.error("Error modifying cron trigger: " + e.getMessage(), e);
}
}
/**
* 删除定时任务
*
* @param jobName 任务名称
* @param jobGroup 任务组
*/
public static void deleteJob(String jobName, String jobGroup) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
if (scheduler.checkExists(jobKey)) {
// 停止并删除Job及其相关的Trigger
scheduler.deleteJob(jobKey);
LOG.info("Job deleted successfully.");
} else {
LOG.info("Job does not exist.");
}
} catch (SchedulerException e) {
LOG.error("Error deleting job: " + e.getMessage());
}
}
/**
* 启用任务
*
* @param jobName 任务名称
* @param jobGroup 任务组
*/
public static void resumeJob(String jobName, String jobGroup) {
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
if (scheduler.checkExists(jobKey)) {
scheduler.resumeJob(jobKey);
LOG.info("Job resumed successfully: {}", jobKey);
} else {
LOG.info("Job does not exist: {}", jobKey);
}
} catch (SchedulerException e) {
LOG.error("Error resuming job: {}", jobKey, e);
}
}
/**
* 停用任务
*
* @param jobName 任务名称
* @param jobGroup 任务组
*/
public static void pauseJob(String jobName, String jobGroup) {
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
if (scheduler.checkExists(jobKey)) {
scheduler.pauseJob(jobKey);
LOG.info("Job paused successfully: {}", jobKey);
} else {
LOG.info("Job does not exist: {}", jobKey);
}
} catch (SchedulerException e) {
LOG.error("Error pausing job: {}", jobKey, e);
}
}
/**
* 判断任务是否存在
*
* @param jobName 任务名称
* @param jobGroup 任务组
*/
public static boolean isExists(String jobName, String jobGroup) {
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
if (scheduler.checkExists(jobKey)) {
return true;
}
} catch (SchedulerException e) {
LOG.error("Error job: {}", jobKey, e);
}
return false;
}
}