/
SpringApplicationShutdownHook.java
208 lines (176 loc) · 6.51 KB
/
SpringApplicationShutdownHook.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot;
import java.security.AccessControlException;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.util.Assert;
/**
* A {@link Runnable} to be used as a {@link Runtime#addShutdownHook(Thread) shutdown
* hook} to perform graceful shutdown of Spring Boot applications. This hook tracks
* registered application contexts as well as any actions registered via
* {@link SpringApplication#getShutdownHandlers()}.
*
* @author Andy Wilkinson
* @author Phillip Webb
*/
class SpringApplicationShutdownHook implements Runnable {
private static final int SLEEP = 50;
private static final long TIMEOUT = TimeUnit.MINUTES.toMillis(10);
private static final Log logger = LogFactory.getLog(SpringApplicationShutdownHook.class);
private final Handlers handlers = new Handlers();
private final Set<ConfigurableApplicationContext> contexts = new LinkedHashSet<>();
private final Set<ConfigurableApplicationContext> closedContexts = Collections.newSetFromMap(new WeakHashMap<>());
private final ApplicationContextClosedListener contextCloseListener = new ApplicationContextClosedListener();
private boolean inProgress;
SpringApplicationShutdownHook() {
try {
addRuntimeShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments
}
}
protected void addRuntimeShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(this, "SpringApplicationShutdownHook"));
}
SpringApplicationShutdownHandlers getHandlers() {
return this.handlers;
}
void registerApplicationContext(ConfigurableApplicationContext context) {
synchronized (SpringApplicationShutdownHook.class) {
assertNotInProgress();
context.addApplicationListener(this.contextCloseListener);
this.contexts.add(context);
}
}
@Override
public void run() {
Set<ConfigurableApplicationContext> contexts;
Set<ConfigurableApplicationContext> closedContexts;
Set<Runnable> actions;
synchronized (SpringApplicationShutdownHook.class) {
this.inProgress = true;
contexts = new LinkedHashSet<>(this.contexts);
closedContexts = new LinkedHashSet<>(this.closedContexts);
actions = new LinkedHashSet<>(this.handlers.getActions());
}
contexts.forEach(this::closeAndWait);
closedContexts.forEach(this::closeAndWait);
actions.forEach(Runnable::run);
}
boolean isApplicationContextRegistered(ConfigurableApplicationContext context) {
synchronized (SpringApplicationShutdownHook.class) {
return this.contexts.contains(context);
}
}
void reset() {
synchronized (SpringApplicationShutdownHook.class) {
this.contexts.clear();
this.closedContexts.clear();
this.handlers.getActions().clear();
this.inProgress = false;
}
}
/**
* Call {@link ConfigurableApplicationContext#close()} and wait until the context
* becomes inactive. We can't assume that just because the close method returns that
* the context is actually inactive. It could be that another thread is still in the
* process of disposing beans.
* @param context the context to clean
*/
private void closeAndWait(ConfigurableApplicationContext context) {
if (!context.isActive()) {
return;
}
context.close();
try {
int waited = 0;
while (context.isActive()) {
if (waited > TIMEOUT) {
throw new TimeoutException();
}
Thread.sleep(SLEEP);
waited += SLEEP;
}
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.warn("Interrupted waiting for application context " + context + " to become inactive");
}
catch (TimeoutException ex) {
logger.warn("Timed out waiting for application context " + context + " to become inactive", ex);
}
}
private void assertNotInProgress() {
Assert.state(!SpringApplicationShutdownHook.this.inProgress, "Shutdown in progress");
}
/**
* The handler actions for this shutdown hook.
*/
private class Handlers implements SpringApplicationShutdownHandlers {
private final Set<Runnable> actions = Collections.newSetFromMap(new IdentityHashMap<>());
@Override
public void add(Runnable action) {
Assert.notNull(action, "Action must not be null");
synchronized (SpringApplicationShutdownHook.class) {
assertNotInProgress();
this.actions.add(action);
}
}
@Override
public void remove(Runnable action) {
Assert.notNull(action, "Action must not be null");
synchronized (SpringApplicationShutdownHook.class) {
assertNotInProgress();
this.actions.remove(action);
}
}
Set<Runnable> getActions() {
return this.actions;
}
}
/**
* {@link ApplicationListener} to track closed contexts.
*/
private class ApplicationContextClosedListener implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
// The ContextClosedEvent is fired at the start of a call to {@code close()}
// and if that happens in a different thread then the context may still be
// active. Rather than just removing the context, we add it to a {@code
// closedContexts} set. This is weak set so that the context can be GC'd once
// the {@code close()} method returns.
synchronized (SpringApplicationShutdownHook.class) {
ApplicationContext applicationContext = event.getApplicationContext();
SpringApplicationShutdownHook.this.contexts.remove(applicationContext);
SpringApplicationShutdownHook.this.closedContexts
.add((ConfigurableApplicationContext) applicationContext);
}
}
}
}