当前位置: 首页 > 工具软件 > quartz web > 使用案例 >

quartz 集群化任务。 web增删改查任务 并且暂停继续任务。

令狐宣
2023-12-01

一、Quartz集群化使用

二、web增删改查任务 并且暂停启动任务。

这里写一个增删改查任务、任务启停的简单的类,实际应用中1、记得持久化任务。2、持久化jobParam类。这里只是展示怎样增删改查和启停

package com.back.scheduler;


import com.back.scheduler.job.BackJob;
import com.back.scheduler.manager.JobParam;
import com.google.common.collect.Lists;
import com.weibo.api.motan.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.CronScheduleBuilder.*;

@Slf4j
public class SchedulerManager {

    public final static String DEFAULT_GROUP = "DODP";
    public static Scheduler defaultScheduler = initScheduler();
    private static Scheduler initScheduler() {
        try {
            SchedulerFactory factory =  new StdSchedulerFactory();
            Scheduler scheduler = factory.getScheduler();
            scheduler.start();
            return scheduler;
        } catch (SchedulerException e) {
            log.error("Scheduler Error!");
            throw new RuntimeException("Scheduler Error!");
        }
    }

    /**
     * 新增任务
     * @param param
     */
    public void addJob(JobParam param){
        if(param.getJobtype() == JobParam.JobType.cron){
            Class<? extends Job> aClass = null;
            try {
                aClass  = (Class<? extends Job>) Class.forName(param.getJobClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            JobDetail job = newJob(aClass).withIdentity(param.getJobKey(), DEFAULT_GROUP).build();
            if(param.getParams().size() > 0){
                JobDataMap jobDataMap = job.getJobDataMap();
                param.getParams().forEach((k,v) -> {
                    jobDataMap.putIfAbsent(k,v);
                });
            }
            CronTrigger build = newTrigger().withIdentity(param.getTriggerKey(), DEFAULT_GROUP)
                    .withSchedule(cronSchedule(param.getCron())).build();
            try {
                defaultScheduler.scheduleJob(job, build);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除任务
     * @param jobKey
     * @param groupName
     */
    public void deleteJob(String jobKey,String groupName){
        JobKey jobKey1 = new JobKey(jobKey, groupName);
        try {
            defaultScheduler.deleteJob(jobKey1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改任务
     * @param jobKey
     * @param groupName
     * @param corn
     */
    public void updateJobCorn(String jobKey,String groupName,String corn){
        JobKey jobKey1 = new JobKey(jobKey, groupName);
        try {
            JobDetail jobDetail = defaultScheduler.getJobDetail(jobKey1);
            CronTrigger build = newTrigger().withSchedule(cronSchedule(corn)).build();
            defaultScheduler.deleteJob(jobKey1);
            defaultScheduler.scheduleJob(jobDetail,build);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有任务
     * @return
     */
    public List<JobParam> selectAllJob(){
        try {
            List<JobParam> result = Lists.newArrayList();
            GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
            Set<JobKey> jobKeys = defaultScheduler.getJobKeys(matcher);
            for (JobKey key : jobKeys){
                List<? extends Trigger> triggersOfJob = defaultScheduler.getTriggersOfJob(key);
                for (Trigger trigger : triggersOfJob){
                    JobParam jobParam = new JobParam();
                    jobParam.setJobKey(key.getName());
                    jobParam.setTriggerKey(trigger.getKey().getName());
                    if(trigger instanceof CronTrigger){
                        jobParam.setJobtype(JobParam.JobType.cron);
                    }
                    JobDetail jobDetail = defaultScheduler.getJobDetail(key);
                    jobParam.setJobClassName(jobDetail.getJobClass().getName());
                    jobParam.setParams(jobDetail.getJobDataMap());
                    result.add(jobParam);
                }
            }
            return result;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 暂停任务
     * @param jobKey
     * @param groupName
     */
    public void stopJob(String jobKey,String groupName){
        JobKey jobKey1 = new JobKey(jobKey, groupName);
        try {
            defaultScheduler.pauseJob(jobKey1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 任务启动
     * @param jobKey
     * @param groupName
     */
    public void resumeJob(String jobKey,String groupName){
        JobKey jobKey1 = new JobKey(jobKey, groupName);
        try {
            defaultScheduler.resumeJob(jobKey1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws InterruptedException {

        SchedulerManager schedulerManager = new SchedulerManager();
        JobParam jobParam = new JobParam();
        jobParam.setId(1L);
        jobParam.setCron("0/2 * * * * ?");
        jobParam.setJobKey("job1");
        jobParam.setTriggerKey("trigger1");
        jobParam.setJobtype(JobParam.JobType.cron);
        jobParam.setJobClassName(BackJob.class.getName());
        jobParam.getParams().put(BackJob.BACK_JOB_KEY,new Date());
        schedulerManager.addJob(jobParam);
        JobParam jobParam2 = new JobParam();
        jobParam2.setId(2L);
        jobParam2.setCron("0/3 * * * * ?");
        jobParam2.setJobKey("job2");
        jobParam2.setTriggerKey("trigger2");
        jobParam2.setJobtype(JobParam.JobType.cron);
        jobParam2.setJobClassName(BackJob.class.getName());
        jobParam2.getParams().put(BackJob.BACK_JOB_KEY,new Date());
        schedulerManager.addJob(jobParam2);
        TimeUnit.SECONDS.sleep(5);
        schedulerManager.deleteJob("job1",DEFAULT_GROUP);

        TimeUnit.SECONDS.sleep(5);
        schedulerManager.updateJobCorn("job2",DEFAULT_GROUP,"0/5 * * * * ?");
        TimeUnit.SECONDS.sleep(5);
        List<JobParam> jobDetails = schedulerManager.selectAllJob();
        for (JobParam job : jobDetails){
            System.out.println(job);
        }
        schedulerManager.stopJob("job2",DEFAULT_GROUP);
        TimeUnit.SECONDS.sleep(10);
        schedulerManager.resumeJob("job2",DEFAULT_GROUP);


    }
}

 

 类似资料: