|
@@ -4,6 +4,9 @@ import cn.iocoder.yudao.framework.quartz.core.enums.JobDataKeyEnum;
|
|
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandlerInvoker;
|
|
|
import org.quartz.*;
|
|
|
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
|
|
|
+
|
|
|
/**
|
|
|
* {@link org.quartz.Scheduler} 的管理器,负责创建任务
|
|
|
*
|
|
@@ -37,6 +40,7 @@ public class SchedulerManager {
|
|
|
public void addJob(Long jobId, String jobHandlerName, String jobHandlerParam, String cronExpression,
|
|
|
Integer retryCount, Integer retryInterval)
|
|
|
throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
// 创建 JobDetail 对象
|
|
|
JobDetail jobDetail = JobBuilder.newJob(JobHandlerInvoker.class)
|
|
|
.usingJobData(JobDataKeyEnum.JOB_ID.name(), jobId)
|
|
@@ -61,6 +65,7 @@ public class SchedulerManager {
|
|
|
public void updateJob(String jobHandlerName, String jobHandlerParam, String cronExpression,
|
|
|
Integer retryCount, Integer retryInterval)
|
|
|
throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
// 创建新 Trigger 对象
|
|
|
Trigger newTrigger = this.buildTrigger(jobHandlerName, jobHandlerParam, cronExpression, retryCount, retryInterval);
|
|
|
// 修改调度
|
|
@@ -74,6 +79,7 @@ public class SchedulerManager {
|
|
|
* @throws SchedulerException 删除异常
|
|
|
*/
|
|
|
public void deleteJob(String jobHandlerName) throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
scheduler.deleteJob(new JobKey(jobHandlerName));
|
|
|
}
|
|
|
|
|
@@ -84,6 +90,7 @@ public class SchedulerManager {
|
|
|
* @throws SchedulerException 暂停异常
|
|
|
*/
|
|
|
public void pauseJob(String jobHandlerName) throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
scheduler.pauseJob(new JobKey(jobHandlerName));
|
|
|
}
|
|
|
|
|
@@ -94,6 +101,7 @@ public class SchedulerManager {
|
|
|
* @throws SchedulerException 启动异常
|
|
|
*/
|
|
|
public void resumeJob(String jobHandlerName) throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
scheduler.resumeJob(new JobKey(jobHandlerName));
|
|
|
scheduler.resumeTrigger(new TriggerKey(jobHandlerName));
|
|
|
}
|
|
@@ -108,11 +116,12 @@ public class SchedulerManager {
|
|
|
*/
|
|
|
public void triggerJob(Long jobId, String jobHandlerName, String jobHandlerParam)
|
|
|
throws SchedulerException {
|
|
|
+ validateScheduler();
|
|
|
+ // 触发任务
|
|
|
JobDataMap data = new JobDataMap(); // 无需重试,所以不设置 retryCount 和 retryInterval
|
|
|
data.put(JobDataKeyEnum.JOB_ID.name(), jobId);
|
|
|
data.put(JobDataKeyEnum.JOB_HANDLER_NAME.name(), jobHandlerName);
|
|
|
data.put(JobDataKeyEnum.JOB_HANDLER_PARAM.name(), jobHandlerParam);
|
|
|
- // 触发任务
|
|
|
scheduler.triggerJob(new JobKey(jobHandlerName), data);
|
|
|
}
|
|
|
|
|
@@ -127,4 +136,11 @@ public class SchedulerManager {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ private void validateScheduler() {
|
|
|
+ if (scheduler == null) {
|
|
|
+ throw exception0(NOT_IMPLEMENTED.getCode(),
|
|
|
+ "[定时任务 - 已禁用][参考 https://doc.iocoder.cn/job/ 开启]");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|