Easy learning with example program codes

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 codesjava
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 codesjava
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 codesjava
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.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki