|
40 | 40 |
|
41 | 41 | public class ShutdownNotifierComponent implements ShutdownNotifier { |
42 | 42 |
|
| 43 | + /** Monitor for listeners and shutdownCause */ |
| 44 | + private final Object monitor = new Object(); |
| 45 | + |
43 | 46 | /** List of all shutdown listeners associated with the component */ |
44 | | - public List<ShutdownListener> listeners |
45 | | - = new ArrayList<ShutdownListener>(); |
| 47 | + private final List<ShutdownListener> shutdownListeners |
| 48 | + = new ArrayList<ShutdownListener>(); |
46 | 49 |
|
47 | 50 | /** |
48 | 51 | * When this value is null, the component is in an "open" |
49 | 52 | * state. When non-null, the component is in "closed" state, and |
50 | 53 | * this value indicates the circumstances of the shutdown. |
51 | 54 | */ |
52 | | - public volatile ShutdownSignalException _shutdownCause = null; |
| 55 | + private volatile ShutdownSignalException shutdownCause = null; |
53 | 56 |
|
54 | 57 | public void addShutdownListener(ShutdownListener listener) |
55 | 58 | { |
56 | | - boolean closed = false; |
57 | | - synchronized(listeners) { |
58 | | - closed = !isOpen(); |
59 | | - listeners.add(listener); |
| 59 | + ShutdownSignalException sse = null; |
| 60 | + synchronized(this.monitor) { |
| 61 | + sse = this.shutdownCause; |
| 62 | + this.shutdownListeners.add(listener); |
60 | 63 | } |
61 | | - if (closed) |
62 | | - listener.shutdownCompleted(getCloseReason()); |
| 64 | + if (sse != null) // closed |
| 65 | + listener.shutdownCompleted(sse); |
63 | 66 | } |
64 | 67 |
|
65 | 68 | public ShutdownSignalException getCloseReason() { |
66 | | - return _shutdownCause; |
| 69 | + return this.shutdownCause; |
67 | 70 | } |
68 | 71 |
|
69 | 72 | public void notifyListeners() |
70 | 73 | { |
71 | | - synchronized(listeners) { |
72 | | - for (ShutdownListener l: listeners) |
73 | | - try { |
74 | | - l.shutdownCompleted(getCloseReason()); |
75 | | - } catch (Exception e) { |
76 | | - // FIXME: proper logging |
77 | | - } |
| 74 | + ShutdownSignalException sse = null; |
| 75 | + ShutdownListener[] sdls = null; |
| 76 | + synchronized(this.monitor) { |
| 77 | + sdls = this.shutdownListeners |
| 78 | + .toArray(new ShutdownListener[this.shutdownListeners.size()]); |
| 79 | + sse = this.shutdownCause; |
| 80 | + } |
| 81 | + for (ShutdownListener l: sdls) { |
| 82 | + try { |
| 83 | + l.shutdownCompleted(sse); |
| 84 | + } catch (Exception e) { |
| 85 | + // FIXME: proper logging |
| 86 | + } |
78 | 87 | } |
79 | 88 | } |
80 | 89 |
|
81 | 90 | public void removeShutdownListener(ShutdownListener listener) |
82 | 91 | { |
83 | | - synchronized(listeners) { |
84 | | - listeners.remove(listener); |
| 92 | + synchronized(this.monitor) { |
| 93 | + this.shutdownListeners.remove(listener); |
85 | 94 | } |
86 | 95 | } |
87 | 96 |
|
88 | 97 | public boolean isOpen() { |
89 | | - return _shutdownCause == null; |
| 98 | + return this.shutdownCause == null; |
90 | 99 | } |
91 | 100 |
|
| 101 | + public boolean setShutdownCauseIfOpen(ShutdownSignalException sse) { |
| 102 | + synchronized (this.monitor) { |
| 103 | + if (isOpen()) { |
| 104 | + this.shutdownCause = sse; |
| 105 | + return true; |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
92 | 110 | } |
0 commit comments