|
@@ -0,0 +1,264 @@
|
|
|
|
+package com.usky.system.service.impl;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.ruoyi.common.core.constant.ScheduleConstants;
|
|
|
|
+import com.ruoyi.common.core.exception.job.TaskException;
|
|
|
|
+import com.usky.common.core.bean.CommonPage;
|
|
|
|
+import com.usky.common.mybatis.core.AbstractCrudService;
|
|
|
|
+import com.usky.system.domain.SysJob;
|
|
|
|
+import com.usky.system.mapper.SysJobMapper;
|
|
|
|
+import com.usky.system.service.SysJobService;
|
|
|
|
+import com.usky.system.service.util.CronUtils;
|
|
|
|
+import com.usky.system.service.util.ScheduleUtils;
|
|
|
|
+import org.quartz.JobDataMap;
|
|
|
|
+import org.quartz.JobKey;
|
|
|
|
+import org.quartz.Scheduler;
|
|
|
|
+import org.quartz.SchedulerException;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 定时任务调度表 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author JCB
|
|
|
|
+ * @since 2022-11-18
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class SysJobServiceImpl extends AbstractCrudService<SysJobMapper, SysJob> implements SysJobService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private Scheduler scheduler;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 项目启动时,初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理(注:不能手动修改数据库ID和任务组名,否则会导致脏数据)
|
|
|
|
+ */
|
|
|
|
+ @PostConstruct
|
|
|
|
+ public void init() throws SchedulerException, TaskException {
|
|
|
|
+ scheduler.clear();
|
|
|
|
+ LambdaQueryWrapper<SysJob> queryWrapper = Wrappers.lambdaQuery();
|
|
|
|
+ List<SysJob> jobList = this.list(queryWrapper);
|
|
|
|
+ for (SysJob job : jobList) {
|
|
|
|
+ ScheduleUtils.createScheduleJob(scheduler, job);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取quartz调度器的计划任务列表
|
|
|
|
+ *
|
|
|
|
+ * @param jobName
|
|
|
|
+ * @param jobGroup
|
|
|
|
+ * @param status
|
|
|
|
+ * @param invokeTarget
|
|
|
|
+ * @param id
|
|
|
|
+ * @param pageNum
|
|
|
|
+ * @param pageSize
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public CommonPage<SysJob> selectJobList(String jobName, String jobGroup, String status, String invokeTarget, Integer id, Integer pageNum, Integer pageSize) {
|
|
|
|
+ IPage<SysJob> page = new Page<>(pageNum, pageSize);
|
|
|
|
+ LambdaQueryWrapper<SysJob> queryWrapper = Wrappers.lambdaQuery();
|
|
|
|
+ queryWrapper.like(StringUtils.isNotBlank(jobName), SysJob::getJobName, jobName)
|
|
|
|
+ .eq(StringUtils.isNotBlank(jobGroup), SysJob::getJobGroup, jobGroup)
|
|
|
|
+ .eq(StringUtils.isNotBlank(status), SysJob::getStatus, status)
|
|
|
|
+ .like(StringUtils.isNotBlank(invokeTarget), SysJob::getInvokeTarget, invokeTarget)
|
|
|
|
+ .eq(id != null && id != 0, SysJob::getJobId, id);
|
|
|
|
+ page = this.page(page, queryWrapper);
|
|
|
|
+ return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通过调度任务ID查询调度信息
|
|
|
|
+ *
|
|
|
|
+ * @param jobId 调度任务ID
|
|
|
|
+ * @return 调度任务对象信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public SysJob selectJobById(Long jobId) {
|
|
|
|
+ LambdaQueryWrapper<SysJob> queryWrapper = Wrappers.lambdaQuery();
|
|
|
|
+ queryWrapper.eq(SysJob::getJobId, jobId);
|
|
|
|
+ SysJob one = this.getOne(queryWrapper);
|
|
|
|
+ return one;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 暂停任务
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public int pauseJob(SysJob job) throws SchedulerException {
|
|
|
|
+ Long jobId = job.getJobId();
|
|
|
|
+ String jobGroup = job.getJobGroup();
|
|
|
|
+ job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
|
|
|
+ job.setUpdateTime(LocalDateTime.now());
|
|
|
|
+ boolean b = this.updateById(job);
|
|
|
|
+ int rows = 0;
|
|
|
|
+ if (b) {
|
|
|
|
+ scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
|
+ rows = 1;
|
|
|
|
+ }
|
|
|
|
+ return rows;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 恢复任务
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public int resumeJob(SysJob job) throws SchedulerException {
|
|
|
|
+ Long jobId = job.getJobId();
|
|
|
|
+ String jobGroup = job.getJobGroup();
|
|
|
|
+ job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
|
|
|
|
+ job.setUpdateTime(LocalDateTime.now());
|
|
|
|
+ boolean b = this.updateById(job);
|
|
|
|
+ int rows = 0;
|
|
|
|
+ if (b) {
|
|
|
|
+ scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
|
+ rows = 1;
|
|
|
|
+ }
|
|
|
|
+ return rows;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除任务后,所对应的trigger也将被删除
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public int deleteJob(SysJob job) throws SchedulerException {
|
|
|
|
+ Long jobId = job.getJobId();
|
|
|
|
+ String jobGroup = job.getJobGroup();
|
|
|
|
+ boolean b = this.removeById(jobId);
|
|
|
|
+ int rows = 0;
|
|
|
|
+ if (b) {
|
|
|
|
+ scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
|
|
|
+ rows = 1;
|
|
|
|
+ }
|
|
|
|
+ return rows;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 批量删除调度信息
|
|
|
|
+ *
|
|
|
|
+ * @param jobId 需要删除的任务ID
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void deleteJobByIds(Long jobId) throws SchedulerException {
|
|
|
|
+// for (Long jobId : jobIds) {
|
|
|
|
+ LambdaQueryWrapper<SysJob> queryWrapper = Wrappers.lambdaQuery();
|
|
|
|
+ queryWrapper.eq(SysJob::getJobId, jobId);
|
|
|
|
+ SysJob job = this.getOne(queryWrapper);
|
|
|
|
+ deleteJob(job);
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 任务调度状态修改
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void changeStatus(SysJob job) throws SchedulerException {
|
|
|
|
+ String status = job.getStatus();
|
|
|
|
+ if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
|
|
|
|
+ resumeJob(job);
|
|
|
|
+ } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
|
|
|
|
+ pauseJob(job);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 立即运行任务
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void run(SysJob job) throws SchedulerException {
|
|
|
|
+ Long jobId = job.getJobId();
|
|
|
|
+ String jobGroup = job.getJobGroup();
|
|
|
|
+ SysJob properties = selectJobById(job.getJobId());
|
|
|
|
+ // 参数
|
|
|
|
+ JobDataMap dataMap = new JobDataMap();
|
|
|
|
+ dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
|
|
|
|
+ scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, jobGroup), dataMap);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增任务
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void insertJob(SysJob job) throws SchedulerException, TaskException {
|
|
|
|
+ job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
|
|
|
+ job.setCreateTime(LocalDateTime.now());
|
|
|
|
+ boolean save = this.save(job);
|
|
|
|
+ if (save) {
|
|
|
|
+ ScheduleUtils.createScheduleJob(scheduler, job);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新任务的时间表达式
|
|
|
|
+ *
|
|
|
|
+ * @param job 调度信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void updateJob(SysJob job) throws SchedulerException, TaskException {
|
|
|
|
+ SysJob properties = selectJobById(job.getJobId());
|
|
|
|
+ job.setUpdateTime(LocalDateTime.now());
|
|
|
|
+ boolean b = this.updateById(job);
|
|
|
|
+ if (b) {
|
|
|
|
+ updateSchedulerJob(job, properties.getJobGroup());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新任务
|
|
|
|
+ *
|
|
|
|
+ * @param job 任务对象
|
|
|
|
+ * @param jobGroup 任务组名
|
|
|
|
+ */
|
|
|
|
+ public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException {
|
|
|
|
+ Long jobId = job.getJobId();
|
|
|
|
+ // 判断是否存在
|
|
|
|
+ JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
|
|
|
|
+ if (scheduler.checkExists(jobKey)) {
|
|
|
|
+ // 防止创建时存在数据问题 先移除,然后在执行创建操作
|
|
|
|
+ scheduler.deleteJob(jobKey);
|
|
|
|
+ }
|
|
|
|
+ ScheduleUtils.createScheduleJob(scheduler, job);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 校验cron表达式是否有效
|
|
|
|
+ *
|
|
|
|
+ * @param cronExpression 表达式
|
|
|
|
+ * @return 结果
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public boolean checkCronExpressionIsValid(String cronExpression) {
|
|
|
|
+ return CronUtils.isValid(cronExpression);
|
|
|
|
+ }
|
|
|
|
+}
|