diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java index f414e989a7..aaf4fe5c47 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java @@ -1,21 +1,29 @@ package cn.iocoder.yudao.framework.quartz.config; import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; +import lombok.extern.slf4j.Slf4j; import org.quartz.Scheduler; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; +import java.util.Optional; + /** * 定时任务 Configuration */ @AutoConfiguration @EnableScheduling // 开启 Spring 自带的定时任务 +@Slf4j public class YudaoQuartzAutoConfiguration { @Bean - public SchedulerManager schedulerManager(Scheduler scheduler) { - return new SchedulerManager(scheduler); + public SchedulerManager schedulerManager(Optional scheduler) { + if (!scheduler.isPresent()) { + log.info("[定时任务 - 已禁用][参考 https://doc.iocoder.cn/job/ 开启]"); + return new SchedulerManager(null); + } + return new SchedulerManager(scheduler.get()); } } diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java index 1c8aa7ba15..cb2dad455a 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java @@ -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/ 开启]"); + } + } + } diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 5dc82c68c1..6f4848b480 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -9,6 +9,7 @@ spring: exclude: - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源 - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration # 排除积木报表带来的 MongoDB 的自动配置 + - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置 datasource: druid: # Druid 【监控】相关的全局配置 web-stat-filter: @@ -78,7 +79,6 @@ spring: # Quartz 配置项,对应 QuartzProperties 配置类 spring: quartz: - auto-startup: false # 本地开发环境,尽量不要开启 Job scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。 wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true