• September 4, 2014
  • Quartz Scheduler
  • Comments Off on Quartz 2 JobListener example

Quartz 2 JobListener example

JobListener:

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

Example:

SimpleTriggerTest.java

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[]){
    	try{
    		//Set job key.
    		JobKey jobKey = new JobKey("helloJob", "group1");
    		//Set job details.
    		JobDetail job = JobBuilder.newJob(HelloJob.class)
    				.withIdentity(jobKey).build();
 
        	//Set the scheduler timings.
    		Trigger trigger = TriggerBuilder.newTrigger()
    		  .withIdentity("simpleTrigger", "group1")
    		  .withSchedule(SimpleScheduleBuilder.simpleSchedule()
    		  .withIntervalInSeconds(10).repeatForever()).build();
 
        	//Execute the job.
    		Scheduler scheduler = 
    			new StdSchedulerFactory().getScheduler();
    		//Attach listener to jobKey, 
    		//use GroupMatcher.jobGroupEquals("groupName")
    		//to attach with group. 
    		scheduler.getListenerManager().addJobListener(
    	    		new HelloJobListener(), 
    	    		KeyMatcher.keyEquals(jobKey));
        	scheduler.start();
        	scheduler.scheduleJob(job, trigger);
    	}catch(Exception e){
    		e.printStackTrace();
    	}    	
	}
}

HelloJobListener.java

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.
	@Override
	public String getName() {
		return LISTENER_NAME;
	}
 
	// It executes when the job is going to start executing.
	@Override
	public void jobToBeExecuted(JobExecutionContext context) { 
		String jobName = 
			context.getJobDetail().getKey().toString();
		System.out.println("Job " + jobName + 
				" is going to start executing."); 
	}
 
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
	     System.out.println("jobExecutionVetoed method is called.");
	}
 
	//It executes when the job has been executed.
	@Override
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {		
	   String jobName = context.getJobDetail().getKey().toString();
	   System.out.println("Job " + jobName + " is finished."); 
	}
 
}

HelloJob.java

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.");		 
	}
}

Output:

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.

 


Comments are closed.