Quartz (2) - 动态操作

2018-06-21  本文已影响60人  游园拾忆

code:

package quartz;

import javassist.expr.NewArray;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class jobScheduler {
    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    // 添加
    public static void addJob(String name, String groupName, Class<? extends Job> cls, String cron, Map<String ,Object> param) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            JobDetail jobDetail = new JobDetailImpl(name, groupName, cls);
            jobDetail.getJobDataMap().putAll(param); // 带参

            CronTrigger trigger = new CronTriggerImpl(name, groupName, name, groupName,cron);

            scheduler.scheduleJob(jobDetail, trigger);
            // 启动
            if (!scheduler.isShutdown()) {
                scheduler.start();
            }
        } catch (SchedulerException | ParseException e) {
            e.printStackTrace();
        }
    }

    // 查询状态
    public static String state(String name, String groupName) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            CronTrigger trigger = new CronTriggerImpl(name, groupName, name, groupName);
            Trigger.TriggerState state = scheduler.getTriggerState(trigger.getKey());
            return state.toString();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "";
    }

    // 修改执行时间
    public static void modifyTime(String name, String groupName, String cron) {
        // 方式1 移除 - 重新添加
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            JobDetail jobDetail = scheduler.getJobDetail(new JobKey(name, groupName));
            remove(name, groupName);
            addJob(name, groupName, jobDetail.getJobClass(), cron, jobDetail.getJobDataMap());
        } catch (SchedulerException  e) {
            e.printStackTrace();
        }

        // 方式2 修改 CronTrigger
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            CronTrigger cronTrigger = (CronTriggerImpl) scheduler.getTrigger(new TriggerKey(name, groupName));
            ((CronTriggerImpl) cronTrigger).setCronExpression(cron);
            scheduler.resumeTrigger(cronTrigger.getKey());
            scheduler.rescheduleJob(cronTrigger.getKey(), cronTrigger); // 不重新启动的话,更改不生效。
        } catch (SchedulerException | ParseException e) {
            e.printStackTrace();
        }

    }

    // 停止一个job任务
    public static void pauseJob(String name, String groupName){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.pauseJob(new JobKey(name, groupName));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    // 重启动
    public static void restart(String name, String groupName){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(new TriggerKey(name, groupName));
            scheduler.rescheduleJob(cronTrigger.getKey(), cronTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    // 移除
    public static void remove(String name, String groupName) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.pauseTrigger(new TriggerKey(name, groupName)); // 停止触发器
            scheduler.unscheduleJob(new TriggerKey(name, groupName)); // 移除触发器
            scheduler.deleteJob(new JobKey(name, groupName)); // 删除job
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    // 关闭所有
    public static void shutdownJobs() {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            if (!sched.isShutdown()) {
                sched.shutdown();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        Map<String ,Object> param = new HashMap<>();
        param.put("param", "paramA");
        param.put("id", 12);
        addJob("test1", "test1", warnJob.class, "0 30 09 22 06 ? 2018", param);
        addJob("test2", "test2", warnJob.class, "*/5 * * * * ?", param);

        try {
            String state_1 = state("test1", "test1");
            String state_2 = state("test2", "test2");
            String state_3 = state("test3", "test3");
            System.out.println("state: " + state_1 + " | " + state_2 + " | " + state_3);

            Thread.sleep(1000L * 5);
            System.out.println("modify : ");
            modifyTime("test2", "test2", "*/1 * * * * ?");

            Thread.sleep(1000L * 5);
            System.out.println("pause : ");
            pauseJob("test2", "test2");

            Thread.sleep(1000L * 5);
            System.out.println("restart : ");
            restart("test2", "test2" );

            Thread.sleep(1000L * 5);
            shutdownJobs();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

Job class:

package quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

public class warnJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("现在时间 :" + new Date() + " | " + jobExecutionContext.getJobDetail().getJobDataMap().get("param")
                + " | " + jobExecutionContext.getJobDetail().getJobDataMap().get("id"));
    }
}

任务暂停重启后,会把任务暂停期间的执行次数补回来,暂时未解决。

上一篇下一篇

猜你喜欢

热点阅读