KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > QuartzServer


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.impl;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import org.quartz.Scheduler;
27 import org.quartz.SchedulerException;
28 import org.quartz.SchedulerFactory;
29 import org.quartz.listeners.SchedulerListenerSupport;
30
31 /**
32  * <p>
33  * Instantiates an instance of Quartz Scheduler as a stand-alone program, if
34  * the scheduler is configured for RMI it will be made available.
35  * </p>
36  *
37  * <p>
38  * The main() method of this class currently accepts 0 or 1 arguemtns, if there
39  * is an argument, and its value is <code>"console"</code>, then the program
40  * will print a short message on the console (std-out) and wait for the user to
41  * type "exit" - at which time the scheduler will be shutdown.
42  * </p>
43  *
44  * <p>
45  * Future versions of this server should allow additional configuration for
46  * responding to scheduler events by allowing the user to specify <code>{@link org.quartz.JobListener}</code>,
47  * <code>{@link org.quartz.TriggerListener}</code> and <code>{@link org.quartz.SchedulerListener}</code>
48  * classes.
49  * </p>
50  *
51  * <p>
52  * Please read the Quartz FAQ entries about RMI before asking questions in the
53  * forums or mail-lists.
54  * </p>
55  *
56  * @author James House
57  */

58 public class QuartzServer extends SchedulerListenerSupport {
59
60     /*
61      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62      *
63      * Data members.
64      *
65      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66      */

67
68     private Scheduler sched = null;
69
70     /*
71      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72      *
73      * Constructors.
74      *
75      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76      */

77
78     QuartzServer() {
79     }
80
81     /*
82      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83      *
84      * Interface.
85      *
86      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87      */

88
89     public void serve(SchedulerFactory schedFact, boolean console)
90         throws Exception JavaDoc {
91         sched = schedFact.getScheduler();
92
93         sched.start();
94
95         try {
96             Thread.sleep(3000l);
97         } catch (Exception JavaDoc ignore) {
98         }
99
100         System.out.println("\n*** The scheduler successfully started.");
101
102         if (console) {
103             System.out.println("\n");
104             System.out
105                     .println("The scheduler will now run until you type \"exit\"");
106             System.out
107                     .println(" If it was configured to export itself via RMI,");
108             System.out.println(" then other process may now use it.");
109
110             BufferedReader JavaDoc rdr = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
111                     System.in));
112
113             while (true) {
114                 System.out.print("Type 'exit' to shutdown the server: ");
115                 if ("exit".equals(rdr.readLine())) {
116                     break;
117                 }
118             }
119
120             System.out.println("\n...Shutting down server...");
121
122             sched.shutdown(true);
123         }
124     }
125
126     /*
127      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128      *
129      * SchedulerListener Interface.
130      *
131      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132      */

133
134     /**
135      * <p>
136      * Called by the <code>{@link Scheduler}</code> when a serious error has
137      * occured within the scheduler - such as repeated failures in the <code>JobStore</code>,
138      * or the inability to instantiate a <code>Job</code> instance when its
139      * <code>Trigger</code> has fired.
140      * </p>
141      *
142      * <p>
143      * The <code>getErrorCode()</code> method of the given SchedulerException
144      * can be used to determine more specific information about the type of
145      * error that was encountered.
146      * </p>
147      */

148     public void schedulerError(String JavaDoc msg, SchedulerException cause) {
149         System.err.println("*** " + msg);
150         cause.printStackTrace();
151     }
152
153     /**
154      * <p>
155      * Called by the <code>{@link Scheduler}</code> to inform the listener
156      * that it has shutdown.
157      * </p>
158      */

159     public void schedulerShutdown() {
160         System.out.println("\n*** The scheduler is now shutdown.");
161         sched = null;
162     }
163
164     /*
165      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166      *
167      * Main Method.
168      *
169      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170      */

171
172     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
173
174         // //Configure Log4J
175
// org.apache.log4j.PropertyConfigurator.configure(
176
// System.getProperty("log4jConfigFile", "log4j.properties"));
177

178         if (System.getSecurityManager() == null) {
179             System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
180         }
181
182         try {
183             QuartzServer server = new QuartzServer();
184             if (args.length == 0) {
185                 server.serve(
186                     new org.quartz.impl.StdSchedulerFactory(), false);
187             } else if (args.length == 1 && args[0].equalsIgnoreCase("console")) {
188                 server.serve(new org.quartz.impl.StdSchedulerFactory(), true);
189             } else {
190                 System.err.println("\nUsage: QuartzServer [console]");
191             }
192         } catch (Exception JavaDoc e) {
193             e.printStackTrace();
194         }
195     }
196
197 }
198
Popular Tags