Quartz (2) - 動態操作

weixin_34253539發表於2018-06-21

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"));
    }
}

任務暫停重啟後,會把任務暫停期間的執行次數補回來,暫時未解決。

相關文章