1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
27
27
import org .springframework .beans .factory .DisposableBean ;
28
28
import org .springframework .beans .factory .InitializingBean ;
29
+ import org .springframework .context .Lifecycle ;
29
30
import org .springframework .lang .Nullable ;
30
31
import org .springframework .util .Assert ;
31
32
34
35
* event loop threads, and {@link ConnectionProvider} for the connection pool,
35
36
* within the lifecycle of a Spring {@code ApplicationContext}.
36
37
*
37
- * <p>This factory implements {@link InitializingBean} and {@link DisposableBean}
38
- * and is expected typically to be declared as a Spring-managed bean.
38
+ * <p>This factory implements {@link InitializingBean}, {@link DisposableBean}
39
+ * and {@link Lifecycle} and is expected typically to be declared as a
40
+ * Spring-managed bean.
39
41
*
40
42
* @author Rossen Stoyanchev
41
43
* @author Brian Clozel
44
+ * @author Sebastien Deleuze
42
45
* @since 5.1
43
46
*/
44
- public class ReactorResourceFactory implements InitializingBean , DisposableBean {
47
+ public class ReactorResourceFactory implements InitializingBean , DisposableBean , Lifecycle {
45
48
46
49
private boolean useGlobalResources = true ;
47
50
@@ -66,6 +69,10 @@ public class ReactorResourceFactory implements InitializingBean, DisposableBean
66
69
67
70
private Duration shutdownTimeout = Duration .ofSeconds (LoopResources .DEFAULT_SHUTDOWN_TIMEOUT );
68
71
72
+ private volatile boolean running ;
73
+
74
+ private final Object lifecycleMonitor = new Object ();
75
+
69
76
70
77
/**
71
78
* Whether to use global Reactor Netty resources via {@link HttpResources}.
@@ -196,54 +203,84 @@ public void setShutdownTimeout(Duration shutdownTimeout) {
196
203
197
204
@ Override
198
205
public void afterPropertiesSet () {
199
- if (this .useGlobalResources ) {
200
- Assert .isTrue (this .loopResources == null && this .connectionProvider == null ,
201
- "'useGlobalResources' is mutually exclusive with explicitly configured resources" );
202
- HttpResources httpResources = HttpResources .get ();
203
- if (this .globalResourcesConsumer != null ) {
204
- this .globalResourcesConsumer .accept (httpResources );
205
- }
206
- this .connectionProvider = httpResources ;
207
- this .loopResources = httpResources ;
208
- }
209
- else {
210
- if (this .loopResources == null ) {
211
- this .manageLoopResources = true ;
212
- this .loopResources = this .loopResourcesSupplier .get ();
213
- }
214
- if (this .connectionProvider == null ) {
215
- this .manageConnectionProvider = true ;
216
- this .connectionProvider = this .connectionProviderSupplier .get ();
217
- }
218
- }
206
+ start ();
219
207
}
220
208
221
209
@ Override
222
210
public void destroy () {
223
- if (this .useGlobalResources ) {
224
- HttpResources .disposeLoopsAndConnectionsLater (this .shutdownQuietPeriod , this .shutdownTimeout ).block ();
225
- }
226
- else {
227
- try {
228
- ConnectionProvider provider = this .connectionProvider ;
229
- if (provider != null && this .manageConnectionProvider ) {
230
- provider .disposeLater ().block ();
211
+ stop ();
212
+ }
213
+
214
+ @ Override
215
+ public void start () {
216
+ synchronized (this .lifecycleMonitor ) {
217
+ if (!isRunning ()) {
218
+ if (this .useGlobalResources ) {
219
+ Assert .isTrue (this .loopResources == null && this .connectionProvider == null ,
220
+ "'useGlobalResources' is mutually exclusive with explicitly configured resources" );
221
+ HttpResources httpResources = HttpResources .get ();
222
+ if (this .globalResourcesConsumer != null ) {
223
+ this .globalResourcesConsumer .accept (httpResources );
224
+ }
225
+ this .connectionProvider = httpResources ;
226
+ this .loopResources = httpResources ;
231
227
}
228
+ else {
229
+ if (this .loopResources == null ) {
230
+ this .manageLoopResources = true ;
231
+ this .loopResources = this .loopResourcesSupplier .get ();
232
+ }
233
+ if (this .connectionProvider == null ) {
234
+ this .manageConnectionProvider = true ;
235
+ this .connectionProvider = this .connectionProviderSupplier .get ();
236
+ }
237
+ }
238
+ this .running = true ;
232
239
}
233
- catch (Throwable ex ) {
234
- // ignore
235
- }
240
+ }
236
241
237
- try {
238
- LoopResources resources = this .loopResources ;
239
- if (resources != null && this .manageLoopResources ) {
240
- resources .disposeLater (this .shutdownQuietPeriod , this .shutdownTimeout ).block ();
242
+ }
243
+
244
+ @ Override
245
+ public void stop () {
246
+ synchronized (this .lifecycleMonitor ) {
247
+ if (isRunning ()) {
248
+ if (this .useGlobalResources ) {
249
+ HttpResources .disposeLoopsAndConnectionsLater (this .shutdownQuietPeriod , this .shutdownTimeout ).block ();
250
+ this .connectionProvider = null ;
251
+ this .loopResources = null ;
241
252
}
242
- }
243
- catch (Throwable ex ) {
244
- // ignore
253
+ else {
254
+ try {
255
+ ConnectionProvider provider = this .connectionProvider ;
256
+ if (provider != null && this .manageConnectionProvider ) {
257
+ this .connectionProvider = null ;
258
+ provider .disposeLater ().block ();
259
+ }
260
+ }
261
+ catch (Throwable ex ) {
262
+ // ignore
263
+ }
264
+
265
+ try {
266
+ LoopResources resources = this .loopResources ;
267
+ if (resources != null && this .manageLoopResources ) {
268
+ this .loopResources = null ;
269
+ resources .disposeLater (this .shutdownQuietPeriod , this .shutdownTimeout ).block ();
270
+ }
271
+ }
272
+ catch (Throwable ex ) {
273
+ // ignore
274
+ }
275
+ }
276
+ this .running = false ;
245
277
}
246
278
}
247
279
}
248
280
281
+ @ Override
282
+ public boolean isRunning () {
283
+ return this .running ;
284
+ }
285
+
249
286
}
0 commit comments