feat(backend): Quartz框架初步实现

This commit is contained in:
junjie 2021-03-05 14:53:36 +08:00
parent 4c75b5b410
commit a60a243114
3 changed files with 67 additions and 28 deletions

View File

@ -1,16 +1,22 @@
package io.dataease.listener; package io.dataease.listener;
import io.dataease.base.domain.DatasetTableTask;
import io.dataease.job.sechedule.ScheduleManager; 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.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
@Component @Component
public class AppStartListener implements ApplicationListener<ApplicationReadyEvent> { public class AppStartListener implements ApplicationListener<ApplicationReadyEvent> {
@Resource @Resource
private ScheduleManager scheduleManager; private ScheduleService scheduleService;
@Resource
private DataSetTableTaskService dataSetTableTaskService;
@Override @Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
@ -21,6 +27,14 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
// long timestamp = System.currentTimeMillis() + 90 * 1000; // long timestamp = System.currentTimeMillis() + 90 * 1000;
// Date date = new Date(timestamp); // Date date = new Date(timestamp);
// scheduleManager.addSingleJob(new JobKey("abc", "def"), new TriggerKey("abc", "def"), TestJob.class, date); // scheduleManager.addSingleJob(new JobKey("abc", "def"), new TriggerKey("abc", "def"), TestJob.class, date);
// TODO 项目启动从数据库读取任务加入到Quartz // 项目启动从数据库读取任务加入到Quartz
List<DatasetTableTask> list = dataSetTableTaskService.list(new DatasetTableTask());
for (DatasetTableTask task : list) {
try {
scheduleService.addSchedule(task);
} catch (Exception e) {
e.printStackTrace();
}
}
} }
} }

View File

@ -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()));
}
}

View File

@ -3,15 +3,11 @@ package io.dataease.service.dataset;
import io.dataease.base.domain.DatasetTableTask; import io.dataease.base.domain.DatasetTableTask;
import io.dataease.base.domain.DatasetTableTaskExample; import io.dataease.base.domain.DatasetTableTaskExample;
import io.dataease.base.mapper.DatasetTableTaskMapper; import io.dataease.base.mapper.DatasetTableTaskMapper;
import io.dataease.job.sechedule.ScheduleManager; import io.dataease.service.ScheduleService;
import io.dataease.job.sechedule.TestJob;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -25,7 +21,7 @@ public class DataSetTableTaskService {
private DatasetTableTaskMapper datasetTableTaskMapper; private DatasetTableTaskMapper datasetTableTaskMapper;
@Resource @Resource
private ScheduleManager scheduleManager; private ScheduleService scheduleService;
public DatasetTableTask save(DatasetTableTask datasetTableTask) throws Exception { public DatasetTableTask save(DatasetTableTask datasetTableTask) throws Exception {
if (StringUtils.isEmpty(datasetTableTask.getId())) { if (StringUtils.isEmpty(datasetTableTask.getId())) {
@ -35,32 +31,14 @@ public class DataSetTableTaskService {
} else { } else {
datasetTableTaskMapper.updateByPrimaryKey(datasetTableTask); datasetTableTaskMapper.updateByPrimaryKey(datasetTableTask);
} }
scheduleService.addSchedule(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);
}
return datasetTableTask; return datasetTableTask;
} }
public void delete(String id) { public void delete(String id) {
DatasetTableTask datasetTableTask = datasetTableTaskMapper.selectByPrimaryKey(id); DatasetTableTask datasetTableTask = datasetTableTaskMapper.selectByPrimaryKey(id);
datasetTableTaskMapper.deleteByPrimaryKey(id); datasetTableTaskMapper.deleteByPrimaryKey(id);
scheduleManager.removeJob(new JobKey(datasetTableTask.getId(), datasetTableTask.getTableId()), new TriggerKey(datasetTableTask.getId(), datasetTableTask.getTableId())); scheduleService.deleteSchedule(datasetTableTask);
} }
public List<DatasetTableTask> list(DatasetTableTask datasetTableTask) { public List<DatasetTableTask> list(DatasetTableTask datasetTableTask) {