如何检查Quartz作业是否正在运行?

时间:2017-02-28 09:23:29

标签: spring quartz-scheduler spring-scheduled

我有一个UI,我在其上显示了作业列表。当我按下作业启动按钮并且每个作业在特定间隔内运行时,每个作业都有“立即运行”按钮。 现在我想停止特定的工作,但

scheduler.getCurrentlyExecutingJobs();

什么都不返回,我如何获得现有工作的参考,以便我能够阻止它。 我的工作班

public class SecondJob implements InterruptableJob{

@Override
public void execute(JobExecutionContext jobContext) throws JobExecutionException {
    System.out.println("My second JOB ");
    try {
        System.out.println(" job starts ");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void interrupt() throws UnableToInterruptJobException {
    System.out.println(" JOB STOPS");

}

}

我的控制器下面的方法运行作业

@RequestMapping(value = { "/runNow" }, method = RequestMethod.POST)
public ResponseEntity<?> runNow(@RequestParam("jobFileName") String jobFileName,
        @RequestParam("jobLocation") String jobLocation) {
    JobDetail job = JobBuilder.newJob(SecondJob.class).withIdentity(jobFileName, jobFileName).build();

    System.out.println(" runNow jobFileName   " + jobFileName);
    System.out.println("  runNow jobLocation   " + jobLocation);

    JobKey jobKey = new JobKey(jobFileName, jobFileName);
    try {

        if (scheduler == null) {
            StdSchedulerFactory stdSchedulerFactory = (StdSchedulerFactory) servletContext
                    .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);
            scheduler = stdSchedulerFactory.getScheduler();
        }
        scheduler.getContext().put("jobLocation", jobLocation);
        scheduler.addJob(job, true);
        scheduler.triggerJob(jobKey);
        //schedulerFactory.getScheduler().triggerJob(jobKey);

        List<JobExecutionContext> runningJobs = scheduler.getCurrentlyExecutingJobs();
    } catch (SchedulerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new ResponseEntity<>(HttpStatus.OK);
}
单击停止按钮

时,方法调用下面的

@RequestMapping(value = { "/stopJob" }, method = RequestMethod.POST)
public ResponseEntity<?> stopJob(@RequestParam("jobFileName") String jobFileName,
        @RequestParam("jobLocation") String jobLocation) {
    //JobDetail job = JobBuilder.newJob(SecondJob.class).withIdentity(jobFileName, jobFileName).build();

    System.out.println(" stopJob jobFileName   " + jobFileName);
    System.out.println("  stopJob jobLocation   " + jobLocation);

    JobKey jobKey = new JobKey(jobFileName, jobFileName);
    try {

        if (scheduler == null) {
            StdSchedulerFactory stdSchedulerFactory = (StdSchedulerFactory) servletContext
                    .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);
            scheduler = stdSchedulerFactory.getScheduler();
        }
        List<JobExecutionContext> runningJobs = scheduler.getCurrentlyExecutingJobs();

        scheduler.getContext().put("jobLocation", jobLocation);
        //scheduler.addJob(job, true);
        scheduler.interrupt(jobKey);
        //scheduler.triggerJob(jobKey);

    } catch (SchedulerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new ResponseEntity<>(HttpStatus.OK);
}

请告诉我需要解决的问题?

0 个答案:

没有答案