Quartz-中断正在执行的任务

转:

Quartz-中断正在执行的任务

版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/78545158

概述

由于业务需要,停止Quartz中正在执行的任务

  1. 任务类只需要实现InterruptableJob类,然后实现interrupt()方法。

  2. 在这个方法中进行标记的改变,在执行中进行这个标记判断,就可实现中断任务了

  3. 另外在调度器上调用方法:sched.interrupt(job.getKey())


示例

job类

package com.xgj.quartz.quartzItself.interruptableJob;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.UnableToInterruptJobException;

/**
 * 
 * 
 * @ClassName: DumbInterruptableJob
 * 
 * @Description: 个可执行的中断可执行程序,用于单元测试。
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年11月15日 上午9:26:36
 */

public class DumbInterruptableJob implements InterruptableJob {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private boolean _interrupted = false; // job 是否中断
    private JobKey _jobKey = null; // job name

    private static int counts = 0; // 中断执行次数

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        _jobKey = context.getJobDetail().getKey();

        System.out.println("【开始执行】任务Key:" + _jobKey + ",执行时间: "
                + sdf.format(new Date()));

        try {

            for (int i = 0; i < 4; i++) {
                try {
                    Thread.sleep(1000L);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 查看是否中断
                if (_interrupted) {
                    counts++;
                    System.out.println("被外界因素停止了这个任务key:" + _jobKey
                            + ",中断累计次数: " + counts + "
");
                    return; // 也可以选择抛出一个JobExecutionException,根据业务需要指定行为
                }
            }

        } finally {
            System.out.println("【完成任务】key:" + _jobKey + " 完成时间:"
                    + sdf.format(new Date()));
        }

    }

    @Override
    public void interrupt() throws UnableToInterruptJobException {
        System.out.println("
—————— 【中断】外界正在调用调度器停止这个任务key:" + _jobKey
                + " ————————");
        _interrupted = true;

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

调度类

package com.xgj.quartz.quartzItself.interruptableJob;

import static org.quartz.DateBuilder.nextGivenSecondDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 
 * 
 * @ClassName: InterruptExample
 * 
 * @Description: 调度类
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年11月15日 上午9:28:21
 */

public class InterruptExample {

    public void run() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("------- 初始化 ----------------------");

        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        // 下一个15秒
        Date startTime = nextGivenSecondDate(null, 15);

        // 当前时间15秒后,每间隔5秒执行一次任务
        JobDetail job = newJob(DumbInterruptableJob.class).withIdentity(
                "interruptableJob1", "group1").build();
        SimpleTrigger trigger = newTrigger()
                .withIdentity("trigger1", "group1")
                .startAt(startTime)
                .withSchedule(
                        simpleSchedule().withIntervalInSeconds(5)
                                .repeatForever()).build();

        Date ft = sched.scheduleJob(job, trigger);
        System.out.println(job.getKey() + " 将运行于:" + sdf.format(ft) + " 并重复:"
                + trigger.getRepeatCount() + " 次,间隔 "
                + trigger.getRepeatInterval() / 1000 + " 秒");

        // 调度开始执行
        sched.start();
        System.out.println("------- 开始调度 (调用.start()方法) ----------------");

        System.out.println("------- 每7秒钟启动一次中断任务(10次中断) ----------");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(7000L);

                // 手动中断调度器中的job
                sched.interrupt(job.getKey());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("------- 关闭调度 ---------------------");

        sched.shutdown(true);

        System.out.println("------- 关闭调度器完成 -----------------");
        SchedulerMetaData metaData = sched.getMetaData();

        System.out.println("~~~~~~~~~~  执行了 "
                + metaData.getNumberOfJobsExecuted() + " 个 jobs.");

    }

    public static void main(String[] args) throws Exception {
        InterruptExample example = new InterruptExample();
        example.run();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

运行结果

------- 初始化 ----------------------
INFO  StdSchedulerFactory - Using default implementation for ThreadExecutor
INFO  SimpleThreadPool - Job execution threads will use class loader of thread: main
INFO  SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
INFO  QuartzScheduler - Quartz Scheduler v.2.2.3 created.
INFO  RAMJobStore - RAMJobStore initialized.
INFO  QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

INFO  StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
INFO  StdSchedulerFactory - Quartz scheduler version: 2.2.3
group1.interruptableJob1 将运行于:2017-11-15 09:29:45 并重复:-1 次,间隔 5 秒
INFO  QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
------- 开始调度 (调用.start()方法) ----------------
------- 每7秒钟启动一次中断任务(10次中断) ----------
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:29:45

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 1

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:29:49
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:29:50
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:29:54
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:29:55

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 2

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:29:56
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:00

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 3

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:03
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:05
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:09
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:10
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:14
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:15

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 4

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:17
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:20

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 5

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:24
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:25
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:29
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:30

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 6

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:31
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:35

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 7

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:38
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:40
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:44
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:45
【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:49
【开始执行】任务Key:group1.interruptableJob1,执行时间: 2017-11-15 09:30:50

—————— 【中断】外界正在调用调度器停止这个任务key:group1.interruptableJob1 ————————
------- 关闭调度 ---------------------
INFO  QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
INFO  QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
被外界因素停止了这个任务key:group1.interruptableJob1,中断累计次数: 8

【完成任务】key:group1.interruptableJob1 完成时间:2017-11-15 09:30:52
INFO  QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
------- 关闭调度器完成 -----------------
~~~~~~~~~~  执行了 14 个 jobs.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

示例源码

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

原文地址:https://www.cnblogs.com/libin6505/p/10571523.html