Quartz listing all jobs example

Example Explanation:

1. Create multiple jobs by implementing Job interface.
2. Perform your business logic in the execute method.
3. Create a class for executing multiple quartz jobs.
4. Get JobDetail object from JobBuilder and set job detail like name and job class for each job.
5. Create Trigger object from TriggerBuilder and set the scheduler details for each job.
6. Get Scheduler object from StdSchedulerFactory.
7. Use Scheduler object to connect quartz job and quartz trigger together and execute the job.
8. Iterate and process the quartz jobs Scheduler and Trigger methods.

Example:

CronTriggerTest.java

import java.util.Date;
import java.util.List;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
 
/**
 * This class is used for listing all quartz 
 * jobs using CronTrigger(Quartz 2.1.5).
 * @author w3spoint
 */
public class CronTriggerTest {
	public static void main(String args[]){
    	try{
    		//Set job details.
    		JobDetail job1 = JobBuilder.newJob(Job1.class)
    			.withIdentity("job1", "group1").build();
 
    		JobDetail job2 = JobBuilder.newJob(Job2.class)
    			.withIdentity("job2", "group1").build();
 
    		JobDetail job3 = JobBuilder.newJob(Job3.class)
    			.withIdentity("job3", "group2").build();
 
        	//Set the scheduler timings.
    		Trigger trigger1 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger1", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
    		Trigger trigger2 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger2", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
    		Trigger trigger3 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger3", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
        	//Execute the job.
    		Scheduler scheduler = 
    			new StdSchedulerFactory().getScheduler();
        	scheduler.start();
        	scheduler.scheduleJob(job1, trigger1);
        	scheduler.scheduleJob(job2, trigger2);
        	scheduler.scheduleJob(job3, trigger3);
 
        	//List all quartz jobs
        	for (String groupName : scheduler.getJobGroupNames()) {
       	         for (JobKey jobKey : scheduler.getJobKeys(
    	    		 GroupMatcher.jobGroupEquals(groupName))) {    	 
    		  String jobName = jobKey.getName();
    		  String jobGroup = jobKey.getGroup();
 
    		  List triggers = (List) 
    				  scheduler.getTriggersOfJob(jobKey);
    		  Date nextFireTime = triggers.get(0).getNextFireTime();
    		  System.out.println("Job : " + jobName + ", Group : "
    				+ jobGroup + ", Next execution time : "
    					+ nextFireTime); 
   	      }    	 
    	   }
    	}catch(Exception e){
    		e.printStackTrace();
    	}    	
    }
}

Job1.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author w3spoint
 */
public class Job1 implements Job{
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		System.out.println("Job1 is running.");		 
	}
}

Job2.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author w3spoint
 */
public class Job2 implements Job{
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		System.out.println("Job2 is running.");		 
	}
}

Job3.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author w3spoint
 */
public class Job3 implements Job{
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		System.out.println("Job3 is running.");		 
	}
}

Output:

Job : job1, Group:group1, Next execution time:Thu Sep 04 14:12:10 IST 2014
Job1 is running.
Job2 is running.
Job3 is running.
Job : job2, Group:group1, Next execution time:Thu Sep 04 14:12:10 IST 2014
Job : job3, Group:group2, Next execution time:Thu Sep 04 14:12:10 IST 2014
Job1 is running.
Job2 is running.
Job3 is running.
Job1 is running.
Job2 is running.
Job3 is running.
Job1 is running.
Job2 is running.
Job3 is running.
...

Download this example.
 
Next Topic: Struts 2 and Quartz 2 scheduler integration.
Previous Topic: Quartz multiple jobs example.

 

Content Protection by DMCA.com
Please Share