KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > plugins > management > ShutdownHookPlugin


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.plugins.management;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.quartz.Scheduler;
26 import org.quartz.SchedulerException;
27 import org.quartz.spi.SchedulerPlugin;
28
29 /**
30  * This plugin catches the event of the JVM terminating (such as upon a CRTL-C)
31  * and tells the scheuler to shutdown.
32  *
33  * @see org.quartz.Scheduler#shutdown(boolean)
34  *
35  * @author James House
36  */

37 public class ShutdownHookPlugin implements SchedulerPlugin {
38
39     /*
40      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41      *
42      * Data members.
43      *
44      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45      */

46
47     private String JavaDoc name;
48
49     private Scheduler scheduler;
50
51     private boolean cleanShutdown = true;
52
53     private final Log log = LogFactory.getLog(getClass());
54     
55     /*
56      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57      *
58      * Constructors.
59      *
60      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61      */

62
63     public ShutdownHookPlugin() {
64     }
65
66     /*
67      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68      *
69      * Interface.
70      *
71      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72      */

73
74     /**
75      * Determine whether or not the plug-in is configured to cause a clean
76      * shutdown of the scheduler.
77      *
78      * <p>
79      * The default value is <code>true</code>.
80      * </p>
81      *
82      * @see org.quartz.Scheduler#shutdown(boolean)
83      */

84     public boolean isCleanShutdown() {
85         return cleanShutdown;
86     }
87
88     /**
89      * Set whether or not the plug-in is configured to cause a clean shutdown
90      * of the scheduler.
91      *
92      * <p>
93      * The default value is <code>true</code>.
94      * </p>
95      *
96      * @see org.quartz.Scheduler#shutdown(boolean)
97      */

98     public void setCleanShutdown(boolean b) {
99         cleanShutdown = b;
100     }
101
102     protected Log getLog() {
103         return log;
104     }
105
106     /*
107      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108      *
109      * SchedulerPlugin Interface.
110      *
111      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112      */

113
114     /**
115      * <p>
116      * Called during creation of the <code>Scheduler</code> in order to give
117      * the <code>SchedulerPlugin</code> a chance to initialize.
118      * </p>
119      *
120      * @throws SchedulerConfigException
121      * if there is an error initializing.
122      */

123     public void initialize(String JavaDoc name, final Scheduler scheduler)
124         throws SchedulerException {
125         this.name = name;
126         this.scheduler = scheduler;
127
128         getLog().info("Registering Quartz shutdown hook.");
129
130         Thread JavaDoc t = new Thread JavaDoc("Quartz Shutdown-Hook "
131                 + scheduler.getSchedulerName()) {
132             public void run() {
133                 getLog().info("Shutting down Quartz...");
134                 try {
135                     scheduler.shutdown(isCleanShutdown());
136                 } catch (SchedulerException e) {
137                     getLog().info(
138                             "Error shutting down Quartz: " + e.getMessage(), e);
139                 }
140             }
141         };
142
143         Runtime.getRuntime().addShutdownHook(t);
144     }
145
146     public void start() {
147         // do nothing.
148
}
149
150     /**
151      * <p>
152      * Called in order to inform the <code>SchedulerPlugin</code> that it
153      * should free up all of it's resources because the scheduler is shutting
154      * down.
155      * </p>
156      */

157     public void shutdown() {
158         // nothing to do in this case (since the scheduler is already shutting
159
// down)
160
}
161
162 }
163
164 // EOF
165
Popular Tags