package com.ots.project.monitor.job.service;
|
import com.ots.common.constant.ScheduleConstants;
|
import com.ots.common.exception.job.TaskException;
|
import com.ots.common.utils.text.Convert;
|
import com.ots.project.monitor.job.domain.Job;
|
import com.ots.project.monitor.job.mapper.JobMapper;
|
import com.ots.project.monitor.job.util.CronUtils;
|
import com.ots.project.monitor.job.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.util.List;
|
|
@Service
|
public class JobServiceImpl implements IJobService {
|
@Autowired
|
private Scheduler scheduler;
|
@Autowired
|
private JobMapper jobMapper;
|
|
@PostConstruct
|
public void init() throws SchedulerException, TaskException {
|
List<Job> jobList = jobMapper.selectJobAll();
|
for (Job job : jobList) {
|
updateSchedulerJob(job, job.getJobGroup());
|
}
|
}
|
|
@Override
|
public List<Job> selectJobList(Job job) {
|
return jobMapper.selectJobList(job);
|
}
|
|
@Override
|
public Job selectJobById(Long jobId) {
|
return jobMapper.selectJobById(jobId);
|
}
|
|
@Override
|
@Transactional
|
public int pauseJob(Job job) throws SchedulerException {
|
Long jobId = job.getJobId();
|
String jobGroup = job.getJobGroup();
|
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
int rows = jobMapper.updateJob(job);
|
if (rows > 0) {
|
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
}
|
return rows;
|
}
|
|
@Override
|
@Transactional
|
public int resumeJob(Job job) throws SchedulerException {
|
Long jobId = job.getJobId();
|
String jobGroup = job.getJobGroup();
|
job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
|
int rows = jobMapper.updateJob(job);
|
if (rows > 0) {
|
scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
}
|
return rows;
|
}
|
|
@Override
|
@Transactional
|
public int deleteJob(Job job) throws SchedulerException {
|
Long jobId = job.getJobId();
|
String jobGroup = job.getJobGroup();
|
int rows = jobMapper.deleteJobById(jobId);
|
if (rows > 0) {
|
scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
|
}
|
return rows;
|
}
|
|
@Override
|
@Transactional
|
public void deleteJobByIds(String ids) throws SchedulerException {
|
Long[] jobIds = Convert.toLongArray(ids);
|
for (Long jobId : jobIds) {
|
Job job = jobMapper.selectJobById(jobId);
|
deleteJob(job);
|
}
|
}
|
|
@Override
|
@Transactional
|
public int changeStatus(Job job) throws SchedulerException {
|
int rows = 0;
|
String status = job.getStatus();
|
if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
|
rows = resumeJob(job);
|
} else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
|
rows = pauseJob(job);
|
}
|
return rows;
|
}
|
|
@Override
|
@Transactional
|
public void run(Job job) throws SchedulerException {
|
Long jobId = job.getJobId();
|
String jobGroup = job.getJobGroup();
|
Job properties = selectJobById(job.getJobId());
|
|
JobDataMap dataMap = new JobDataMap();
|
dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
|
scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, jobGroup), dataMap);
|
}
|
|
@Override
|
@Transactional
|
public int insertJob(Job job) throws SchedulerException, TaskException {
|
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
|
int rows = jobMapper.insertJob(job);
|
if (rows > 0) {
|
ScheduleUtils.createScheduleJob(scheduler, job);
|
}
|
return rows;
|
}
|
|
@Override
|
@Transactional
|
public int updateJob(Job job) throws SchedulerException, TaskException {
|
Job properties = selectJobById(job.getJobId());
|
int rows = jobMapper.updateJob(job);
|
if (rows > 0) {
|
updateSchedulerJob(job, properties.getJobGroup());
|
}
|
return rows;
|
}
|
|
public void updateSchedulerJob(Job 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);
|
}
|
|
@Override
|
public boolean checkCronExpressionIsValid(String cronExpression) {
|
return CronUtils.isValid(cronExpression);
|
}
|
}
|