All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.job.application.JobServiceImpl 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.common.domain.PagedList;
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.QueryJobDO;
import com.github.rexsheng.springboot.faster.system.job.domain.gateway.QueryJobLogDO;
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 java.util.List;
import java.util.stream.Collectors;

@Service
@ConditionalOnClass(Scheduler.class)
@Lazy
public class JobServiceImpl implements JobService {

    @Resource
    private JobGateway jobGateway;

    @Resource
    private TriggerService triggerService;

    @Resource
    private Scheduler scheduler;

    @Override
    public PagedList paginateJob(QueryJobRequest request) {
        QueryJobDO query=new QueryJobDO();
        query.setName(request.getName());
        query.setStatus(request.getStatus());
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        PagedList dataList=jobGateway.paginateJobs(query);
        return PagedList.of(dataList, JobDetailResponse::of);
    }

    @Override
    public List queryJob(QueryJobRequest request) {
        QueryJobDO query=new QueryJobDO();
        query.setName(request.getName());
        query.setStatus(request.getStatus());
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        List dataList=jobGateway.queryJobs(query);
        return dataList.stream().map(JobDetailResponse::of).collect(Collectors.toList());
    }

    @Override
    public JobDetailResponse getJob(Long id) {
        SysJob data= jobGateway.getJob(id);
        Assert.notNull(data,"任务不存在");
        return JobDetailResponse.of(data);
    }

    @Override
    public void addJob(AddJobRequest request) {
        SysJob domain = request.toDomain();
        jobGateway.insertJob(domain);
    }

    @Override
    public void update(UpdateJobRequest request) {
        SysJob data=request.toDomain();
        jobGateway.updateJobById(data);
    }

    @Override
    @Transactional
    public void updateStatus(List request) {
        List dataList=request.stream().map(UpdateJobRequest::toDomain).collect(Collectors.toList());
        if(CommonConstant.STATUS_STOP.equals(dataList.get(0).getStatus())){
            triggerService.stopTriggerByJobIds(dataList.stream().map(data->data.getJobId()).collect(Collectors.toList()));
            QuartzTool quartzTool=getQuartzTool();
            quartzTool.deleteJobs(dataList);
        }
        jobGateway.updateJobStatus(dataList);
    }

    @Override
    @Transactional
    public void delete(List ids) {
        List dataList=SysJob.of(ids,true);
        triggerService.deleteTrigger(DeleteTriggerRequest.ofJobs(ids,false));
        getQuartzTool().deleteJobs(dataList);
        jobGateway.deleteJobs(dataList);
    }

    @Override
    public PagedList paginateJobLog(QueryJobLogRequest request) {
        QueryJobLogDO query=new QueryJobLogDO();
        query.setJobId(request.getJobId());
        query.setTriggerId(request.getTriggerId());
        query.setState(request.getState());
        query.setStartTime(DateUtil.parseDateTime(request.getStartTime(),DateUtil.DATE_TIME_FORMAT));
        query.setEndTime(DateUtil.parseDateTime(request.getEndTime(),DateUtil.DATE_TIME_FORMAT));
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        PagedList dataList=jobGateway.paginateJobLogs(query);
        return PagedList.of(dataList, JobLogResponse::of);
    }

    @Override
    public PagedList paginateJobLogDetail(QueryJobLogDetailRequest request) {
        QueryJobLogDO query=new QueryJobLogDO();
        query.setJobLogId(request.getJobLogId());
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        PagedList dataList=jobGateway.paginateJobLogDetails(query);
        return PagedList.of(dataList, JobLogDetailResponse::of);
    }

    @Override
    public void executeOnce(Long id) {
        SysJob job=jobGateway.getJob(id);
        if(CommonConstant.STATUS_STOP.equals(job.getStatus())){
            throw new RuntimeException("请先启用任务");
        }
        QuartzTool quartzTool=getQuartzTool();
        quartzTool.executeJobOnce(job);
    }

    private QuartzTool getQuartzTool(){
        return new QuartzTool(scheduler);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy