一般情况下不存在取消正在执行的定时任务的方法


取消正在执行的定时任务的主要需求分为两种:

1.负责执行的Java线程直接终止被杀死

2.定时任务本身的代码逻辑中加入了标志位判断是否结束。

实际业务中,大多数都是第一种,这是基本无法做到的。能取消正在执行的任务,一般都是第二种。

1.实际业务中的定时任务一般是这样:到了固定时间,执行某些SQL将查询的结果进行某些处理

2.我问问你:已经执行SQL的时候,你有办法通过java代码停止吗?没办法的,所以别想东想西了,我特么最无语的就是当初做这个需求的时候:项目经理要我实现取消定时任务时,如果任务已经在执行中了,让正在执行的任务停止。当时我就和他掰扯,这咋实现,不行的啊,顶多是继续让任务执行完某个节点准备执行下一个节点的时候进行判断是否要终止,他让我上网找个解决方法。……

3.网上所谓的可以停止的方法实现:就是加一个isInterrupted的标志位判断,但是实际上,如果你是通过服务调用,并且被调用方的代码无法更改,你实际上是没有办法去处理的。

4.服务调用另一个方法,该方法不允许修改,并且此方法执行需要半个小时。要求:执行到15分钟的时候,停止这个方法。结论:做不到。

Quartz的注释上面就这样说过:

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.quartz;

/**
 * The interface to be implemented by {@link Job}s that provide a 
 * mechanism for having their execution interrupted.  It is NOT a requirement
 * for jobs to implement this interface - in fact, for most people, none of
 * their jobs will.
 * 
 * 

Interrupting a Job is very analogous in concept and * challenge to normal interruption of a Thread in Java. * *

* The means of actually interrupting the Job must be implemented within the * Job itself (the interrupt() method of this * interface is simply a means for the scheduler to inform the Job * that a request has been made for it to be interrupted). The mechanism that * your jobs use to interrupt themselves might vary between implementations. * However the principle idea in any implementation should be to have the * body of the job's execute(..) periodically check some flag to * see if an interruption has been requested, and if the flag is set, somehow * abort the performance of the rest of the job's work. An example of * interrupting a job can be found in the java source for the class * org.quartz.examples.DumbInterruptableJob. It is legal to use * some combination of wait() and notify() * synchronization within interrupt() and execute(..) * in order to have the interrupt() method block until the * execute(..) signals that it has noticed the set flag. *

* *

* If the Job performs some form of blocking I/O or similar functions, you may * want to consider having the Job.execute(..) method store a * reference to the calling Thread as a member variable. Then the * Implementation of this interfaces interrupt() method can call * interrupt() on that Thread. Before attempting this, make * sure that you fully understand what java.lang.Thread.interrupt() * does and doesn't do. Also make sure that you clear the Job's member * reference to the Thread when the execute(..) method exits (preferably in a * finally block. *

* *

* See Example 7 (org.quartz.examples.example7.DumbInterruptableJob) for a simple * implementation demonstration. *

* @see Job * @see StatefulJob * @see Scheduler#interrupt(JobKey) * @see Scheduler#interrupt(String) * * @author James House */ public interface InterruptableJob extends Job { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** *

* Called by the {@link Scheduler} when a user * interrupts the Job. *

* *
@throws UnableToInterruptJobException * if there is an exception while interrupting the job. */ void interrupt() throws UnableToInterruptJobException; }

主要内容是说:中断作业在概念上非常类似于Java中线程的正常中断,并且是一种挑战。作业用于中断自身的机制可能因实现而异。然而,任何实现中的原则思想都应该是让作业的主体执行定期检查一些标志,查看是否请求了中断,如果设置了该标志,则以某种方式中止作业其余工作的执行。并且quartz给了一个expamle,就是通过判断标志位实现的。

https://github.com/quartz-scheduler/quartz/tree/master/distribution/examples/src/main/java/org/quartz/examples/example7