♻️ Refactoring code. 重构quartz 代码

This commit is contained in:
lbw 2023-07-12 16:26:34 +08:00
parent cc5a9b22c0
commit 2f675791ca
12 changed files with 60 additions and 57 deletions

View File

@ -17,7 +17,7 @@
package com.pig4cloud.pig.daemon.quartz.config;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.service.SysJobService;
import com.pig4cloud.pig.daemon.quartz.util.TaskUtil;
import lombok.AllArgsConstructor;
@ -34,7 +34,7 @@ import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
@AllArgsConstructor
public class PigxInitQuartzJob {
public class PigInitQuartzJob {
private final SysJobService sysJobService;
@ -45,13 +45,13 @@ public class PigxInitQuartzJob {
@Bean
public void customize() {
sysJobService.list().forEach(sysjob -> {
if (PigxQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())) {
if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())) {
taskUtil.removeJob(sysjob, scheduler);
}
else if (PigxQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())) {
else if (PigQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())) {
taskUtil.resumeJob(sysjob, scheduler);
}
else if (PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
else if (PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
taskUtil.pauseJob(sysjob, scheduler);
}
else {

View File

@ -44,7 +44,7 @@ import java.util.Properties;
@Configuration
@ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class })
@EnableConfigurationProperties({ QuartzProperties.class })
public class PigxQuartzConfig {
public class PigQuartzConfig {
private final QuartzProperties properties;
@ -58,7 +58,7 @@ public class PigxQuartzConfig {
private final ApplicationContext applicationContext;
public PigxQuartzConfig(QuartzProperties properties,
public PigQuartzConfig(QuartzProperties properties,
ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers, ObjectProvider<JobDetail[]> jobDetails,
ObjectProvider<Map<String, Calendar>> calendars, ObjectProvider<Trigger[]> triggers,
ApplicationContext applicationContext) {

View File

@ -25,7 +25,7 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
* @author 郑健楠
*/
@Configuration
public class PigxQuartzCustomizerConfig implements SchedulerFactoryBeanCustomizer {
public class PigQuartzCustomizerConfig implements SchedulerFactoryBeanCustomizer {
@Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {

View File

@ -17,7 +17,7 @@
package com.pig4cloud.pig.daemon.quartz.config;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -34,16 +34,16 @@ import org.springframework.beans.factory.annotation.Autowired;
*/
@Slf4j
@DisallowConcurrentExecution
public class PigxQuartzFactory implements Job {
public class PigQuartzFactory implements Job {
@Autowired
private PigxQuartzInvokeFactory pigxQuartzInvokeFactory;
private PigQuartzInvokeFactory pigxQuartzInvokeFactory;
@Override
@SneakyThrows
public void execute(JobExecutionContext jobExecutionContext) {
SysJob sysJob = (SysJob) jobExecutionContext.getMergedJobDataMap()
.get(PigxQuartzEnum.SCHEDULE_JOB_KEY.getType());
.get(PigQuartzEnum.SCHEDULE_JOB_KEY.getType());
pigxQuartzInvokeFactory.init(sysJob, jobExecutionContext.getTrigger());
}

View File

@ -34,7 +34,7 @@ import org.springframework.stereotype.Service;
@Aspect
@Service
@AllArgsConstructor
public class PigxQuartzInvokeFactory {
public class PigQuartzInvokeFactory {
private final ApplicationEventPublisher publisher;

View File

@ -28,7 +28,7 @@ import lombok.Getter;
*/
@Getter
@AllArgsConstructor
public enum PigxQuartzEnum {
public enum PigQuartzEnum {
/**
* 错失执行策略默认

View File

@ -25,7 +25,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import com.pig4cloud.pig.daemon.quartz.entity.SysJobLog;
import com.pig4cloud.pig.daemon.quartz.service.SysJobLogService;
@ -103,9 +103,12 @@ public class SysJobController {
@PreAuthorize("@pms.hasPermission('job_sys_job_add')")
@Operation(description = "新增定时任务")
public R save(@RequestBody SysJob sysJob) {
sysJob.setJobStatus(PigxQuartzEnum.JOB_STATUS_RELEASE.getType());
sysJob.setJobStatus(PigQuartzEnum.JOB_STATUS_RELEASE.getType());
sysJob.setCreateBy(SecurityUtils.getUser().getUsername());
return R.ok(sysJobService.save(sysJob));
sysJobService.save(sysJob);
// 初始化任务
taskUtil.addOrUpateJob(sysJob, scheduler);
return R.ok();
}
/**
@ -120,11 +123,11 @@ public class SysJobController {
public R updateById(@RequestBody SysJob sysJob) {
sysJob.setUpdateBy(SecurityUtils.getUser().getUsername());
SysJob querySysJob = this.sysJobService.getById(sysJob.getJobId());
if (PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
if (PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
this.taskUtil.addOrUpateJob(sysJob, scheduler);
sysJobService.updateById(sysJob);
}
else if (PigxQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
sysJobService.updateById(sysJob);
}
return R.ok();
@ -141,11 +144,11 @@ public class SysJobController {
@Operation(description = "唯一标识查询定时任务,暂停任务才能删除")
public R removeById(@PathVariable Long id) {
SysJob querySysJob = this.sysJobService.getById(id);
if (PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
if (PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(querySysJob.getJobStatus())) {
this.taskUtil.removeJob(querySysJob, scheduler);
this.sysJobService.removeById(id);
}
else if (PigxQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
else if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(querySysJob.getJobStatus())) {
this.sysJobService.removeById(id);
}
return R.ok();
@ -162,16 +165,16 @@ public class SysJobController {
public R shutdownJobs() {
taskUtil.pauseJobs(scheduler);
long count = this.sysJobService.count(
new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, PigxQuartzEnum.JOB_STATUS_RUNNING.getType()));
new LambdaQueryWrapper<SysJob>().eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_RUNNING.getType()));
if (count <= 0) {
return R.ok("无正在运行定时任务");
}
else {
// 更新定时任务状态条件运行状态2更新为暂停状态2
this.sysJobService.update(
SysJob.builder().jobStatus(PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()).build(),
SysJob.builder().jobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()).build(),
new UpdateWrapper<SysJob>().lambda()
.eq(SysJob::getJobStatus, PigxQuartzEnum.JOB_STATUS_RUNNING.getType()));
.eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_RUNNING.getType()));
return R.ok("暂停成功");
}
}
@ -186,9 +189,9 @@ public class SysJobController {
@Operation(description = "启动全部定时任务")
public R startJobs() {
// 更新定时任务状态条件暂停状态3更新为运行状态2
this.sysJobService.update(SysJob.builder().jobStatus(PigxQuartzEnum.JOB_STATUS_RUNNING.getType()).build(),
this.sysJobService.update(SysJob.builder().jobStatus(PigQuartzEnum.JOB_STATUS_RUNNING.getType()).build(),
new UpdateWrapper<SysJob>().lambda()
.eq(SysJob::getJobStatus, PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()));
.eq(SysJob::getJobStatus, PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType()));
taskUtil.startJobs(scheduler);
return R.ok();
}
@ -203,12 +206,12 @@ public class SysJobController {
@Operation(description = "刷新全部定时任务")
public R refreshJobs() {
sysJobService.list().forEach((sysjob) -> {
if (PigxQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
|| PigxQuartzEnum.JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
if (PigQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())
|| PigQuartzEnum.JOB_STATUS_DEL.getType().equals(sysjob.getJobStatus())) {
taskUtil.removeJob(sysjob, scheduler);
}
else if (PigxQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
|| PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
else if (PigQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysjob.getJobStatus())
|| PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysjob.getJobStatus())) {
taskUtil.addOrUpateJob(sysjob, scheduler);
}
else {
@ -229,7 +232,7 @@ public class SysJobController {
@Operation(description = "启动定时任务")
public R startJob(@PathVariable("id") Long jobId) {
SysJob querySysJob = this.sysJobService.getById(jobId);
if (querySysJob != null && PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
if (querySysJob != null && PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(querySysJob.getJobStatus())) {
taskUtil.addOrUpateJob(querySysJob, scheduler);
}
else {
@ -237,7 +240,7 @@ public class SysJobController {
}
// 更新定时任务状态条件暂停状态3更新为运行状态2
this.sysJobService
.updateById(SysJob.builder().jobId(jobId).jobStatus(PigxQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
.updateById(SysJob.builder().jobId(jobId).jobStatus(PigQuartzEnum.JOB_STATUS_RUNNING.getType()).build());
return R.ok();
}
@ -268,7 +271,7 @@ public class SysJobController {
// 更新定时任务状态条件运行状态2更新为暂停状态3
this.sysJobService.updateById(SysJob.builder()
.jobId(querySysJob.getJobId())
.jobStatus(PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())
.jobStatus(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())
.build());
taskUtil.pauseJob(querySysJob, scheduler);
return R.ok();

View File

@ -17,7 +17,7 @@
package com.pig4cloud.pig.daemon.quartz.task;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@ -37,7 +37,7 @@ public class SpringBeanTaskDemo {
@SneakyThrows
public String demoMethod(String para) {
log.info("测试于:{},输入参数{}", LocalDateTime.now(), para);
return PigxQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
return PigQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
}
}

View File

@ -18,7 +18,7 @@
package com.pig4cloud.pig.daemon.quartz.util;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import com.pig4cloud.pig.daemon.quartz.exception.TaskException;
import lombok.extern.slf4j.Slf4j;
@ -56,7 +56,7 @@ public class JavaClassTaskInvok implements ITaskInvok {
returnValue = method.invoke(obj);
}
if (StrUtil.isEmpty(returnValue.toString())
|| PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
|| PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
log.error("定时任务javaClassTaskInvok异常,执行任务:{}", sysJob.getClassName());
throw new TaskException("定时任务javaClassTaskInvok业务执行失败,任务:" + sysJob.getClassName());
}

View File

@ -19,7 +19,7 @@ package com.pig4cloud.pig.daemon.quartz.util;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import com.pig4cloud.pig.daemon.quartz.exception.TaskException;
import lombok.extern.slf4j.Slf4j;
@ -57,7 +57,7 @@ public class SpringBeanTaskInvok implements ITaskInvok {
returnValue = method.invoke(target);
}
if (StrUtil.isEmpty(returnValue.toString())
|| PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
|| PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
log.error("定时任务springBeanTaskInvok异常,执行任务:{}", sysJob.getClassName());
throw new TaskException("定时任务springBeanTaskInvok业务执行失败,任务:" + sysJob.getClassName());
}

View File

@ -18,7 +18,7 @@
package com.pig4cloud.pig.daemon.quartz.util;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import com.pig4cloud.pig.daemon.quartz.entity.SysJobLog;
import com.pig4cloud.pig.daemon.quartz.event.SysJobLogEvent;
@ -78,20 +78,20 @@ public class TaskInvokUtil {
// 确保租户上下文有值使得当前线程中的多租户特性生效
iTaskInvok.invokMethod(sysJob);
// 记录成功状态
sysJobLog.setJobMessage(PigxQuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription());
sysJobLog.setJobLogStatus(PigxQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
sysJobLog.setJobMessage(PigQuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription());
sysJobLog.setJobLogStatus(PigQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
// 任务表信息更新
updateSysjob.setJobExecuteStatus(PigxQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
updateSysjob.setJobExecuteStatus(PigQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
}
catch (Throwable e) {
log.error("定时任务执行失败,任务名称:{};任务组名:{}cron执行表达式{},执行时间:{}", sysJob.getJobName(), sysJob.getJobGroup(),
sysJob.getCronExpression(), new Date());
// 记录失败状态
sysJobLog.setJobMessage(PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getDescription());
sysJobLog.setJobLogStatus(PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getType());
sysJobLog.setJobMessage(PigQuartzEnum.JOB_LOG_STATUS_FAIL.getDescription());
sysJobLog.setJobLogStatus(PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType());
sysJobLog.setExceptionInfo(StrUtil.sub(e.getMessage(), 0, 2000));
// 任务表信息更新
updateSysjob.setJobExecuteStatus(PigxQuartzEnum.JOB_LOG_STATUS_FAIL.getType());
updateSysjob.setJobExecuteStatus(PigQuartzEnum.JOB_LOG_STATUS_FAIL.getType());
}
finally {
// 记录执行时间 立刻执行使用的是simpleTeigger

View File

@ -17,8 +17,8 @@
package com.pig4cloud.pig.daemon.quartz.util;
import com.pig4cloud.pig.daemon.quartz.config.PigxQuartzFactory;
import com.pig4cloud.pig.daemon.quartz.constants.PigxQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.config.PigQuartzFactory;
import com.pig4cloud.pig.daemon.quartz.constants.PigQuartzEnum;
import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
@ -66,9 +66,9 @@ public class TaskUtil {
// 判断触发器是否存在如果存在说明之前运行过但是在当前被禁用了如果不存在说明一次都没运行过
if (trigger == null) {
// 新建一个工作任务 指定任务类型为串接进行的
JobDetail jobDetail = JobBuilder.newJob(PigxQuartzFactory.class).withIdentity(jobKey).build();
JobDetail jobDetail = JobBuilder.newJob(PigQuartzFactory.class).withIdentity(jobKey).build();
// 将任务信息添加到任务信息中
jobDetail.getJobDataMap().put(PigxQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysjob);
jobDetail.getJobDataMap().put(PigQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysjob);
// 将cron表达式进行转换
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(sysjob.getCronExpression());
cronScheduleBuilder = this.handleCronScheduleMisfirePolicy(sysjob, cronScheduleBuilder);
@ -89,12 +89,12 @@ public class TaskUtil {
.withSchedule(cronScheduleBuilder)
.build();
// 将任务信息更新到任务信息中
trigger.getJobDataMap().put(PigxQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysjob);
trigger.getJobDataMap().put(PigQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysjob);
// 重启
scheduler.rescheduleJob(triggerKey, trigger);
}
// 如任务状态为暂停
if (sysjob.getJobStatus().equals(PigxQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())) {
if (sysjob.getJobStatus().equals(PigQuartzEnum.JOB_STATUS_NOT_RUNNING.getType())) {
this.pauseJob(sysjob, scheduler);
}
}
@ -110,7 +110,7 @@ public class TaskUtil {
try {
// 参数
JobDataMap dataMap = new JobDataMap();
dataMap.put(PigxQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysJob);
dataMap.put(PigQuartzEnum.SCHEDULE_JOB_KEY.getType(), sysJob);
scheduler.triggerJob(getJobKey(sysJob), dataMap);
}
@ -215,16 +215,16 @@ public class TaskUtil {
*/
private CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob sysJob,
CronScheduleBuilder cronScheduleBuilder) {
if (PigxQuartzEnum.MISFIRE_DEFAULT.getType().equals(sysJob.getMisfirePolicy())) {
if (PigQuartzEnum.MISFIRE_DEFAULT.getType().equals(sysJob.getMisfirePolicy())) {
return cronScheduleBuilder;
}
else if (PigxQuartzEnum.MISFIRE_IGNORE_MISFIRES.getType().equals(sysJob.getMisfirePolicy())) {
else if (PigQuartzEnum.MISFIRE_IGNORE_MISFIRES.getType().equals(sysJob.getMisfirePolicy())) {
return cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
}
else if (PigxQuartzEnum.MISFIRE_FIRE_AND_PROCEED.getType().equals(sysJob.getMisfirePolicy())) {
else if (PigQuartzEnum.MISFIRE_FIRE_AND_PROCEED.getType().equals(sysJob.getMisfirePolicy())) {
return cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
}
else if (PigxQuartzEnum.MISFIRE_DO_NOTHING.getType().equals(sysJob.getMisfirePolicy())) {
else if (PigQuartzEnum.MISFIRE_DO_NOTHING.getType().equals(sysJob.getMisfirePolicy())) {
return cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
}
else {