Pages Navigation Menu

Coding is much easier than you think

Integrating Quartz Scheduler with Java Web Application

 
Quartz-Schedular
 
In our previous article we learn about Quartz 2 hello world example along with types of Triggers in Quartz 2, In this article we shall learn to Integrating Quartz 2 Scheduler in Java Web Application. Quartz, is an open source job scheduling framework, that let you schedule a task to run on a predefine date and time.
 

Library required

 
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
 

Scheduler Job

 
Create a Quartz’s job

package com.quartz;

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

public class QuartzJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("Java web application + Quartz 2.2.1");
	}
}

 

Servlet Listener

 
Now Create servlet listener class by implementing ServletContextListener interface and override contextInitialized and contextDestroyed methods with your logic’s.

  • contextInitialized() method will be executed automatically during Servlet container initialization, which in turn calls the Quartz scheduler job, which gets executed every 10 seconds.
  • contextDestroyed() method will be executed when the application shuts down, So in this function I have invoked the shutdown function of quartz scheduler

 

package com.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzListener implements ServletContextListener {
	Scheduler scheduler = null;

	@Override
	public void contextInitialized(ServletContextEvent servletContext) {
		System.out.println("Context Initialized");
		
		try {
			// Setup the Job class and the Job group
			JobDetail job = newJob(QuartzJob.class).withIdentity(
					"CronQuartzJob", "Group").build();

			// Create a Trigger that fires every 5 minutes.
			Trigger trigger = newTrigger()
			.withIdentity("TriggerName", "Group")
			.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
			.build();

			// Setup the Job and Trigger with Scheduler & schedule jobs
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
		}
		catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContext) {
		System.out.println("Context Destroyed");
		try 
		{
			scheduler.shutdown();
		} 
		catch (SchedulerException e) 
		{
			e.printStackTrace();
		}
	}
}

 
Recommended Article

 

web.xml

 
Configure the listener class QuartzSchedulerListener.java into the web.xml file as shown below

<listener>
<listener-class>com.quartz.QuartzListener</listener-class>
</listener>

 

Demo

 
Now on starting the Tomcat Server, the project gets started, and so the registered listener class QuartzSchedulerListener.java will be fired, and following output is obtained at the console
 
Integrating Quartz Scheduler with Java Web Applications
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Thiru

    Hi, Could you help me on injecting the beans with quartz job ?

  • info2scs

    Thanks for the tutorial. It works. However, when I use the cron expression as ‘0 0/5 * * * MON-FRI’ or ‘0 0/5 * * * 2-6′ I am getting the following message

    java.lang.RuntimeException: CronExpression ‘0 0/5 * * * MON-FRI’ is invalid.

    at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:111) [quartz-2.2.1.jar:]

    java.lang.RuntimeException: CronExpression ‘0 0/5 * * * 2-6′ is invalid.

    at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:111) [quartz-2.2.1.jar:]

    and when I used the cron expression ‘0 0/5 10-17 * * ?’, the cron was running even after 5pm. Any idea why is these behaviour?

  • NIC NIC

    how to get database connection in job with this approach.

  • Abinash Raula

    when i am trying to write any job related to data base the job is not working.

  • Abinash Raula

    But when i have written any sql query to select some data then it is not working.

  • Texan Rock

    thanks you!!! it works!!

    • http://www.simplecodestuffs.com/ Mohaideen Jamil

      Most welcome :)