Thursday, 4 March 2021

Quartz: Get all triggers

 

List<String> triggerGroupNames = scheduler.getTriggerGroupNames();
for (String groupName : triggerGroupNames) {
	for (TriggerKey triggerKey : scheduler.getTriggerKeys(GroupMatcher.triggerGroupEquals(groupName))) {
		String triggerName = triggerKey.getName();
		String triggerGroup = triggerKey.getGroup();

		Trigger trigger = scheduler.getTrigger(triggerKey);
		Date nextFireTime = trigger.getNextFireTime();

		System.out.println("triggerName : " + triggerName + " triggerGroup : " + triggerGroup + " - " + 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);

			}

		}

		List<String> triggerGroupNames = scheduler.getTriggerGroupNames();
		for (String groupName : triggerGroupNames) {
			for (TriggerKey triggerKey : scheduler.getTriggerKeys(GroupMatcher.triggerGroupEquals(groupName))) {
				String triggerName = triggerKey.getName();
				String triggerGroup = triggerKey.getGroup();

				Trigger trigger = scheduler.getTrigger(triggerKey);
				Date nextFireTime = trigger.getNextFireTime();

				System.out.println(
						"triggerName : " + triggerName + " triggerGroup : " + triggerGroup + " - " + nextFireTime);

			}

		}

	}
}

 

Output

jobName : My_Timer_Job groupName : My_Timer_Job_Group - Thu Mar 04 15:14:54 IST 2021 triggerName : Trigger1 triggerGroup : DEFAULT - Thu Mar 04 15:14:54 IST 2021

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment