KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > ee > servlet > QuartzInitializerServlet


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.ee.servlet;
22
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.ServletConfig JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServlet JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.quartz.Scheduler;
32 import org.quartz.impl.StdSchedulerFactory;
33
34 /**
35  * <p>
36  * A Servlet that can be used to initialize Quartz, if configured as a
37  * load-on-startup servlet in a web application.
38  * </p>
39  *
40  * <p>
41  * You'll want to add something like this to your WEB-INF/web.xml file:
42  *
43  * <pre>
44  * &lt;servlet&gt;
45  * &lt;servlet-name&gt;
46  * QuartzInitializer
47  * &lt;/servlet-name&gt;
48  * &lt;display-name&gt;
49  * Quartz Initializer Servlet
50  * &lt;/display-name&gt;
51  * &lt;servlet-class&gt;
52  * org.quartz.ee.servlet.QuartzInitializerServlet
53  * &lt;/servlet-class&gt;
54  * &lt;load-on-startup&gt;
55  * 1
56  * &lt;/load-on-startup&gt;
57  * &lt;init-param&gt;
58  * &lt;param-name&gt;config-file&lt;/param-name&gt;
59  * &lt;param-value&gt;/some/path/my_quartz.properties&lt;/param-value&gt;
60  * &lt;/init-param&gt;
61  * &lt;init-param&gt;
62  * &lt;param-name&gt;shutdown-on-unload&lt;/param-name&gt;
63  * &lt;param-value&gt;true&lt;/param-value&gt;
64  * &lt;/init-param&gt;
65  * &lt;init-param&gt;
66  * &lt;param-name&gt;start-scheduler-on-load&lt;/param-name&gt;
67  * &lt;param-value&gt;true&lt;/param-value&gt;
68  * &lt;/init-param&gt;
69  * &lt;/servlet&gt;
70  * </pre>
71  *
72  * </p>
73  * <p>
74  * The init parameter 'config-file' can be used to specify the path (and
75  * filename) of your Quartz properties file. If you leave out this parameter,
76  * the default ("quartz.properties") will be used.
77  * </p>
78  *
79  * <p>
80  * The init parameter 'shutdown-on-unload' can be used to specify whether you
81  * want scheduler.shutdown() called when the servlet is unloaded (usually when
82  * the application server is being shutdown). Possible values are "true" or
83  * "false". The default is "true".
84  * </p>
85  *
86  * <p>
87  * The init parameter 'start-scheduler-on-load' can be used to specify whether
88  * you want the scheduler.start() method called when the servlet is first loaded.
89  * If set to false, your application will need to call the start() method before
90  * the scheduler begins to run and process jobs. Possible values are "true" or
91  * "false". The default is "true", which means the scheduler is started.
92  * </p>
93  *
94  * A StdSchedulerFactory instance is stored into the ServletContext. You can gain access
95  * to the factory from a ServletContext instance like this:
96  * <br>
97  * <pre>
98  * StdSchedulerFactory factory = (StdSchedulerFactory) ctx
99  * .getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);</pre>
100  * <p>
101  * The init parameter 'servlet-context-factory-key' can be used to override the
102  * name under which the StdSchedulerFactory is stored into the ServletContext, in
103  * which case you will want to use this name rather than
104  * <code>QuartzFactoryServlet.QUARTZ_FACTORY_KEY</code> in the above example.
105  * </p>
106  *
107  * Once you have the factory instance, you can retrieve the Scheduler instance by calling
108  * <code>getScheduler()</code> on the factory.
109  *
110  * @author James House
111  * @author Chuck Cavaness
112  */

113 public class QuartzInitializerServlet extends HttpServlet JavaDoc {
114
115     public static final String JavaDoc QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
116
117     private boolean performShutdown = true;
118
119     private Scheduler scheduler = null;
120
121     /*
122      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123      *
124      * Interface.
125      *
126      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127      */

128
129     public void init(ServletConfig JavaDoc cfg) throws javax.servlet.ServletException JavaDoc {
130         super.init(cfg);
131
132         log("Quartz Initializer Servlet loaded, initializing Scheduler...");
133
134         StdSchedulerFactory factory;
135         try {
136
137             String JavaDoc configFile = cfg.getInitParameter("config-file");
138             String JavaDoc shutdownPref = cfg.getInitParameter("shutdown-on-unload");
139
140             if (shutdownPref != null) {
141                 performShutdown = Boolean.valueOf(shutdownPref).booleanValue();
142             }
143
144             // get Properties
145
if (configFile != null) {
146                 factory = new StdSchedulerFactory(configFile);
147             } else {
148                 factory = new StdSchedulerFactory();
149             }
150             
151             // Always want to get the scheduler, even if it isn't starting,
152
// to make sure it is both initialized and registered.
153
scheduler = factory.getScheduler();
154             
155             // Should the Scheduler being started now or later
156
String JavaDoc startOnLoad = cfg
157                     .getInitParameter("start-scheduler-on-load");
158             /*
159              * If the "start-scheduler-on-load" init-parameter is not specified,
160              * the scheduler will be started. This is to maintain backwards
161              * compatability.
162              */

163             if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) {
164                 // Start now
165
scheduler.start();
166                 log("Scheduler has been started...");
167             } else {
168                 log("Scheduler has not been started. Use scheduler.start()");
169             }
170
171             String JavaDoc 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 JavaDoc e) {
181             log("Quartz Scheduler failed to initialize: " + e.toString());
182             throw new ServletException JavaDoc(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 JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
205         throws ServletException JavaDoc, IOException JavaDoc {
206         response.sendError(HttpServletResponse.SC_FORBIDDEN);
207     }
208
209     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
210         throws ServletException JavaDoc, IOException JavaDoc {
211         response.sendError(HttpServletResponse.SC_FORBIDDEN);
212     }
213
214 }
215
Popular Tags