mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-17 20:45:06 +08:00
默认禁用 quartz 依赖,保证启动速度
This commit is contained in:
@ -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> scheduler) {
|
||||
if (!scheduler.isPresent()) {
|
||||
log.info("[定时任务 - 已禁用][参考 https://doc.iocoder.cn/job/ 开启]");
|
||||
return new SchedulerManager(null);
|
||||
}
|
||||
return new SchedulerManager(scheduler.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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/ 开启]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user