How to interrupt or stop currently running quartz job?

前端 未结 5 1328
时光取名叫无心
时光取名叫无心 2020-12-10 01:42

I have some tasks that are executed with the help of Java Quartz Jobs, but I need to stop some tasks by some condition in my code. I read that this can be done via Interrupt

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 02:28

    The best solution in my opinion is the one described in this thread: http://forums.terracotta.org/forums/posts/list/7700.page

    I've just introduced a "sleep" after set stop flag to true to allow the job to finish cleanly.

        @Override
    public void interrupt() throws UnableToInterruptJobException {
        stopFlag.set(true);
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            //logger.error("interrupt()", e);
        }
        Thread thread = runningThread.getAndSet(null);
        if (thread != null)
            thread.interrupt();
    }
    

提交回复
热议问题