|
|
@@ -1,12 +1,14 @@
|
|
|
package cn.tr.module.quartz.job.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.core.exception.ServiceException;
|
|
|
import cn.tr.core.exception.TRExcCode;
|
|
|
import cn.tr.module.quartz.constant.ScheduleConstants;
|
|
|
-import cn.tr.module.quartz.core.mq.producer.QuartzProducer;
|
|
|
import cn.tr.module.quartz.exception.TaskException;
|
|
|
import cn.tr.module.quartz.utils.CronUtils;
|
|
|
import cn.tr.module.quartz.utils.ScheduleUtils;
|
|
|
+import cn.tr.plugin.eventbus.annotation.Subscribe;
|
|
|
+import cn.tr.plugin.eventbus.config.EventBus;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -38,8 +40,25 @@ import javax.annotation.PostConstruct;
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> implements ISysJobService {
|
|
|
+ /**
|
|
|
+ * 任务更新主题
|
|
|
+ */
|
|
|
+ private final String JOB_UPDATE_TOPIC="/quartz/job/update";
|
|
|
+ /**
|
|
|
+ * 任务状态刷新主题
|
|
|
+ */
|
|
|
+ private final String JOB_STATUS_TOPIC="/quartz/job/status";
|
|
|
+ /**
|
|
|
+ * 任务新增主题
|
|
|
+ */
|
|
|
+ private final String JOB_INSERT_TOPIC="/quartz/job/insert";
|
|
|
+ /**
|
|
|
+ * 任务删除主题
|
|
|
+ */
|
|
|
+ private final String JOB_REMOVE_TOPIC="/quartz/job/remove";
|
|
|
+
|
|
|
private final Scheduler scheduler;
|
|
|
- private final QuartzProducer quartzProducer;
|
|
|
+ private final EventBus eventBus;
|
|
|
@PostConstruct
|
|
|
@Override
|
|
|
public void initLocal() throws TaskException, SchedulerException {
|
|
|
@@ -86,11 +105,10 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
- public boolean updateSysJobById(SysJobDTO source) throws SchedulerException, TaskException {
|
|
|
- SysJobDTO properties = selectSysJobById(source.getJobId());
|
|
|
+ public boolean updateSysJobById(SysJobDTO source){
|
|
|
int rows = baseMapper.updateById(SysJobMapper.INSTANCE.convertPO(source));
|
|
|
if (rows > 0) {
|
|
|
- updateSchedulerJob(source, properties.getJobGroup());
|
|
|
+ eventBus.publishShare(JOB_UPDATE_TOPIC,source.getJobId());
|
|
|
}
|
|
|
return rows!=0;
|
|
|
};
|
|
|
@@ -103,13 +121,12 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public boolean insertSysJob(SysJobDTO source) throws SchedulerException, TaskException {
|
|
|
+ public boolean insertSysJob(SysJobDTO source) {
|
|
|
source.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
|
|
SysJobPO po = SysJobMapper.INSTANCE.convertPO(source);
|
|
|
int rows = baseMapper.insert(po);
|
|
|
if (rows > 0) {
|
|
|
- source.setJobId(po.getJobId());
|
|
|
- ScheduleUtils.createScheduleJob(scheduler, source);
|
|
|
+ eventBus.publishShare(JOB_INSERT_TOPIC,po.getJobId());
|
|
|
}
|
|
|
return rows!=0;
|
|
|
};
|
|
|
@@ -122,14 +139,15 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public int removeSysJobByIds(Collection<String> ids) throws SchedulerException {
|
|
|
+ public int removeSysJobByIds(Collection<String> ids) {
|
|
|
List<SysJobPO> jobs = this.listByIds(ids);
|
|
|
if(CollectionUtil.isEmpty(jobs)){
|
|
|
return CollectionUtil.size(ids);
|
|
|
}
|
|
|
- int result = baseMapper.deleteBatchIds(ids);
|
|
|
- removeJobs(SysJobMapper.INSTANCE.convertDtoList(jobs));
|
|
|
- return result;
|
|
|
+ for (SysJobDTO sysJobDTO : SysJobMapper.INSTANCE.convertDtoList(jobs)) {
|
|
|
+ eventBus.publishShare(JOB_REMOVE_TOPIC,sysJobDTO);
|
|
|
+ }
|
|
|
+ return baseMapper.deleteBatchIds(ids);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -155,40 +173,29 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean changeStatus(String id, String status) throws SchedulerException {
|
|
|
- quartzProducer.sendQuartzJobChangeStatusMessage(id,status);
|
|
|
+ SysJobPO job = this.baseMapper.selectById(id);
|
|
|
if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
|
|
|
- return resumeJob(id);
|
|
|
+ job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
|
|
|
}
|
|
|
else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
|
|
|
- return pauseJob(id);
|
|
|
+ job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
|
|
}
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void delCacheJob(SysJobDTO job) throws SchedulerException {
|
|
|
- removeJobs(Collections.singleton(job));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void refreshCacheJob(String jobId) throws TaskException, SchedulerException {
|
|
|
- SysJobDTO job = selectSysJobById(jobId);
|
|
|
- updateSchedulerJob(job,job.getJobGroup());
|
|
|
+ this.baseMapper.updateById(job);
|
|
|
+ this.eventBus.publishShare(JOB_STATUS_TOPIC,id);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除调度器中的任务
|
|
|
- * @param jobs
|
|
|
+ * @param job
|
|
|
* @throws SchedulerException
|
|
|
*/
|
|
|
- private void removeJobs(Collection<SysJobDTO> jobs) throws SchedulerException {
|
|
|
- for (SysJobDTO job : jobs) {
|
|
|
- String jobId = job.getJobId();
|
|
|
- String jobGroup = job.getJobGroup();
|
|
|
- scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
- }
|
|
|
- quartzProducer.sendQuartzDelMessage(jobs);
|
|
|
+ private void removeJobs(SysJobDTO job) throws SchedulerException {
|
|
|
+ String jobId = job.getJobId();
|
|
|
+ String jobGroup = job.getJobGroup();
|
|
|
+ scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -206,7 +213,6 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
scheduler.deleteJob(jobKey);
|
|
|
}
|
|
|
ScheduleUtils.createScheduleJob(scheduler, job);
|
|
|
- quartzProducer.sendQuartzJobRefreshMessage(jobId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -250,4 +256,31 @@ public class SysJobServiceImpl extends ServiceImpl<SysJobRepository,SysJobPO> im
|
|
|
}
|
|
|
return rows!=0;
|
|
|
}
|
|
|
+
|
|
|
+ @Subscribe(JOB_STATUS_TOPIC)
|
|
|
+ public void jobStatusChange(String jobId) throws SchedulerException {
|
|
|
+ SysJobDTO job = selectSysJobById(jobId);
|
|
|
+ if(StrUtil.equals(ScheduleConstants.Status.NORMAL.getValue(),job.getStatus())){
|
|
|
+ scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, job.getJobGroup()));
|
|
|
+ }else {
|
|
|
+ scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, job.getJobGroup()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(JOB_REMOVE_TOPIC)
|
|
|
+ public void jobRemove(SysJobDTO job) throws SchedulerException {
|
|
|
+ removeJobs(job);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(JOB_INSERT_TOPIC)
|
|
|
+ public void jobCreate(String jobId) throws TaskException, SchedulerException {
|
|
|
+ SysJobDTO job = selectSysJobById(jobId);
|
|
|
+ ScheduleUtils.createScheduleJob(scheduler, job);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(JOB_UPDATE_TOPIC)
|
|
|
+ public void jobUpdate(String jobId) throws TaskException, SchedulerException {
|
|
|
+ SysJobDTO job = selectSysJobById(jobId);
|
|
|
+ updateSchedulerJob(job, job.getJobGroup());
|
|
|
+ }
|
|
|
}
|