From 168b63ca8bf9a01dc305031bb415ad83563282cd Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 14 Feb 2021 00:05:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=20Quartz=20=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/quartz/config/QuartzConfig.java | 9 ++++ .../core/handler/JobHandlerInvoker.java | 8 +-- .../core/scheduler/SchedulerManager.java | 39 +++++++-------- .../infra/dal/dataobject/job/InfJobDO.java | 6 --- .../job/auth/SysUserSessionTimeoutJob.java | 19 +++++++ .../core/scheduler/SchedulerManagerTest.java | 50 +++++++++++++++++++ 6 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/job/auth/SysUserSessionTimeoutJob.java create mode 100644 src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/quartz/config/QuartzConfig.java b/src/main/java/cn/iocoder/dashboard/framework/quartz/config/QuartzConfig.java index 24abfa9b7..30d883739 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/quartz/config/QuartzConfig.java +++ b/src/main/java/cn/iocoder/dashboard/framework/quartz/config/QuartzConfig.java @@ -1,9 +1,18 @@ package cn.iocoder.dashboard.framework.quartz.config; +import cn.iocoder.dashboard.framework.quartz.core.scheduler.SchedulerManager; +import org.quartz.Scheduler; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @EnableScheduling // 开启 Spring 自带的定时任务 public class QuartzConfig { + + @Bean + public SchedulerManager schedulerManager(Scheduler scheduler) { + return new SchedulerManager(scheduler); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/framework/quartz/core/handler/JobHandlerInvoker.java b/src/main/java/cn/iocoder/dashboard/framework/quartz/core/handler/JobHandlerInvoker.java index 8989475de..b5ab7e019 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/quartz/core/handler/JobHandlerInvoker.java +++ b/src/main/java/cn/iocoder/dashboard/framework/quartz/core/handler/JobHandlerInvoker.java @@ -36,14 +36,8 @@ public class JobHandlerInvoker extends QuartzJobBean { } } - @SuppressWarnings("SwitchStatementWithTooFewBranches") private static String getJobData(JobExecutionContext executionContext, JobDataKeyEnum key) { - switch (key) { - case JOB_ID: - return executionContext.getJobDetail().getJobDataMap().getString(key.name()); - default: - return executionContext.getTrigger().getJobDataMap().getString(key.name()); - } + return executionContext.getMergedJobDataMap().getString(key.name()); } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManager.java b/src/main/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManager.java index 7223374e7..6004ca620 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManager.java +++ b/src/main/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManager.java @@ -17,35 +17,25 @@ public class SchedulerManager { this.scheduler = scheduler; } - public void addJob(Long jobId, String jobHandlerName, String jobHandlerParam, - String triggerName, String cronExpression) + public void addJob(Long jobId, String jobHandlerName, String jobHandlerParam, String cronExpression) throws SchedulerException { // 创建 JobDetail 对象 JobDetail jobDetail = JobBuilder.newJob(JobHandlerInvoker.class) .usingJobData(JobDataKeyEnum.JOB_ID.name(), jobId) + .usingJobData(JobDataKeyEnum.JOB_HANDLER_NAME.name(), jobHandlerName) .withIdentity(jobHandlerName).build(); // 创建 Trigger 对象 - Trigger trigger = TriggerBuilder.newTrigger() - .withIdentity(triggerName) - .usingJobData(JobDataKeyEnum.JOB_HANDLER_NAME.name(), jobHandlerName) - .usingJobData(JobDataKeyEnum.JOB_HANDLER_PARAM.name(), jobHandlerParam) - .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) - .build(); + Trigger trigger = this.buildTrigger(jobHandlerName, jobHandlerParam, cronExpression); // 新增调度 scheduler.scheduleJob(jobDetail, trigger); } - public void updateJob(String jobHandlerName, String jobHandlerParam, - String triggerName, String cronExpression) throws SchedulerException { - // 创建 Trigger 对象 - Trigger trigger = TriggerBuilder.newTrigger() - .withIdentity(triggerName) - .usingJobData(JobDataKeyEnum.JOB_HANDLER_NAME.name(), jobHandlerName) - .usingJobData(JobDataKeyEnum.JOB_HANDLER_PARAM.name(), jobHandlerParam) - .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) - .build(); + public void updateJob(String jobHandlerName, String jobHandlerParam, String cronExpression) + throws SchedulerException { + // 创建新 Trigger 对象 + Trigger newTrigger = this.buildTrigger(jobHandlerName, jobHandlerParam, cronExpression); // 修改调度 - scheduler.rescheduleJob(new TriggerKey(triggerName), trigger); + scheduler.rescheduleJob(new TriggerKey(jobHandlerName), newTrigger); } public void deleteJob(String jobHandlerName) throws SchedulerException { @@ -56,10 +46,9 @@ public class SchedulerManager { scheduler.pauseJob(new JobKey(jobHandlerName)); } - public void resumeJob(String jobHandlerName, - String triggerName) throws SchedulerException { + public void resumeJob(String jobHandlerName) throws SchedulerException { scheduler.resumeJob(new JobKey(jobHandlerName)); - scheduler.resumeTrigger(new TriggerKey(triggerName)); + scheduler.resumeTrigger(new TriggerKey(jobHandlerName)); } public void triggerJob(Long jobId, String jobHandlerName, String jobHandlerParam) @@ -72,4 +61,12 @@ public class SchedulerManager { scheduler.triggerJob(new JobKey(jobHandlerName), data); } + private Trigger buildTrigger(String jobHandlerName, String jobHandlerParam, String cronExpression) { + return TriggerBuilder.newTrigger() + .withIdentity(jobHandlerName) + .usingJobData(JobDataKeyEnum.JOB_HANDLER_PARAM.name(), jobHandlerParam) + .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) + .build(); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/job/InfJobDO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/job/InfJobDO.java index 013a39027..9830095db 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/job/InfJobDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/job/InfJobDO.java @@ -2,7 +2,6 @@ package cn.iocoder.dashboard.modules.infra.dal.dataobject.job; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; -import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -31,11 +30,6 @@ public class InfJobDO extends BaseDO { * 任务名称 */ private String name; - /** - * 任务分组 - */ - @TableField("`group`") - private String group; /** * 任务状态 * diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/job/auth/SysUserSessionTimeoutJob.java b/src/main/java/cn/iocoder/dashboard/modules/system/job/auth/SysUserSessionTimeoutJob.java new file mode 100644 index 000000000..1231be16e --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/job/auth/SysUserSessionTimeoutJob.java @@ -0,0 +1,19 @@ +package cn.iocoder.dashboard.modules.system.job.auth; + +import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.framework.quartz.core.handler.JobHandler; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class SysUserSessionTimeoutJob implements JobHandler { + + @Override + public CommonResult execute(String param) throws Exception { +// System.out.println("执行了一次任务"); + log.info("[execute][执行任务:{}]", param); + return null; + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java b/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java new file mode 100644 index 000000000..fdfe1a241 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java @@ -0,0 +1,50 @@ +package cn.iocoder.dashboard.framework.quartz.core.scheduler; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.TestApplication; +import cn.iocoder.dashboard.modules.system.job.auth.SysUserSessionTimeoutJob; +import org.junit.jupiter.api.Test; +import org.quartz.SchedulerException; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class SchedulerManagerTest { + + @Resource + private SchedulerManager schedulerManager; + + @Test + public void testAddJob() throws SchedulerException { + String jobHandlerName = StrUtil.lowerFirst(SysUserSessionTimeoutJob.class.getSimpleName()); + schedulerManager.addJob(1L, jobHandlerName, "test", "0/10 * * * * ? *"); + } + + @Test + public void testUpdateJob() throws SchedulerException { + String jobHandlerName = StrUtil.lowerFirst(SysUserSessionTimeoutJob.class.getSimpleName()); + schedulerManager.updateJob(jobHandlerName, "hahaha", "0/20 * * * * ? *"); + } + + @Test + public void testPauseJob() throws SchedulerException { + String jobHandlerName = StrUtil.lowerFirst(SysUserSessionTimeoutJob.class.getSimpleName()); + schedulerManager.pauseJob(jobHandlerName); + } + + @Test + public void testResumeJob() throws SchedulerException { + String jobHandlerName = StrUtil.lowerFirst(SysUserSessionTimeoutJob.class.getSimpleName()); + schedulerManager.resumeJob(jobHandlerName); + } + + @Test + public void testTriggerJob() throws SchedulerException { + String jobHandlerName = StrUtil.lowerFirst(SysUserSessionTimeoutJob.class.getSimpleName()); + schedulerManager.triggerJob(1L, jobHandlerName, "niubi!!!"); + } + +}