Commit 1274f925449e12f4fff9105d52cfc698ff5e3592
1 parent
62c6c4b9
优化 job 调度:
1. 新增或修改 job 配置时,校验 handlerName 对应的 Spring Bean 存在 2. 删除 job 时,额外暂停 Trigger、取消调度,更完善
Showing
4 changed files
with
79 additions
and
39 deletions
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java
| ... | ... | @@ -48,7 +48,7 @@ public class SchedulerManager { |
| 48 | 48 | .withIdentity(jobHandlerName).build(); |
| 49 | 49 | // 创建 Trigger 对象 |
| 50 | 50 | Trigger trigger = this.buildTrigger(jobHandlerName, jobHandlerParam, cronExpression, retryCount, retryInterval); |
| 51 | - // 新增调度 | |
| 51 | + // 新增 Job 调度 | |
| 52 | 52 | scheduler.scheduleJob(jobDetail, trigger); |
| 53 | 53 | } |
| 54 | 54 | |
| ... | ... | @@ -80,6 +80,10 @@ public class SchedulerManager { |
| 80 | 80 | */ |
| 81 | 81 | public void deleteJob(String jobHandlerName) throws SchedulerException { |
| 82 | 82 | validateScheduler(); |
| 83 | + // 暂停 Trigger 对象 | |
| 84 | + scheduler.pauseTrigger(new TriggerKey(jobHandlerName)); | |
| 85 | + // 取消并删除 Job 调度 | |
| 86 | + scheduler.unscheduleJob(new TriggerKey(jobHandlerName)); | |
| 83 | 87 | scheduler.deleteJob(new JobKey(jobHandlerName)); |
| 84 | 88 | } |
| 85 | 89 | ... | ... |
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ErrorCodeConstants.java
| ... | ... | @@ -22,6 +22,8 @@ public interface ErrorCodeConstants { |
| 22 | 22 | ErrorCode JOB_CHANGE_STATUS_EQUALS = new ErrorCode(1_001_001_003, "定时任务已经处于该状态,无需修改"); |
| 23 | 23 | ErrorCode JOB_UPDATE_ONLY_NORMAL_STATUS = new ErrorCode(1_001_001_004, "只有开启状态的任务,才可以修改"); |
| 24 | 24 | ErrorCode JOB_CRON_EXPRESSION_VALID = new ErrorCode(1_001_001_005, "CRON 表达式不正确"); |
| 25 | + ErrorCode JOB_HANDLER_BEAN_NOT_EXISTS = new ErrorCode(1_001_001_006, "定时任务的处理器 Bean 不存在"); | |
| 26 | + ErrorCode JOB_HANDLER_BEAN_TYPE_ERROR = new ErrorCode(1_001_001_007, "定时任务的处理器 Bean 类型不正确,未实现 JobHandler 接口"); | |
| 25 | 27 | |
| 26 | 28 | // ========== API 错误日志 1-001-002-000 ========== |
| 27 | 29 | ErrorCode API_ERROR_LOG_NOT_FOUND = new ErrorCode(1_001_002_000, "API 错误日志不存在"); | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.job; |
| 2 | 2 | |
| 3 | +import cn.hutool.extra.spring.SpringUtil; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | |
| 5 | 7 | import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; |
| 6 | 8 | import cn.iocoder.yudao.framework.quartz.core.util.CronUtils; |
| 7 | 9 | import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobPageReqVO; |
| ... | ... | @@ -9,13 +11,12 @@ import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobSaveReqVO; |
| 9 | 11 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO; |
| 10 | 12 | import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper; |
| 11 | 13 | import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum; |
| 14 | +import jakarta.annotation.Resource; | |
| 12 | 15 | import org.quartz.SchedulerException; |
| 13 | 16 | import org.springframework.stereotype.Service; |
| 14 | 17 | import org.springframework.transaction.annotation.Transactional; |
| 15 | 18 | import org.springframework.validation.annotation.Validated; |
| 16 | 19 | |
| 17 | -import jakarta.annotation.Resource; | |
| 18 | - | |
| 19 | 20 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 20 | 21 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.containsAny; |
| 21 | 22 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; |
| ... | ... | @@ -39,24 +40,25 @@ public class JobServiceImpl implements JobService { |
| 39 | 40 | @Transactional(rollbackFor = Exception.class) |
| 40 | 41 | public Long createJob(JobSaveReqVO createReqVO) throws SchedulerException { |
| 41 | 42 | validateCronExpression(createReqVO.getCronExpression()); |
| 42 | - // 校验唯一性 | |
| 43 | + // 1.1 校验唯一性 | |
| 43 | 44 | if (jobMapper.selectByHandlerName(createReqVO.getHandlerName()) != null) { |
| 44 | 45 | throw exception(JOB_HANDLER_EXISTS); |
| 45 | 46 | } |
| 46 | - // 插入 | |
| 47 | + // 1.2 校验 JobHandler 是否存在 | |
| 48 | + validateJobHandlerExists(createReqVO.getHandlerName()); | |
| 49 | + | |
| 50 | + // 2. 插入 JobDO | |
| 47 | 51 | JobDO job = BeanUtils.toBean(createReqVO, JobDO.class); |
| 48 | 52 | job.setStatus(JobStatusEnum.INIT.getStatus()); |
| 49 | 53 | fillJobMonitorTimeoutEmpty(job); |
| 50 | 54 | jobMapper.insert(job); |
| 51 | 55 | |
| 52 | - // 添加 Job 到 Quartz 中 | |
| 56 | + // 3.1 添加 Job 到 Quartz 中 | |
| 53 | 57 | schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), |
| 54 | 58 | createReqVO.getRetryCount(), createReqVO.getRetryInterval()); |
| 55 | - // 更新 | |
| 59 | + // 3.2 更新 JobDO | |
| 56 | 60 | JobDO updateObj = JobDO.builder().id(job.getId()).status(JobStatusEnum.NORMAL.getStatus()).build(); |
| 57 | 61 | jobMapper.updateById(updateObj); |
| 58 | - | |
| 59 | - // 返回 | |
| 60 | 62 | return job.getId(); |
| 61 | 63 | } |
| 62 | 64 | |
| ... | ... | @@ -64,22 +66,35 @@ public class JobServiceImpl implements JobService { |
| 64 | 66 | @Transactional(rollbackFor = Exception.class) |
| 65 | 67 | public void updateJob(JobSaveReqVO updateReqVO) throws SchedulerException { |
| 66 | 68 | validateCronExpression(updateReqVO.getCronExpression()); |
| 67 | - // 校验存在 | |
| 69 | + // 1.1 校验存在 | |
| 68 | 70 | JobDO job = validateJobExists(updateReqVO.getId()); |
| 69 | - // 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行 | |
| 71 | + // 1.2 只有开启状态,才可以修改.原因是,如果出暂停状态,修改 Quartz Job 时,会导致任务又开始执行 | |
| 70 | 72 | if (!job.getStatus().equals(JobStatusEnum.NORMAL.getStatus())) { |
| 71 | 73 | throw exception(JOB_UPDATE_ONLY_NORMAL_STATUS); |
| 72 | 74 | } |
| 73 | - // 更新 | |
| 75 | + // 1.3 校验 JobHandler 是否存在 | |
| 76 | + validateJobHandlerExists(updateReqVO.getHandlerName()); | |
| 77 | + | |
| 78 | + // 2. 更新 JobDO | |
| 74 | 79 | JobDO updateObj = BeanUtils.toBean(updateReqVO, JobDO.class); |
| 75 | 80 | fillJobMonitorTimeoutEmpty(updateObj); |
| 76 | 81 | jobMapper.updateById(updateObj); |
| 77 | 82 | |
| 78 | - // 更新 Job 到 Quartz 中 | |
| 83 | + // 3. 更新 Job 到 Quartz 中 | |
| 79 | 84 | schedulerManager.updateJob(job.getHandlerName(), updateReqVO.getHandlerParam(), updateReqVO.getCronExpression(), |
| 80 | 85 | updateReqVO.getRetryCount(), updateReqVO.getRetryInterval()); |
| 81 | 86 | } |
| 82 | 87 | |
| 88 | + private void validateJobHandlerExists(String handlerName) { | |
| 89 | + Object handler = SpringUtil.getBean(handlerName); | |
| 90 | + if (handler == null) { | |
| 91 | + throw exception(JOB_HANDLER_BEAN_NOT_EXISTS); | |
| 92 | + } | |
| 93 | + if (!(handler instanceof JobHandler)) { | |
| 94 | + throw exception(JOB_HANDLER_BEAN_TYPE_ERROR); | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 83 | 98 | @Override |
| 84 | 99 | @Transactional(rollbackFor = Exception.class) |
| 85 | 100 | public void updateJobStatus(Long id, Integer status) throws SchedulerException { | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.job; |
| 2 | 2 | |
| 3 | +import cn.hutool.extra.spring.SpringUtil; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 5 | import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; |
| 5 | 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| ... | ... | @@ -8,7 +9,9 @@ import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobSaveReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO; |
| 9 | 10 | import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper; |
| 10 | 11 | import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum; |
| 12 | +import cn.iocoder.yudao.module.infra.job.job.JobLogCleanJob; | |
| 11 | 13 | import org.junit.jupiter.api.Test; |
| 14 | +import org.mockito.MockedStatic; | |
| 12 | 15 | import org.quartz.SchedulerException; |
| 13 | 16 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | 17 | import org.springframework.context.annotation.Import; |
| ... | ... | @@ -23,6 +26,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString |
| 23 | 26 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*; |
| 24 | 27 | import static org.junit.jupiter.api.Assertions.*; |
| 25 | 28 | import static org.mockito.ArgumentMatchers.eq; |
| 29 | +import static org.mockito.Mockito.mockStatic; | |
| 26 | 30 | import static org.mockito.Mockito.verify; |
| 27 | 31 | |
| 28 | 32 | @Import(JobServiceImpl.class) |
| ... | ... | @@ -35,6 +39,9 @@ public class JobServiceImplTest extends BaseDbUnitTest { |
| 35 | 39 | @MockBean |
| 36 | 40 | private SchedulerManager schedulerManager; |
| 37 | 41 | |
| 42 | + @MockBean | |
| 43 | + private JobLogCleanJob jobLogCleanJob; | |
| 44 | + | |
| 38 | 45 | @Test |
| 39 | 46 | public void testCreateJob_cronExpressionValid() { |
| 40 | 47 | // 准备参数。Cron 表达式为 String 类型,默认随机字符串。 |
| ... | ... | @@ -48,11 +55,15 @@ public class JobServiceImplTest extends BaseDbUnitTest { |
| 48 | 55 | public void testCreateJob_jobHandlerExists() throws SchedulerException { |
| 49 | 56 | // 准备参数 指定 Cron 表达式 |
| 50 | 57 | JobSaveReqVO reqVO = randomPojo(JobSaveReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")); |
| 51 | - | |
| 52 | - // 调用 | |
| 53 | - jobService.createJob(reqVO); | |
| 54 | - // 调用,并断言异常 | |
| 55 | - assertServiceException(() -> jobService.createJob(reqVO), JOB_HANDLER_EXISTS); | |
| 58 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | |
| 59 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(reqVO.getHandlerName()))) | |
| 60 | + .thenReturn(jobLogCleanJob); | |
| 61 | + | |
| 62 | + // 调用 | |
| 63 | + jobService.createJob(reqVO); | |
| 64 | + // 调用,并断言异常 | |
| 65 | + assertServiceException(() -> jobService.createJob(reqVO), JOB_HANDLER_EXISTS); | |
| 66 | + } | |
| 56 | 67 | } |
| 57 | 68 | |
| 58 | 69 | @Test |
| ... | ... | @@ -60,18 +71,22 @@ public class JobServiceImplTest extends BaseDbUnitTest { |
| 60 | 71 | // 准备参数 指定 Cron 表达式 |
| 61 | 72 | JobSaveReqVO reqVO = randomPojo(JobSaveReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")) |
| 62 | 73 | .setId(null); |
| 63 | - | |
| 64 | - // 调用 | |
| 65 | - Long jobId = jobService.createJob(reqVO); | |
| 66 | - // 断言 | |
| 67 | - assertNotNull(jobId); | |
| 68 | - // 校验记录的属性是否正确 | |
| 69 | - JobDO job = jobMapper.selectById(jobId); | |
| 70 | - assertPojoEquals(reqVO, job, "id"); | |
| 71 | - assertEquals(JobStatusEnum.NORMAL.getStatus(), job.getStatus()); | |
| 72 | - // 校验调用 | |
| 73 | - verify(schedulerManager).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), | |
| 74 | - eq(job.getCronExpression()), eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval())); | |
| 74 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | |
| 75 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(reqVO.getHandlerName()))) | |
| 76 | + .thenReturn(jobLogCleanJob); | |
| 77 | + | |
| 78 | + // 调用 | |
| 79 | + Long jobId = jobService.createJob(reqVO); | |
| 80 | + // 断言 | |
| 81 | + assertNotNull(jobId); | |
| 82 | + // 校验记录的属性是否正确 | |
| 83 | + JobDO job = jobMapper.selectById(jobId); | |
| 84 | + assertPojoEquals(reqVO, job, "id"); | |
| 85 | + assertEquals(JobStatusEnum.NORMAL.getStatus(), job.getStatus()); | |
| 86 | + // 校验调用 | |
| 87 | + verify(schedulerManager).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), | |
| 88 | + eq(job.getCronExpression()), eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval())); | |
| 89 | + } | |
| 75 | 90 | } |
| 76 | 91 | |
| 77 | 92 | @Test |
| ... | ... | @@ -109,15 +124,19 @@ public class JobServiceImplTest extends BaseDbUnitTest { |
| 109 | 124 | o.setId(job.getId()); |
| 110 | 125 | o.setCronExpression("0 0/1 * * * ? *"); |
| 111 | 126 | }); |
| 112 | - | |
| 113 | - // 调用 | |
| 114 | - jobService.updateJob(updateReqVO); | |
| 115 | - // 校验记录的属性是否正确 | |
| 116 | - JobDO updateJob = jobMapper.selectById(updateReqVO.getId()); | |
| 117 | - assertPojoEquals(updateReqVO, updateJob); | |
| 118 | - // 校验调用 | |
| 119 | - verify(schedulerManager).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), | |
| 120 | - eq(updateReqVO.getCronExpression()), eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval())); | |
| 127 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | |
| 128 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(updateReqVO.getHandlerName()))) | |
| 129 | + .thenReturn(jobLogCleanJob); | |
| 130 | + | |
| 131 | + // 调用 | |
| 132 | + jobService.updateJob(updateReqVO); | |
| 133 | + // 校验记录的属性是否正确 | |
| 134 | + JobDO updateJob = jobMapper.selectById(updateReqVO.getId()); | |
| 135 | + assertPojoEquals(updateReqVO, updateJob); | |
| 136 | + // 校验调用 | |
| 137 | + verify(schedulerManager).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), | |
| 138 | + eq(updateReqVO.getCronExpression()), eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval())); | |
| 139 | + } | |
| 121 | 140 | } |
| 122 | 141 | |
| 123 | 142 | @Test | ... | ... |