KickJava   Java API By Example, From Geeks To Geeks.

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


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

101 public class QuartzInitializerListener implements ServletContextListener JavaDoc {
102
103     public static final String JavaDoc QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
104
105     private boolean performShutdown = true;
106
107     private Scheduler scheduler = null;
108
109     /*
110      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111      *
112      * Interface.
113      *
114      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115      */

116
117     public void contextInitialized(ServletContextEvent JavaDoc sce) {
118
119         System.out.println("Quartz Initializer Servlet loaded, initializing Scheduler...");
120
121         ServletContext JavaDoc servletContext = sce.getServletContext();
122         StdSchedulerFactory factory;
123         try {
124
125             String JavaDoc configFile = servletContext.getInitParameter("config-file");
126             String JavaDoc shutdownPref = servletContext.getInitParameter("shutdown-on-unload");
127
128             if (shutdownPref != null) {
129                 performShutdown = Boolean.valueOf(shutdownPref).booleanValue();
130             }
131
132             // get Properties
133
if (configFile != null) {
134                 factory = new StdSchedulerFactory(configFile);
135             } else {
136                 factory = new StdSchedulerFactory();
137             }
138
139             // Always want to get the scheduler, even if it isn't starting,
140
// to make sure it is both initialized and registered.
141
scheduler = factory.getScheduler();
142
143             // Should the Scheduler being started now or later
144
String JavaDoc startOnLoad = servletContext
145                     .getInitParameter("start-scheduler-on-load");
146             /*
147              * If the "start-scheduler-on-load" init-parameter is not specified,
148              * the scheduler will be started. This is to maintain backwards
149              * compatability.
150              */

151             if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) {
152                 // Start now
153
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 JavaDoc 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 JavaDoc e) {
170             System.out.println("Quartz Scheduler failed to initialize: " + e.toString());
171             e.printStackTrace();
172         }
173     }
174
175     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
176
177         if (!performShutdown) {
178             return;
179         }
180
181         try {
182             if (scheduler != null) {
183                 scheduler.shutdown();
184             }
185         } catch (Exception JavaDoc 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