Quartz之QuartzInitializerListener

问题:我想在WEB容器启动时就执行任务怎么办呢 
Quartz:使用QuartzInitializerListener就可办到了
 
请注意它的优先级别比QuartzInitializerServlet要高 
在web.xml中可配置的参数如下: 
如: 

Java代码    收藏代码
  1. <context-param>  
  2.          <param-name>quartz:config-file</param-name>  
  3.          <param-value>/quartz.properties</param-value>  
  4. </context-param>  


以下二者参数可代表都是同一个意思 
quartz:config-file                                                       或者 config-file 
quartz:shutdown-on-unload                                       或者  shutdown-on-unload 
quartz:wait-on-shutdown 
quartz:start-on-load                                                  或者   start-scheduler-on-load 
quartz:start-delay-seconds                                        或者   start-delay-seconds 
quartz:servlet-context-factory-key                              或者   servlet-context-factory-key 
默认值为:org.quartz.impl.StdSchedulerFactory.KEY 
quartz:scheduler-context-servlet-context-key              或者 scheduler-context-servlet-context-key
 
以上参数都是根据QuartzInitializerListener源码得来的 
QuartzInitializerListener源码如下: 

Java代码    收藏代码
  1. package org.quartz.ee.servlet;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6. import org.quartz.Scheduler;  
  7. import org.quartz.impl.StdSchedulerFactory;  
  8. import org.slf4j.Logger;  
  9. import org.slf4j.LoggerFactory;  
  10.   
  11. public class QuartzInitializerListener implements ServletContextListener {  
  12.       
  13.     public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";  
  14.     private boolean performShutdown;  
  15.     private boolean waitOnShutdown;  
  16.     private Scheduler scheduler;  
  17.     private final Logger log;  
  18.   
  19.     public QuartzInitializerListener() {  
  20.         this.performShutdown = true;  
  21.         this.waitOnShutdown = false;  
  22.   
  23.         this.scheduler = null;  
  24.   
  25.         this.log = LoggerFactory.getLogger(super.getClass());  
  26.     }  
  27.   
  28.     public void contextInitialized(ServletContextEvent sce) {  
  29.         this.log  
  30.                 .info("Quartz Initializer Servlet loaded, initializing Scheduler...");  
  31.   
  32.         ServletContext servletContext = sce.getServletContext();  
  33.         try {  
  34.             String configFile = servletContext  
  35.                     .getInitParameter("quartz:config-file");  
  36.             if (configFile == null)  
  37.                 configFile = servletContext.getInitParameter("config-file");  
  38.             String shutdownPref = servletContext  
  39.                     .getInitParameter("quartz:shutdown-on-unload");  
  40.             if (shutdownPref == null)  
  41.                 shutdownPref = servletContext  
  42.                         .getInitParameter("shutdown-on-unload");  
  43.             if (shutdownPref != null) {  
  44.                 this.performShutdown = Boolean.valueOf(shutdownPref)  
  45.                         .booleanValue();  
  46.             }  
  47.             String shutdownWaitPref = servletContext  
  48.                     .getInitParameter("quartz:wait-on-shutdown");  
  49.             if (shutdownPref != null)  
  50.                 this.waitOnShutdown = Boolean.valueOf(shutdownWaitPref)  
  51.                         .booleanValue();  
  52.   
  53.             StdSchedulerFactory factory;  
  54.   
  55.             if (configFile != null)  
  56.                 factory = new StdSchedulerFactory(configFile);  
  57.             else {  
  58.                 factory = new StdSchedulerFactory();  
  59.             }  
  60.   
  61.             this.scheduler = factory.getScheduler();  
  62.   
  63.             String startOnLoad = servletContext  
  64.                     .getInitParameter("quartz:start-on-load");  
  65.             if (startOnLoad == null) {  
  66.                 startOnLoad = servletContext  
  67.                         .getInitParameter("start-scheduler-on-load");  
  68.             }  
  69.             int startDelay = 0;  
  70.             String startDelayS = servletContext  
  71.                     .getInitParameter("quartz:start-delay-seconds");  
  72.             if (startDelayS == null)  
  73.                 startDelayS = servletContext  
  74.                         .getInitParameter("start-delay-seconds");  
  75.             try {  
  76.                 if ((startDelayS != null) && (startDelayS.trim().length() > 0))  
  77.                     startDelay = Integer.parseInt(startDelayS);  
  78.             } catch (Exception e) {  
  79.                 this.log  
  80.                         .error("Cannot parse value of 'start-delay-seconds' to an integer: "  
  81.                                 + startDelayS + ", defaulting to 5 seconds.");  
  82.                 startDelay = 5;  
  83.             }  
  84.   
  85.             if ((startOnLoad == null)  
  86.                     || (Boolean.valueOf(startOnLoad).booleanValue()))  
  87.                 if (startDelay <= 0) {  
  88.                     this.scheduler.start();  
  89.                     this.log.info("Scheduler has been started...");  
  90.                 } else {  
  91.                     this.scheduler.startDelayed(startDelay);  
  92.                     this.log.info("Scheduler will start in " + startDelay  
  93.                             + " seconds.");  
  94.                 }  
  95.             else {  
  96.                 this.log  
  97.                         .info("Scheduler has not been started. Use scheduler.start()");  
  98.             }  
  99.   
  100.             String factoryKey = servletContext  
  101.                     .getInitParameter("quartz:servlet-context-factory-key");  
  102.             if (factoryKey == null)  
  103.                 factoryKey = servletContext  
  104.                         .getInitParameter("servlet-context-factory-key");  
  105.             if (factoryKey == null) {  
  106.                 factoryKey = "org.quartz.impl.StdSchedulerFactory.KEY";  
  107.             }  
  108.   
  109.             this.log  
  110.                     .info("Storing the Quartz Scheduler Factory in the servlet context at key: "  
  111.                             + factoryKey);  
  112.   
  113.             servletContext.setAttribute(factoryKey, factory);  
  114.   
  115.             String servletCtxtKey = servletContext  
  116.                     .getInitParameter("quartz:scheduler-context-servlet-context-key");  
  117.             if (servletCtxtKey == null)  
  118.                 servletCtxtKey = servletContext  
  119.                         .getInitParameter("scheduler-context-servlet-context-key");  
  120.             if (servletCtxtKey != null) {  
  121.                 this.log  
  122.                         .info("Storing the ServletContext in the scheduler context at key: "  
  123.                                 + servletCtxtKey);  
  124.   
  125.                 this.scheduler.getContext().put(servletCtxtKey, servletContext);  
  126.             }  
  127.         } catch (Exception e) {  
  128.             this.log.error("Quartz Scheduler failed to initialize: "  
  129.                     + e.toString());  
  130.             e.printStackTrace();  
  131.         }  
  132.     }  
  133.   
  134.     public void contextDestroyed(ServletContextEvent sce) {  
  135.         if (!this.performShutdown) {  
  136.             return;  
  137.         }  
  138.         try {  
  139.             if (this.scheduler != null)  
  140.                 this.scheduler.shutdown(this.waitOnShutdown);  
  141.         } catch (Exception e) {  
  142.             this.log.error("Quartz Scheduler failed to shutdown cleanly: "  
  143.                     + e.toString());  
  144.             e.printStackTrace();  
  145.         }  
  146.   
  147.         this.log.info("Quartz Scheduler successful shutdown.");  
  148.     }  
  149. }  

猜你喜欢

转载自zxb1985.iteye.com/blog/1841412