How to Get List of Jobs in Quartz Scheduler?

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.


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");

		// Creating schedule time with trigger
		SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
		simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));

		// Start scheduler
		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
				Date nextFireTime = triggers.get(0).getNextFireTime();
				System.out.println("[jobName] : " + jobName + " [groupName] : "
						+ jobGroup + " - " + nextFireTime);


Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!