检查作业是否正在运行?

时间:2014-07-04 06:54:07

标签: java cron quartz-scheduler job-scheduling

我想检查Quartz作业是否正在运行。我发现它可以使用scheduler.getCurrentlyExecutingJobs()来使用。 BuI与此混淆,我应该把它放在哪里才能得到结果? 感谢

try {

        // specify the job' s details..
        JobDetail job = JobBuilder.newJob(TestJob.class)
            .withIdentity("testJob")
            .build();

        // specify the running period of the job
        Trigger trigger = TriggerBuilder.newTrigger()
              .withSchedule(  
                    SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(10)
                    .repeatForever())  
                         .build();  

        //schedule the job
        SchedulerFactory schFactory = new StdSchedulerFactory();
        Scheduler sch = schFactory.getScheduler();
        sch.start();            
        sch.scheduleJob(job, trigger);

        System.out.println("******* " + sch.getCurrentlyExecutingJobs());

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

1 个答案:

答案 0 :(得分:0)

您可以使用scheduler.getCurrentlyExecutingJobs()来获取当前正在运行的所有作业的列表。

       List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
       for (JobExecutionContext jobCtx: currentJobs){
           jobName = jobCtx.getJobDetail().getName();
           groupName = jobCtx.getJobDetail().getGroup();
           if (jobName.equalsIgnoreCase("job_I_am_looking_for_name") &&            groupName.equalsIgnoreCase("job_group_I_am_looking_for_name")) {
               //found it!
               logger.warn("the job is already running - do nothing");
                        return;
           }               
       }