• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Write Quartz JobListener?

November 8, 2013 //  by Krishna Srinivasan//  Leave a Comment

In my previous posts I have explained about writing simple quartz scheduler and how to list jobs. This tutorial explains how to write a listener for the scheduler. In certain scenarios, we have to execute few lines of code or log messages before and after the batch process. The listeners are very useful for achieving those goals. In quartz, JobListener interface provides jobToBeExecuted and jobWasExecuted as the callback methods for before and after every job is executed. These methods takes the parameter of JobExecutionContext, with this you can access the current Job details and print them. For the complete setup of the quartz scheduler, please refer my previous tutorial. If you have any questions, please write it in the comments section.

1. Create a Job

FirstJob.java

package javabeat.net.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class FirstJob implements Job{

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("This is my first quartz job!!");
	}

}

2. Create Quartz Job Listener

FirstJobListener.java

package javabeat.net.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class FirstJobListener implements JobListener{

	@Override
	public String getName() {
		return "FirstJobListener";
	}

	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		System.out.println("jobToBeExecuted : " + context.getJobDetail().getKey().toString());

	}
	@Override
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException arg1) {
		System.out.println("jobWasExecuted : " + context.getJobDetail().getKey().toString());

	}
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		System.out.println("jobExecutionVetoed : " + context.getJobDetail().getKey().toString());
	}

}

3. Add Quartz Job Listener to Scheduler

QuartzExample.java

package javabeat.net.quartz;

import java.util.Date;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

public class QuartzExample {
	public static void main(String args[]) throws SchedulerException{
		//Creating scheduler factory and scheduler
		SchedulerFactory factory = new StdSchedulerFactory();
		Scheduler scheduler = factory.getScheduler();

		//Creating Job and link to our Job class
		JobDetailImpl jobDetail = new JobDetailImpl();
		jobDetail.setName("First Job");
		jobDetail.setJobClass(FirstJob.class);

		//Creating schedule time with trigger
		SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
		simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
		simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
		simpleTrigger.setRepeatInterval(2000);
		simpleTrigger.setName("FirstTrigger");

		//Start scheduler
		scheduler.start();
		scheduler.scheduleJob(jobDetail,simpleTrigger);

		//Adding the listener
		scheduler.getListenerManager().addJobListener(new FirstJobListener());
	}
}

Category: JavaTag: Quartz Scheduler

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to Get List of Jobs in Quartz Scheduler?
Next Post: Configure Quartz Scheduler Jobs using XML File »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact