定时任务支持并发控制

This commit is contained in:
RuoYi
2019-03-13 19:45:12 +08:00
parent 9fc42511c6
commit e34b4ea63d
24 changed files with 430 additions and 300 deletions

View File

@ -1,6 +1,8 @@
package com.ruoyi.quartz.service;
import java.util.List;
import org.quartz.SchedulerException;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
/**
@ -32,7 +34,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int pauseJob(SysJob job);
public int pauseJob(SysJob job) throws SchedulerException;
/**
* 恢复任务
@ -40,7 +42,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int resumeJob(SysJob job);
public int resumeJob(SysJob job) throws SchedulerException;
/**
* 删除任务后所对应的trigger也将被删除
@ -48,7 +50,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int deleteJob(SysJob job);
public int deleteJob(SysJob job) throws SchedulerException;
/**
* 批量删除调度信息
@ -56,7 +58,7 @@ public interface ISysJobService
* @param ids 需要删除的数据ID
* @return 结果
*/
public void deleteJobByIds(String ids);
public void deleteJobByIds(String ids) throws SchedulerException;
/**
* 任务调度状态修改
@ -64,7 +66,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int changeStatus(SysJob job);
public int changeStatus(SysJob job) throws SchedulerException;
/**
* 立即运行任务
@ -72,7 +74,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int run(SysJob job);
public void run(SysJob job) throws SchedulerException;
/**
* 新增任务表达式
@ -80,7 +82,7 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int insertJobCron(SysJob job);
public int insertJobCron(SysJob job) throws SchedulerException, TaskException;
/**
* 更新任务的时间表达式
@ -88,8 +90,8 @@ public interface ISysJobService
* @param job 调度信息
* @return 结果
*/
public int updateJobCron(SysJob job);
public int updateJobCron(SysJob job) throws SchedulerException, TaskException;
/**
* 校验cron表达式是否有效
*

View File

@ -4,11 +4,13 @@ import java.util.List;
import javax.annotation.PostConstruct;
import org.quartz.CronTrigger;
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 com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.mapper.SysJobMapper;
import com.ruoyi.quartz.service.ISysJobService;
@ -33,7 +35,7 @@ public class SysJobServiceImpl implements ISysJobService
* 项目启动时,初始化定时器
*/
@PostConstruct
public void init()
public void init() throws SchedulerException, TaskException
{
List<SysJob> jobList = jobMapper.selectJobAll();
for (SysJob job : jobList)
@ -82,7 +84,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int pauseJob(SysJob job)
public int pauseJob(SysJob job) throws SchedulerException
{
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
int rows = jobMapper.updateJob(job);
@ -100,7 +102,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int resumeJob(SysJob job)
public int resumeJob(SysJob job) throws SchedulerException
{
job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
int rows = jobMapper.updateJob(job);
@ -118,7 +120,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int deleteJob(SysJob job)
public int deleteJob(SysJob job) throws SchedulerException
{
int rows = jobMapper.deleteJobById(job.getJobId());
if (rows > 0)
@ -136,7 +138,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public void deleteJobByIds(String ids)
public void deleteJobByIds(String ids) throws SchedulerException
{
Long[] jobIds = Convert.toLongArray(ids);
for (Long jobId : jobIds)
@ -153,7 +155,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int changeStatus(SysJob job)
public int changeStatus(SysJob job) throws SchedulerException
{
int rows = 0;
String status = job.getStatus();
@ -175,9 +177,9 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int run(SysJob job)
public void run(SysJob job) throws SchedulerException
{
return ScheduleUtils.run(scheduler, selectJobById(job.getJobId()));
ScheduleUtils.run(scheduler, selectJobById(job.getJobId()));
}
/**
@ -187,7 +189,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int insertJobCron(SysJob job)
public int insertJobCron(SysJob job) throws SchedulerException, TaskException
{
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
int rows = jobMapper.insertJob(job);
@ -205,7 +207,7 @@ public class SysJobServiceImpl implements ISysJobService
*/
@Override
@Transactional
public int updateJobCron(SysJob job)
public int updateJobCron(SysJob job) throws SchedulerException, TaskException
{
int rows = jobMapper.updateJob(job);
if (rows > 0)
@ -214,7 +216,7 @@ public class SysJobServiceImpl implements ISysJobService
}
return rows;
}
/**
* 校验cron表达式是否有效
*
@ -225,5 +227,5 @@ public class SysJobServiceImpl implements ISysJobService
public boolean checkCronExpressionIsValid(String cronExpression)
{
return CronUtils.isValid(cronExpression);
}
}
}