Tutorialspoint examples

Learn java tutorial with examples for beginners online

Quartz 2 JobListener example


JobListener provides the facility to track the status of running jobs. To write a JobListener we have to implements the JobListener interface.



import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
 * This class is used for executing quartz job 
 * using SimpleTrigger(Quartz 2.1.5).
 * @author javawithease
public class SimpleTriggerTest {
	public static void main(String args[]){
    		//Set job key.
    		JobKey jobKey = new JobKey("helloJob", "group1");
    		//Set job details.
    		JobDetail job = JobBuilder.newJob(HelloJob.class)
        	//Set the scheduler timings.
    		Trigger trigger = TriggerBuilder.newTrigger()
    		  .withIdentity("simpleTrigger", "group1")
        	//Execute the job.
    		Scheduler scheduler = 
    			new StdSchedulerFactory().getScheduler();
    		//Attach listener to jobKey, 
    		//use GroupMatcher.jobGroupEquals("groupName")
    		//to attach with group. 
    	    		new HelloJobListener(), 
        	scheduler.scheduleJob(job, trigger);
    	}catch(Exception e){


import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
 * This class is used as JobListener for HelloJob.
 * @author javawithease
public class HelloJobListener implements JobListener {
	public static final String LISTENER_NAME = "helloJobListener";
	//It returns the job listener name.
	public String getName() {
	// It executes when the job is going to start executing.
	public void jobToBeExecuted(JobExecutionContext context) { 
		String jobName = 
		System.out.println("Job " + jobName + 
				" is going to start executing."); 
	public void jobExecutionVetoed(JobExecutionContext context) {
	     System.out.println("jobExecutionVetoed method is called.");
	//It executes when the job has been executed.
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {		
	   String jobName = context.getJobDetail().getKey().toString();
	   System.out.println("Job " + jobName + " is finished."); 


import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 * This class defines a quartz job.
 * @author javawithease
public class HelloJob implements Job{
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		System.out.println("Hello World.");		 


Job group1.helloJob is going to start executing.
Hello World.
Job group1.helloJob is finished.
Job group1.helloJob is going to start executing.
Hello World.
Job group1.helloJob is finished.
Job group1.helloJob is going to start executing.
Hello World.
Job group1.helloJob is finished.

Download this example.
Next Topic: Quartz multiple jobs example.
Previous Topic: Quartz 2.1.5 example using CronTrigger with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status