How to use quartz with QuartzInitializerListener?

I find it difficult to understand how to use quartz with QuartzInitializerListener .

First, I declare this listener in the deployment descriptor. But then, how can I add my work? Looking at the implementation of QuartzInitializerListener , I see that it creates SchedulerFactory and Scheduler , but I see no way to add tasks, the factory receives the configuration file , but again nothing is connected with the tasks there.

I just found very simple examples from my searches, all about instantiating everything in the main method.

Can someone point me to a more real example? I use JBoss 5 if that matters. Thanks.

+7
source share
2 answers

Everything is described in the Javadoc source code you quote:

The StdSchedulerFactory instance is stored in the ServletContext. You can access the factory from a ServletContext instance, for example:

 StdSchedulerFactory factory = (StdSchedulerFactory) ctx .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY); 

EDIT . This means that when you use this listener, you can get a SchedulerFactory inside each servlet / Spring of the MVC / ... controller by doing:

 StdSchedulerFactory factory = (StdSchedulerFactory)httpServletRequest .getServletContext() .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY); 

Note that context listeners are guaranteed to be executed before any servlet is used to process incoming requests. This means that the scheduler will always be properly initialized before it is used.


Summary of the discussion in the comments below, the discussion actually answers the question asked: If you want to add tasks at application startup, write another listener (similar to the one provided by Quartz), look at StdSchedulerFactory (ServletContext is easily accessible) and do what you want. Listeners are guaranteed to execute in the same order as they are declared in web.xml, so put your listener after Quartz.

+5
source

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.

+2
source

All Articles