com.github.rexsheng.springboot.faster.system.job.application.TriggerServiceImpl Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.job.application;
import com.github.rexsheng.springboot.faster.common.constant.CommonConstant;
import com.github.rexsheng.springboot.faster.system.job.application.dto.*;
import com.github.rexsheng.springboot.faster.system.job.domain.QuartzTool;
import com.github.rexsheng.springboot.faster.system.job.domain.SysJob;
import com.github.rexsheng.springboot.faster.system.job.domain.gateway.JobGateway;
import com.github.rexsheng.springboot.faster.system.job.domain.gateway.QueryTriggerDO;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import jakarta.annotation.Resource;
import org.quartz.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ConditionalOnClass(Scheduler.class)
@Lazy
public class TriggerServiceImpl implements TriggerService {
@Resource
@Lazy
private JobGateway jobGateway;
@Resource
@Lazy
private Scheduler scheduler;
@Override
public void addTrigger(AddTriggerRequest request) {
SysJob job=request.toDomain();
jobGateway.insertTrigger(job);
}
@Override
public void updateTrigger(UpdateTriggerRequest request) {
SysJob data=request.toDomain();
jobGateway.updateTriggerById(data);
}
@Override
@Transactional
public void deleteTrigger(DeleteTriggerRequest request) {
List triggerIds=new ArrayList<>();
if(!ObjectUtils.isEmpty(request.getJobIds())){
for(Long jobId:request.getJobIds()){
QueryTriggerDO query=new QueryTriggerDO();
query.setJobId(jobId);
List dataList=jobGateway.queryTriggers(query);
triggerIds.addAll(dataList.stream().map(a->a.getTriggerId()).collect(Collectors.toList()));
}
}
if(!ObjectUtils.isEmpty(request.getTriggerIds())){
triggerIds.addAll(request.getTriggerIds());
}
if(!ObjectUtils.isEmpty(triggerIds)){
if(Boolean.TRUE.equals(request.getStopSchedule())){
for(Long id:triggerIds){
stop(id);
}
}
jobGateway.deleteTriggers(SysJob.SysJobTrigger.of(triggerIds,true));
}
}
@Override
@Transactional
public void stopTriggerByJobIds(List jobIds) {
for(Long jobId:jobIds){
QueryTriggerDO query=new QueryTriggerDO();
query.setJobId(jobId);
List dataList=jobGateway.queryTriggers(query);
if(!ObjectUtils.isEmpty(dataList)){
for(SysJob.SysJobTrigger data:dataList){
stop(data.getTriggerId());
}
}
}
}
@Override
@Transactional
public void restartTriggersByJobId(Long jobId) {
QueryTriggerRequest request=new QueryTriggerRequest();
request.setJobId(jobId);
List triggers=queryTriggers(request);
for(TriggerDetailResponse trigger:triggers){
if(CommonConstant.JOB_STATE_RUNNING.equals(trigger.getStatus())){
restart(trigger.getId());
}
else if(CommonConstant.JOB_STATE_PAUSE.equals(trigger.getStatus())){
continue;
}
}
}
@Override
public TriggerDetailResponse getTrigger(Long id) {
SysJob.SysJobTrigger data= jobGateway.getTrigger(id);
Assert.notNull(data,"任务不存在");
return TriggerDetailResponse.of(data);
}
@Override
public List queryTriggers(QueryTriggerRequest request) {
QueryTriggerDO query=new QueryTriggerDO();
query.setJobId(request.getJobId());
query.setStatus(request.getStatus());
List dataList=jobGateway.queryTriggers(query);
return dataList.stream().map(TriggerDetailResponse::of).collect(Collectors.toList());
}
@Override
@Transactional
public void start(Long id) {
SysJob.SysJobTrigger trigger=jobGateway.getTrigger(id);
SysJob job=jobGateway.getJob(trigger.getJobId());
if(CommonConstant.STATUS_STOP.equals(job.getStatus())){
throw new RuntimeException("请先启用任务");
}
job.setTriggerList(Arrays.asList(trigger));
getQuartzTool().startTrigger(job);
jobGateway.updateTriggerStatus(id,CommonConstant.JOB_STATE_RUNNING);
}
@Override
@Transactional
public void stop(Long id) {
SysJob.SysJobTrigger trigger=jobGateway.getTrigger(id);
SysJob job=jobGateway.getJob(trigger.getJobId());
if(!CommonConstant.JOB_STATE_STOP.equals(trigger.getStatus()) && CommonConstant.STATUS_STOP.equals(job.getStatus())){
throw new RuntimeException("请先启用任务");
}
QuartzTool quartzTool=getQuartzTool();
quartzTool.stopTrigger(trigger);
quartzTool.deleteJobWithoutTriggers(job);
jobGateway.updateTriggerStatus(id,CommonConstant.JOB_STATE_STOP);
}
@Override
@Transactional
public void pause(Long id) {
SysJob.SysJobTrigger trigger=jobGateway.getTrigger(id);
getQuartzTool().pauseTrigger(trigger);
jobGateway.updateTriggerStatus(id,CommonConstant.JOB_STATE_PAUSE);
}
@Override
@Transactional
public void resume(Long id) {
SysJob.SysJobTrigger trigger=jobGateway.getTrigger(id);
getQuartzTool().resumeTrigger(trigger);
jobGateway.updateTriggerStatus(id,CommonConstant.JOB_STATE_RUNNING);
}
@Override
@Transactional
public void restart(Long id) {
stop(id);
start(id);
}
@Override
public List validateCronExpression(String cronExpression) {
if(ObjectUtils.isEmpty(cronExpression)){
throw new RuntimeException("表达式不能为空");
}
try {
CronExpression cron = new CronExpression(cronExpression);
List list=new ArrayList<>();
final int maxCount=3;
Date compareDate=DateUtil.toDate(DateUtil.currentDateTime());
for(int i=0;i
© 2015 - 2024 Weber Informatics LLC | Privacy Policy