- 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
Integrating Quartz Scheduler with Java Web Application
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.
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
com.quartz.QuartzListener
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
Hi, Could you help me on injecting the beans with quartz job ?
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?
how to get database connection in job with this approach.
when i am trying to write any job related to data base the job is not working.
But when i have written any sql query to select some data then it is not working.
thanks you!!! it works!!
Most welcome :)