In my previous article I have explained about how to write a simple quartz scheduler. In some cases, you would like to get the list of Jobs running on the scheduler. It is obvious that there will be multiple batch process configured in a large applications. If you could get the list of jobs and the next triggered time for each job, it is good for developers to use in their programming. With the extension of my previous example, this tutorial adds code snippet for listing the jobs running on scheduler. This also get the next trigger time for that particular job. Hope this helps. If you have any questions, please write it in the comments section.
getJobGroupNames() method in the scheduler instance returns the list of jobs. Also you can use the size method to get number of jobs.
ListJobs.java
package javabeat.net.quartz; import java.util.Date; import java.util.List; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleTrigger; import org.quartz.Trigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.matchers.GroupMatcher; import org.quartz.impl.triggers.SimpleTriggerImpl; public class ListJobs { public static void main(String args[]) throws SchedulerException { Scheduler scheduler = new StdSchedulerFactory().getScheduler(); // Creating Job and link to our Job class JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(FirstJob.class); // Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(2000); simpleTrigger.setName("FirstTrigger"); // Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail, simpleTrigger); for (String groupName : scheduler.getJobGroupNames()) { for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher .jobGroupEquals(groupName))) { String jobName = jobKey.getName(); String jobGroup = jobKey.getGroup(); List<Trigger> triggers = (List<Trigger>) scheduler .getTriggersOfJob(jobKey); Date nextFireTime = triggers.get(0).getNextFireTime(); System.out.println("[jobName] : " + jobName + " [groupName] : " + jobGroup + " - " + nextFireTime); } } } }