测试 Quartz 的代码
This commit is contained in:
parent
dadb66c8e9
commit
168b63ca8b
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
/**
|
||||
* 任务状态
|
||||
*
|
||||
|
|
|
@ -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<String> execute(String param) throws Exception {
|
||||
// System.out.println("执行了一次任务");
|
||||
log.info("[execute][执行任务:{}]", param);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -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!!!");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue