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

No comments: