Hi Here is the answer to your request:
1) Step 1: Record the task:
package com.hitesh.quartz.test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class QuartzJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("Hello"); } }
2) Write web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>TestWebBasedQuartz</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>quartz:shutdown-on-unload</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>quartz:wait-on-shutdown</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>quartz:start-on-load</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class> </listener> <listener> <listener-class>com.hitesh.quartz.test.QuartzJobListener</listener-class> </listener> </web-app>
As you can see, there are two listeners. One of them relates to the Quartz API and the other APIs. The first Quartz API listener will execute it in order. At the moment, we will have a ready-made factory scheduler. This listener will also start the scheduler if the corresponding quartz: start-on-load property is either not specified or is specified as true.
3) Write your quartz listener:
package com.hitesh.quartz.test; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.ee.servlet.QuartzInitializerListener; import org.quartz.impl.StdSchedulerFactory; public class QuartzJobListener implements ServletContextListener { private Scheduler scheduler; @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent ctx) { JobDetail job = JobBuilder.newJob(QuartzJob.class) .withIdentity("dummyJobName", "group1").build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(1).repeatForever()) .build(); try{ scheduler = ((StdSchedulerFactory) ctx.getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY)).getScheduler(); scheduler.scheduleJob(job, trigger); }catch(SchedulerException e){ } } }
This listener will be executed after the Quartz listener is executed. This means that we have a factory ready scheduler with us and a running scheduler. Therefore, you only need to add the task to the scheduler. As you can see, the contextDestroyed method is empty, since the work to turn off the scheduler will be performed by the Quartz API scheduler.
Hitesh
source share