1 17 18 21 package org.quartz.ee.servlet; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletContextEvent ; 25 import javax.servlet.ServletContextListener ; 26 27 import org.quartz.Scheduler; 28 import org.quartz.impl.StdSchedulerFactory; 29 30 101 public class QuartzInitializerListener implements ServletContextListener { 102 103 public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY"; 104 105 private boolean performShutdown = true; 106 107 private Scheduler scheduler = null; 108 109 116 117 public void contextInitialized(ServletContextEvent sce) { 118 119 System.out.println("Quartz Initializer Servlet loaded, initializing Scheduler..."); 120 121 ServletContext servletContext = sce.getServletContext(); 122 StdSchedulerFactory factory; 123 try { 124 125 String configFile = servletContext.getInitParameter("config-file"); 126 String shutdownPref = servletContext.getInitParameter("shutdown-on-unload"); 127 128 if (shutdownPref != null) { 129 performShutdown = Boolean.valueOf(shutdownPref).booleanValue(); 130 } 131 132 if (configFile != null) { 134 factory = new StdSchedulerFactory(configFile); 135 } else { 136 factory = new StdSchedulerFactory(); 137 } 138 139 scheduler = factory.getScheduler(); 142 143 String startOnLoad = servletContext 145 .getInitParameter("start-scheduler-on-load"); 146 151 if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) { 152 scheduler.start(); 154 System.out.println("Scheduler has been started..."); 155 } else { 156 System.out.println("Scheduler has not been started. Use scheduler.start()"); 157 } 158 159 String factoryKey = 160 servletContext.getInitParameter("servlet-context-factory-key"); 161 if (factoryKey == null) { 162 factoryKey = QUARTZ_FACTORY_KEY; 163 } 164 165 System.out.println("Storing the Quartz Scheduler Factory in the servlet context at key: " 166 + factoryKey); 167 servletContext.setAttribute(factoryKey, factory); 168 169 } catch (Exception e) { 170 System.out.println("Quartz Scheduler failed to initialize: " + e.toString()); 171 e.printStackTrace(); 172 } 173 } 174 175 public void contextDestroyed(ServletContextEvent sce) { 176 177 if (!performShutdown) { 178 return; 179 } 180 181 try { 182 if (scheduler != null) { 183 scheduler.shutdown(); 184 } 185 } catch (Exception e) { 186 System.out.println("Quartz Scheduler failed to shutdown cleanly: " + e.toString()); 187 e.printStackTrace(); 188 } 189 190 System.out.println("Quartz Scheduler successful shutdown."); 191 } 192 193 194 } 195 | Popular Tags |