This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StaticShutdownCallbackRegistry.java
137 lines (118 loc) · 4.12 KB
/
StaticShutdownCallbackRegistry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.djdch.log4j;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LifeCycle;
import org.apache.logging.log4j.core.util.Cancellable;
import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
import org.apache.logging.log4j.status.StatusLogger;
/**
* ShutdownRegistrationStrategy designed to be manually invoked. If not invoked, log4j will not shutdown properly.
*
* @see org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry
*/
public class StaticShutdownCallbackRegistry implements ShutdownCallbackRegistry, LifeCycle, Runnable, Serializable {
private static final long serialVersionUID = 1L;
protected static final Logger LOGGER = StatusLogger.getLogger();
private final AtomicReference<State> state = new AtomicReference<State>(State.INITIALIZED);
private final Collection<Cancellable> hooks = new CopyOnWriteArrayList<Cancellable>();
private static final Collection<StaticShutdownCallbackRegistry> instances = new CopyOnWriteArrayList<StaticShutdownCallbackRegistry>();
/**
* Constructs a DelayedShutdownCallbackRegistry.
*/
public StaticShutdownCallbackRegistry() {
instances.add(this);
}
/**
* Invoke all ShutdownCallbackRegistry instances.
*/
public static void invoke() {
for (final Runnable instance : instances) {
try {
instance.run();
} catch (final Throwable t) {
LOGGER.error(SHUTDOWN_HOOK_MARKER, "Caught exception executing shutdown hook {}", instance, t);
}
}
}
/**
* Executes the registered shutdown callbacks.
*/
@Override
public void run() {
if (state.compareAndSet(State.STARTED, State.STOPPING)) {
for (final Runnable hook : hooks) {
try {
hook.run();
} catch (final Throwable t) {
LOGGER.error(SHUTDOWN_HOOK_MARKER, "Caught exception executing shutdown hook {}", hook, t);
}
}
state.set(State.STOPPED);
}
}
@Override
public Cancellable addShutdownCallback(final Runnable callback) {
if (isStarted()) {
final Cancellable receipt = new Cancellable() {
// use a reference to prevent memory leaks
private final Reference<Runnable> hook = new SoftReference<Runnable>(callback);
@Override
public void cancel() {
hook.clear();
hooks.remove(this);
}
@Override
public void run() {
final Runnable hook = this.hook.get();
if (hook != null) {
hook.run();
this.hook.clear();
}
}
@Override
public String toString() {
return String.valueOf(hook.get());
}
};
hooks.add(receipt);
return receipt;
}
throw new IllegalStateException("Cannot add new shutdown hook as this is not started. Current state: " +
state.get().name());
}
/**
* Registers the shutdown thread only if this is initialized.
*/
@Override
public void start() {
state.set(State.STARTED);
}
/**
* Cancels the shutdown thread only if this is started.
*/
@Override
public void stop() {
state.set(State.STOPPED);
}
public State getState() {
return state.get();
}
/**
* Indicates if this can accept shutdown hooks.
*
* @return true if this can accept shutdown hooks
*/
@Override
public boolean isStarted() {
return state.get() == State.STARTED;
}
@Override
public boolean isStopped() {
return state.get() == State.STOPPED;
}
}