Using a ServletContextListener

It's worth mentioning that you can configure and integrate Quartz within a Web application in another way. Starting with version 2.3 of the Servlet API, you can create listeners that get callbacks from the servlet container at certain times during the life cycle of the container. One of the listener interfaces is called javax.servlet.ServletContextListener and contains two methods:

public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce );

The container calls these two methods when the container is started and shut down, respectively. It's possible to initialize the Quartz Scheduler in the contextInitialized() method and shut it down from the contextDestroyed() method. Listing 13.5 illustrates this.

Listing 13.5. A ServletContextListener Can Also Be Used to Initialize Quartz

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzServletContextListener implements ServletContextListener {
 private static Log logger = LogFactory
 .getLog(QuartzServletContextListener.class);

 public static final String QUARTZ_FACTORY_KEY =
 "org.quartz.impl.StdSchedulerFactory.KEY";

 private ServletContext ctx = null;

 private StdSchedulerFactory factory = null;

 /**
 * Called when the container is shutting down.
 */
 public void contextDestroyed(ServletContextEvent sce) {
 try {
 factory.getDefaultScheduler().shutdown();
 } catch (SchedulerException ex) {
 logger.error("Error stopping Quartz", ex);
 }

 }

 /**
 * Called when the container is first started.
 */
 public void contextInitialized(ServletContextEvent sce) {

 ctx = sce.getServletContext();

 try {

 factory = new StdSchedulerFactory();

 // Start the scheduler now

 factory.getScheduler().start();

 logger.info("Storing QuartzScheduler Factory at"
 + QUARTZ_FACTORY_KEY);

 ctx.setAttribute(QUARTZ_FACTORY_KEY, factory);

 } catch (Exception ex) {
 logger.error("Quartz failed to initialize", ex);
 }
 }
}

As we did for the QuartzInitializerServlet, we need to add some configuration information to the Web deployment descriptor (web.xml) for the listener. For our listener, we need to add a element to the deployment descriptor. It is shown in this snippet:

 
 
 org.cavaness.jobconsole.web.QuartzServletContextListener
 
 
 

 

New QuartzInitializerListener Added to Quartz

A ServletContextListener has been added to the Quartz framework because of requests from the user community. The code from Listing 13.5 should be considered an example in case you need to develop your own listener.


Introducing the Quartz Web Application

Scheduling in the Enterprise

Getting Started with Quartz

Hello, Quartz

Scheduling Jobs

Cron Triggers and More

JobStores and Persistence

Implementing Quartz Listeners

Using Quartz Plug-Ins

Using Quartz Remotely

Using Quartz with J2EE

Clustering Quartz

Quartz Cookbook

Quartz and Web Applications

Using Quartz with Workflow

Appendix A. Quartz Configuration Reference



Quartz Job Scheduling Framework(c) Building Open Source Enterprise Applications
Quartz Job Scheduling Framework: Building Open Source Enterprise Applications
ISBN: 0131886703
EAN: 2147483647
Year: N/A
Pages: 148

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net