javaquartz-scheduler

How to query the scheduled jobs from Quartz scheduler?


I have a Scheduler object in my application and I add Jobs to it using the scheduleJob method.

In my code I schedule Jobs with an instant Trigger:

TriggerBuilder.newTrigger().startNow().build();

My question is how to tell which Jobs are scheduled into my Scheduler? There is only a getCurrentlyExecutingJobs method which seems unreliable so far.


Solution

  • The below code list all Quartz job associated to a scheduler (Quartz 2.x.x)

    for (String groupName : scheduler.getJobGroupNames()) {
    
     for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
    
      String jobName = jobKey.getName();
      String jobGroup = jobKey.getGroup();
    
      //get job's trigger
      List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
      Date nextFireTime = triggers.get(0).getNextFireTime(); 
    
        System.out.println("[jobName] : " + jobName + " [groupName] : "
            + jobGroup + " - " + nextFireTime);
    
      }
    
    }