From a60a2431142bd435b6b2084ea2faf4bc1d479e4c Mon Sep 17 00:00:00 2001 From: junjie Date: Fri, 5 Mar 2021 14:53:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20Quartz=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataease/listener/AppStartListener.java | 18 ++++++- .../io/dataease/service/ScheduleService.java | 47 +++++++++++++++++++ .../dataset/DataSetTableTaskService.java | 30 ++---------- 3 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 backend/src/main/java/io/dataease/service/ScheduleService.java diff --git a/backend/src/main/java/io/dataease/listener/AppStartListener.java b/backend/src/main/java/io/dataease/listener/AppStartListener.java index 55bfb2d865..11dfdfac91 100644 --- a/backend/src/main/java/io/dataease/listener/AppStartListener.java +++ b/backend/src/main/java/io/dataease/listener/AppStartListener.java @@ -1,16 +1,22 @@ package io.dataease.listener; +import io.dataease.base.domain.DatasetTableTask; import io.dataease.job.sechedule.ScheduleManager; +import io.dataease.service.ScheduleService; +import io.dataease.service.dataset.DataSetTableTaskService; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; @Component public class AppStartListener implements ApplicationListener { @Resource - private ScheduleManager scheduleManager; + private ScheduleService scheduleService; + @Resource + private DataSetTableTaskService dataSetTableTaskService; @Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { @@ -21,6 +27,14 @@ public class AppStartListener implements ApplicationListener list = dataSetTableTaskService.list(new DatasetTableTask()); + for (DatasetTableTask task : list) { + try { + scheduleService.addSchedule(task); + } catch (Exception e) { + e.printStackTrace(); + } + } } } diff --git a/backend/src/main/java/io/dataease/service/ScheduleService.java b/backend/src/main/java/io/dataease/service/ScheduleService.java new file mode 100644 index 0000000000..79739fa41e --- /dev/null +++ b/backend/src/main/java/io/dataease/service/ScheduleService.java @@ -0,0 +1,47 @@ +package io.dataease.service; + +import io.dataease.base.domain.DatasetTableTask; +import io.dataease.job.sechedule.ScheduleManager; +import io.dataease.job.sechedule.TestJob; +import org.apache.commons.lang3.StringUtils; +import org.quartz.JobKey; +import org.quartz.TriggerKey; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; + +/** + * @Author gin + * @Date 2021/3/5 2:38 下午 + */ +@Service +public class ScheduleService { + @Resource + private ScheduleManager scheduleManager; + + public void addSchedule(DatasetTableTask datasetTableTask) throws Exception { + if (StringUtils.equalsIgnoreCase(datasetTableTask.getRate(), "0")) { + scheduleManager.addOrUpdateSingleJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), + new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId()), + TestJob.class,//TODO + new Date(datasetTableTask.getStartTime())); + } else if (StringUtils.equalsIgnoreCase(datasetTableTask.getRate(), "1")) { + Date endTime; + if (datasetTableTask.getEndTime() == null || datasetTableTask.getEndTime() == 0) { + endTime = null; + } else { + endTime = new Date(datasetTableTask.getEndTime()); + } + + scheduleManager.addOrUpdateCronJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), + new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId()), + TestJob.class,// TODO + datasetTableTask.getCron(), new Date(datasetTableTask.getStartTime()), endTime); + } + } + + public void deleteSchedule(DatasetTableTask datasetTableTask) { + scheduleManager.removeJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId())); + } +} diff --git a/backend/src/main/java/io/dataease/service/dataset/DataSetTableTaskService.java b/backend/src/main/java/io/dataease/service/dataset/DataSetTableTaskService.java index 2d513f6fe4..62076a1137 100644 --- a/backend/src/main/java/io/dataease/service/dataset/DataSetTableTaskService.java +++ b/backend/src/main/java/io/dataease/service/dataset/DataSetTableTaskService.java @@ -3,15 +3,11 @@ package io.dataease.service.dataset; import io.dataease.base.domain.DatasetTableTask; import io.dataease.base.domain.DatasetTableTaskExample; import io.dataease.base.mapper.DatasetTableTaskMapper; -import io.dataease.job.sechedule.ScheduleManager; -import io.dataease.job.sechedule.TestJob; +import io.dataease.service.ScheduleService; import org.apache.commons.lang3.StringUtils; -import org.quartz.JobKey; -import org.quartz.TriggerKey; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.Date; import java.util.List; import java.util.UUID; @@ -25,7 +21,7 @@ public class DataSetTableTaskService { private DatasetTableTaskMapper datasetTableTaskMapper; @Resource - private ScheduleManager scheduleManager; + private ScheduleService scheduleService; public DatasetTableTask save(DatasetTableTask datasetTableTask) throws Exception { if (StringUtils.isEmpty(datasetTableTask.getId())) { @@ -35,32 +31,14 @@ public class DataSetTableTaskService { } else { datasetTableTaskMapper.updateByPrimaryKey(datasetTableTask); } - - if (StringUtils.equalsIgnoreCase(datasetTableTask.getRate(), "0")) { - scheduleManager.addOrUpdateSingleJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), - new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId()), - TestJob.class, - new Date(datasetTableTask.getStartTime())); - } else if (StringUtils.equalsIgnoreCase(datasetTableTask.getRate(), "1")) { - Date endTime; - if (datasetTableTask.getEndTime() == null || datasetTableTask.getEndTime() == 0) { - endTime = null; - } else { - endTime = new Date(datasetTableTask.getEndTime()); - } - - scheduleManager.addOrUpdateCronJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), - new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId()), - TestJob.class, - datasetTableTask.getCron(), new Date(datasetTableTask.getStartTime()), endTime); - } + scheduleService.addSchedule(datasetTableTask); return datasetTableTask; } public void delete(String id) { DatasetTableTask datasetTableTask = datasetTableTaskMapper.selectByPrimaryKey(id); datasetTableTaskMapper.deleteByPrimaryKey(id); - scheduleManager.removeJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId())); + scheduleService.deleteSchedule(datasetTableTask); } public List list(DatasetTableTask datasetTableTask) {