1 17 18 21 package org.quartz.ee.servlet; 22 23 import java.io.IOException ; 24 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.quartz.Scheduler; 32 import org.quartz.impl.StdSchedulerFactory; 33 34 113 public class QuartzInitializerServlet extends HttpServlet { 114 115 public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY"; 116 117 private boolean performShutdown = true; 118 119 private Scheduler scheduler = null; 120 121 128 129 public void init(ServletConfig cfg) throws javax.servlet.ServletException { 130 super.init(cfg); 131 132 log("Quartz Initializer Servlet loaded, initializing Scheduler..."); 133 134 StdSchedulerFactory factory; 135 try { 136 137 String configFile = cfg.getInitParameter("config-file"); 138 String shutdownPref = cfg.getInitParameter("shutdown-on-unload"); 139 140 if (shutdownPref != null) { 141 performShutdown = Boolean.valueOf(shutdownPref).booleanValue(); 142 } 143 144 if (configFile != null) { 146 factory = new StdSchedulerFactory(configFile); 147 } else { 148 factory = new StdSchedulerFactory(); 149 } 150 151 scheduler = factory.getScheduler(); 154 155 String startOnLoad = cfg 157 .getInitParameter("start-scheduler-on-load"); 158 163 if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) { 164 scheduler.start(); 166 log("Scheduler has been started..."); 167 } else { 168 log("Scheduler has not been started. Use scheduler.start()"); 169 } 170 171 String factoryKey = cfg.getInitParameter("servlet-context-factory-key"); 172 if (factoryKey == null) { 173 factoryKey = QUARTZ_FACTORY_KEY; 174 } 175 176 log("Storing the Quartz Scheduler Factory in the servlet context at key: " 177 + factoryKey); 178 cfg.getServletContext().setAttribute(factoryKey, factory); 179 180 } catch (Exception e) { 181 log("Quartz Scheduler failed to initialize: " + e.toString()); 182 throw new ServletException (e); 183 } 184 } 185 186 public void destroy() { 187 188 if (!performShutdown) { 189 return; 190 } 191 192 try { 193 if (scheduler != null) { 194 scheduler.shutdown(); 195 } 196 } catch (Exception e) { 197 log("Quartz Scheduler failed to shutdown cleanly: " + e.toString()); 198 e.printStackTrace(); 199 } 200 201 log("Quartz Scheduler successful shutdown."); 202 } 203 204 public void doPost(HttpServletRequest request, HttpServletResponse response) 205 throws ServletException , IOException { 206 response.sendError(HttpServletResponse.SC_FORBIDDEN); 207 } 208 209 public void doGet(HttpServletRequest request, HttpServletResponse response) 210 throws ServletException , IOException { 211 response.sendError(HttpServletResponse.SC_FORBIDDEN); 212 } 213 214 } 215 | Popular Tags |