001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.ee.servlet;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletContextEvent;
025: import javax.servlet.ServletContextListener;
026:
027: import org.quartz.Scheduler;
028: import org.quartz.impl.StdSchedulerFactory;
029:
030: /**
031: * <p>
032: * A ServletContextListner that can be used to initialize Quartz.
033: * </p>
034: *
035: * <p>
036: * You'll want to add something like this to your WEB-INF/web.xml file:
037: *
038: * <pre>
039: * <context-param>
040: * <param-name>config-file</param-name>
041: * <param-value>/some/path/my_quartz.properties</param-value>
042: * </context-param>
043: * <context-param>
044: * <param-name>shutdown-on-unload</param-name>
045: * <param-value>true</param-value>
046: * </context-param>
047: * <context-param>
048: * <param-name>start-scheduler-on-load</param-name>
049: * <param-value>true</param-value>
050: * </context-param>
051: *
052: * <listener>
053: * <listener-class>
054: * org.quartz.ee.servlet.QuartzInitializerServletListener
055: * </listener-class>
056: * </listener>
057: * </pre>
058: *
059: * </p>
060: * <p>
061: * The init parameter 'config-file' can be used to specify the path (and
062: * filename) of your Quartz properties file. If you leave out this parameter,
063: * the default ("quartz.properties") will be used.
064: * </p>
065: *
066: * <p>
067: * The init parameter 'shutdown-on-unload' can be used to specify whether you
068: * want scheduler.shutdown() called when the servlet is unloaded (usually when
069: * the application server is being shutdown). Possible values are "true" or
070: * "false". The default is "true".
071: * </p>
072: *
073: * <p>
074: * The init parameter 'start-scheduler-on-load' can be used to specify whether
075: * you want the scheduler.start() method called when the servlet is first loaded.
076: * If set to false, your application will need to call the start() method before
077: * the scheduler begins to run and process jobs. Possible values are "true" or
078: * "false". The default is "true", which means the scheduler is started.
079: * </p>
080: *
081: * A StdSchedulerFactory instance is stored into the ServletContext. You can gain access
082: * to the factory from a ServletContext instance like this:
083: * <br>
084: * <pre>
085: * StdSchedulerFactory factory = (StdSchedulerFactory) ctx
086: * .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);</pre>
087: * <p>
088: * The init parameter 'servlet-context-factory-key' can be used to override the
089: * name under which the StdSchedulerFactory is stored into the ServletContext, in
090: * which case you will want to use this name rather than
091: * <code>QuartzInitializerListener.QUARTZ_FACTORY_KEY</code> in the above example.
092: * </p>
093: *
094: * Once you have the factory instance, you can retrieve the Scheduler instance by calling
095: * <code>getScheduler()</code> on the factory.
096: *
097: * @author James House
098: * @author Chuck Cavaness
099: * @author John Petrocik
100: */
101: public class QuartzInitializerListener implements
102: ServletContextListener {
103:
104: public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
105:
106: private boolean performShutdown = true;
107:
108: private Scheduler scheduler = null;
109:
110: /*
111: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112: *
113: * Interface.
114: *
115: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116: */
117:
118: public void contextInitialized(ServletContextEvent sce) {
119:
120: System.out
121: .println("Quartz Initializer Servlet loaded, initializing Scheduler...");
122:
123: ServletContext servletContext = sce.getServletContext();
124: StdSchedulerFactory factory;
125: try {
126:
127: String configFile = servletContext
128: .getInitParameter("config-file");
129: String shutdownPref = servletContext
130: .getInitParameter("shutdown-on-unload");
131:
132: if (shutdownPref != null) {
133: performShutdown = Boolean.valueOf(shutdownPref)
134: .booleanValue();
135: }
136:
137: // get Properties
138: if (configFile != null) {
139: factory = new StdSchedulerFactory(configFile);
140: } else {
141: factory = new StdSchedulerFactory();
142: }
143:
144: // Always want to get the scheduler, even if it isn't starting,
145: // to make sure it is both initialized and registered.
146: scheduler = factory.getScheduler();
147:
148: // Should the Scheduler being started now or later
149: String startOnLoad = servletContext
150: .getInitParameter("start-scheduler-on-load");
151: /*
152: * If the "start-scheduler-on-load" init-parameter is not specified,
153: * the scheduler will be started. This is to maintain backwards
154: * compatability.
155: */
156: if (startOnLoad == null
157: || (Boolean.valueOf(startOnLoad).booleanValue())) {
158: // Start now
159: scheduler.start();
160: System.out.println("Scheduler has been started...");
161: } else {
162: System.out
163: .println("Scheduler has not been started. Use scheduler.start()");
164: }
165:
166: String factoryKey = servletContext
167: .getInitParameter("servlet-context-factory-key");
168: if (factoryKey == null) {
169: factoryKey = QUARTZ_FACTORY_KEY;
170: }
171:
172: System.out
173: .println("Storing the Quartz Scheduler Factory in the servlet context at key: "
174: + factoryKey);
175: servletContext.setAttribute(factoryKey, factory);
176:
177: } catch (Exception e) {
178: System.out
179: .println("Quartz Scheduler failed to initialize: "
180: + e.toString());
181: e.printStackTrace();
182: }
183: }
184:
185: public void contextDestroyed(ServletContextEvent sce) {
186:
187: if (!performShutdown) {
188: return;
189: }
190:
191: try {
192: if (scheduler != null) {
193: scheduler.shutdown();
194: }
195: } catch (Exception e) {
196: System.out
197: .println("Quartz Scheduler failed to shutdown cleanly: "
198: + e.toString());
199: e.printStackTrace();
200: }
201:
202: System.out.println("Quartz Scheduler successful shutdown.");
203: }
204:
205: }
|