How to interrupt or stop currently running quartz job?

前端 未结 5 1311
时光取名叫无心
时光取名叫无心 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条回答
  •  爱一瞬间的悲伤
    2020-12-10 02:29

    In Quartz 2.1 with Spring you can:

    @Autowired
    private Scheduler schedulerFactoryBean; //injected by spring
    ...
    ...
    
    List currentlyExecuting = schedulerFactoryBean.getCurrentlyExecutingJobs();
    
    //verifying if job is running       
    for (JobExecutionContext jobExecutionContext : currentlyExecuting) {
        if(jobExecutionContext.getJobDetail().getKey().getName().equals("JobKeyNameToInterrupt")){
            result = schedulerFactoryBean.interrupt(jobExecutionContext.getJobDetail().getKey());
        }
    }
    

提交回复
热议问题