Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

Check whether the job is running or not?

I want to check whether Quartz job is running or not.I found that it can be used using scheduler.getCurrentlyExecutingJobs(). BuI'm confused with that and where exactly should I put it to get the results? Thanks

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();
    }

Upvotes: 0

Views: 4269

Answers (1)

paul
paul

Reputation: 13471

You can use scheduler.getCurrentlyExecutingJobs() to get a list of all jobs which are currently running.

       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;
           }               
       }

Upvotes: 2

Related Questions