01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.impl;
17:
18: import org.directwebremoting.extend.Alarm;
19: import org.directwebremoting.extend.ServerLoadMonitor;
20: import org.directwebremoting.extend.Sleeper;
21: import org.directwebremoting.extend.WaitController;
22:
23: /**
24: * An Alarm that allows the system to close all connections when it is shutting
25: * down.
26: * <p><b>WARNING</b>: This code has a non-obvious side effect - The server load
27: * monitor (which hands out shutdown messages) also monitors usage by looking
28: * at the number of connected alarms.
29: * @author Joe Walker [joe at getahead dot ltd dot uk]
30: */
31: public class ShutdownAlarm extends BasicAlarm implements Alarm {
32: /**
33: * Register ourselves with the ServerLoadMonitor so we can raise an
34: * Alarm if we get shutdown
35: * @param serverLoadMonitor
36: */
37: public ShutdownAlarm(ServerLoadMonitor serverLoadMonitor) {
38: this .serverLoadMonitor = serverLoadMonitor;
39: }
40:
41: /* (non-Javadoc)
42: * @see org.directwebremoting.dwrp.PollHandler.Alarm#setAlarmAction(org.directwebremoting.dwrp.PollHandler.Sleeper)
43: */
44: @Override
45: public void setAlarmAction(Sleeper sleeper) {
46: serverLoadMonitor.threadWaitStarting(waitController);
47: super .setAlarmAction(sleeper);
48: }
49:
50: /* (non-Javadoc)
51: * @see org.directwebremoting.dwrp.PollHandler.Alarm#cancel()
52: */
53: @Override
54: public void cancel() {
55: serverLoadMonitor.threadWaitEnding(waitController);
56: super .cancel();
57: }
58:
59: /**
60: * The listener to allow the ServerLoadMonitor to shut us down
61: */
62: private WaitController waitController = new WaitController() {
63: /* (non-Javadoc)
64: * @see org.directwebremoting.extend.WaitController#shutdown()
65: */
66: public void shutdown() {
67: raiseAlarm();
68: shutdown = true;
69: }
70:
71: /* (non-Javadoc)
72: * @see org.directwebremoting.extend.WaitController#isShutdown()
73: */
74: public boolean isShutdown() {
75: return shutdown;
76: }
77:
78: /**
79: * We neeed to be able to say if we have been shutdown
80: */
81: boolean shutdown = false;
82: };
83:
84: /**
85: * The source of shutdown messages
86: */
87: protected ServerLoadMonitor serverLoadMonitor;
88: }
|