Thursday, 4 March 2021

Quartz: Get all the jobs


List<String> groupNames = scheduler.getJobGroupNames();
for (String groupName : groupNames) {
	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);

	}

}

 

Find the below working application.

App.java

package com.sample.app;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.impl.triggers.CronTriggerImpl;

import com.sample.app.jobs.TimerJob;

public class App {
	private static final String JOB_NAME = "My_Timer_Job";
	private static final String JOB_GROUP_NMAE = "My_Timer_Job_Group";

	public static void main(String args[]) throws ParseException, SchedulerException, InterruptedException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();

		JobDetailImpl jobDetail = new JobDetailImpl();
		jobDetail.setName("First Job");
		jobDetail.setJobClass(TimerJob.class);
		jobDetail.setDescription("Simple Task Application");
		jobDetail.setRequestsRecovery(true);
		jobDetail.setDurability(true);
		jobDetail.setKey(new JobKey(JOB_NAME, JOB_GROUP_NMAE));

		CronTriggerImpl cronTrigger1 = new CronTriggerImpl();
		cronTrigger1.setStartTime(new Date(System.currentTimeMillis() + 1000));
		cronTrigger1.setCronExpression("0/3 * * * * ?");
		cronTrigger1.setName("FirstTrigger");
		cronTrigger1.setDescription("Simple Trigger");
		cronTrigger1.setKey(new TriggerKey("Trigger1"));

		scheduler.start();
		scheduler.scheduleJob(jobDetail, cronTrigger1);

		//TimeUnit.SECONDS.sleep(12);

		List<String> groupNames = scheduler.getJobGroupNames();
		for (String groupName : groupNames) {
			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);

			}

		}

	}
}


Output

jobName : My_Timer_Job groupName : My_Timer_Job_Group - Thu Mar 04 15:10:36 IST 2021




Previous                                                    Next                                                    Home

No comments:

Post a Comment