1 26 27 package org.infoglue.deliver.jobs; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.quartz.Scheduler; 39 import org.quartz.impl.StdSchedulerFactory; 40 41 90 public class QuartzInitializerServlet extends HttpServlet { 91 92 private boolean performShutdown = true; 93 94 101 102 public void init(ServletConfig cfg) throws javax.servlet.ServletException { 103 super.init(cfg); 104 105 log("Quartz Initializer Servlet loaded, initializing Scheduler..."); 106 107 StdSchedulerFactory factory; 108 try { 109 110 String configFile = cfg.getInitParameter("config-file"); 111 String shutdownPref = cfg.getInitParameter("shutdown-on-unload"); 112 if(configFile == null) 113 { 114 String contextRootPath = cfg.getServletContext().getRealPath("/"); 115 if(!contextRootPath.endsWith("/") && !contextRootPath.endsWith("\\")) 116 contextRootPath = contextRootPath + File.separator; 117 118 configFile = contextRootPath + "WEB-INF" + File.separator + "classes" + File.separator + "quartz.properties"; 119 } 120 121 if(shutdownPref != null) 122 performShutdown = Boolean.valueOf(shutdownPref).booleanValue(); 123 124 if (configFile != null) 126 { 127 factory = new StdSchedulerFactory(configFile); 128 } 129 else 130 { 131 factory = new StdSchedulerFactory(); 132 } 133 134 Scheduler scheduler = factory.getScheduler(); 135 scheduler.start(); 136 137 } catch (Exception e) { 138 e.printStackTrace(); 139 log("Quartz Scheduler failed to initialize: " + e.toString()); 140 throw new ServletException (e); 141 } 142 } 143 144 public void destroy() { 145 146 if(!performShutdown) 147 return; 148 149 try { 150 Scheduler sched = StdSchedulerFactory.getDefaultScheduler(); 151 152 if (sched != null) sched.shutdown(); 153 } catch (Exception e) { 154 log("Quartz Scheduler failed to shutdown cleanly: " + e.toString()); 155 e.printStackTrace(); 156 } 157 158 log("Quartz Scheduler successful shutdown."); 159 } 160 161 public void doPost(HttpServletRequest request, HttpServletResponse response) 162 throws ServletException , IOException { 163 response.sendError(HttpServletResponse.SC_FORBIDDEN); 164 } 165 166 public void doGet(HttpServletRequest request, HttpServletResponse response) 167 throws ServletException , IOException { 168 response.sendError(HttpServletResponse.SC_FORBIDDEN); 169 } 170 171 } | Popular Tags |