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

com.sinszm.sofa.service.impl.IQuartzServiceImpl Maven / Gradle / Ivy

Go to download

高可用服务框架,分布式任务调度服务组件 Copyright © 2021 智慧程序猿(sinsz.com) All rights reserved.

The newest version!
package com.sinszm.sofa.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sinszm.sofa.exception.ApiException;
import com.sinszm.sofa.service.AbstractQuartzMethod;
import com.sinszm.sofa.service.IQuartzService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * quartz服务实现
 *
 * @author admin
 */
@Deprecated
@Slf4j
@ConditionalOnProperty(prefix = "quartz", name = "xxl", havingValue = "false", matchIfMissing = true)
@Service
public class IQuartzServiceImpl extends AbstractQuartzMethod implements IQuartzService {

    private final SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    public IQuartzServiceImpl(SchedulerFactoryBean schedulerFactoryBean) {
        this.schedulerFactoryBean = schedulerFactoryBean;
    }

    /**
     * 调度器
     *
     * @return {Scheduler}
     */
    @Override
    public Scheduler scheduler() {
        return schedulerFactoryBean.getScheduler();
    }

    @SneakyThrows
    @Override
    public Date addJob(String name, String group, Class clazz, String cronExpression, Map jobValue) {
        if (checkExists(name, group)) {
            throw new ApiException("202", "任务已存在,请先删除重试");
        }
        //调度器
        Scheduler scheduler = scheduler();
        //构造任务
        JobDetail jobDetail = newJob(clazz)
                .withIdentity(name, group)
                .storeDurably(true)
                .build();
        //构造任务触发器
        Trigger trigger = newTrigger()
                .withIdentity(name, group)
                .withSchedule(
                        cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing()
                ).build();
        Date nextDate = nextDate(scheduler,jobDetail, trigger, clazz, jobValue,false, false);
        log.info("新增作业=> [作业名称:" + name + " 作业组:" + group + "] ");
        return nextDate;
    }

    @SneakyThrows
    @Override
    public Date addJob(String name, String group, Class clazz, Trigger trigger, Map jobValue) {
        if (checkExists(name, group)) {
            throw new ApiException("202", "任务已存在,请先删除重试");
        }
        //调度器
        Scheduler scheduler = scheduler();
        //构造任务
        JobDetail jobDetail = newJob(clazz)
                .withIdentity(name, group)
                .storeDurably(true)
                .build();
        Date nextDate = nextDate(scheduler,jobDetail, trigger, clazz, jobValue,false, false);
        log.info("新增作业=> [作业名称:" + name + " 作业组:" + group + "] ");
        return nextDate;
    }

    @SneakyThrows
    @Override
    public void executeJob(String name, String group, Class clazz, Date startTime, Map jobValue) {
        if (checkExists(name, group)) {
            throw new ApiException("202", "任务已存在,请先删除重试");
        }
        //调度器
        Scheduler scheduler = scheduler();
        //构造任务
        JobDetail jobDetail = newJob(clazz)
                .withIdentity(name, group)
                .storeDurably(true)
                .build();
        //处理参数
        if (ObjectUtil.isNotEmpty(jobValue)) {
            for (Map.Entry entry : jobValue.entrySet()) {
                jobDetail.getJobDataMap().put(entry.getKey(), entry.getValue());
            }
        }
        //触发器
        Trigger trigger = newTrigger()
                .withIdentity(name, group)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow()
                )
                .startAt(startTime == null || startTime.before(new Date()) ? new Date() : startTime)
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

    @SneakyThrows
    @Override
    public void removeJob(String name, String group) {
        //调度器
        Scheduler scheduler = scheduler();
        //触发器
        TriggerKey tk = TriggerKey.triggerKey(name, group);
        //停止触发器
        scheduler.pauseTrigger(tk);
        //移除触发器
        scheduler.unscheduleJob(tk);
        //作业
        JobKey jobKey = JobKey.jobKey(name, group);
        //删除作业
        scheduler.deleteJob(jobKey);
        log.info("删除作业=> [作业名称:" + name + " 作业组:" + group + "] ");
    }

    @SneakyThrows
    @Override
    public void pauseJob(String name, String group) {
        //调度器
        Scheduler scheduler = scheduler();
        //作业
        JobKey jobKey = JobKey.jobKey(name, group);
        //暂停作业
        scheduler.pauseJob(jobKey);
        log.info("暂停作业=> [作业名称:" + name + " 作业组:" + group + "] ");
    }

    @SneakyThrows
    @Override
    public void resumeJob(String name, String group) {
        //调度器
        Scheduler scheduler = scheduler();
        //作业
        JobKey jobKey = JobKey.jobKey(name, group);
        //恢复作业
        scheduler.resumeJob(jobKey);
        log.info("恢复作业=> [作业名称:" + name + " 作业组:" + group + "] ");
    }

    @SneakyThrows
    @Override
    public Date modifyTime(String name, String group, String cronExpression) {
        //调度器
        Scheduler scheduler = scheduler();
        //触发器
        TriggerKey tk = TriggerKey.triggerKey(name, group);
        //构造任务触发器
        Trigger trg = newTrigger().withIdentity(name, group)
                .forJob(name, group).withSchedule(cronSchedule(cronExpression)
                        .withMisfireHandlingInstructionDoNothing()).build();
        Date nextFireTime = scheduler.rescheduleJob(tk, trg);
        log.info("修改作业触发时间=> [作业名称:" + name + " 作业组:" + group + "] ");
        return nextFireTime;
    }

    @SneakyThrows
    @Override
    public Date modifyTrigger(String name, String group, Trigger trigger) {
        //调度器
        Scheduler scheduler = scheduler();
        //触发器
        TriggerKey tk = TriggerKey.triggerKey(name, group);
        Date nextFireTime = scheduler.rescheduleJob(tk, trigger);
        log.info("修改作业触发时间=> [作业名称:" + name + " 作业组:" + group + "] ");
        return nextFireTime;
    }

    @SneakyThrows
    @Override
    public boolean checkExists(String name, String group) {
        Scheduler scheduler = scheduler();
        JobKey jobKey = JobKey.jobKey(name, group);
        return scheduler.checkExists(jobKey);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy