Skip to content

Commit 09c1081

Browse files
committed
Merge branch '6.1.x'
2 parents 5706ee1 + 61d045c commit 09c1081

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,6 +73,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
7373

7474
private long connectionRequestTimeout = -1;
7575

76+
7677
/**
7778
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
7879
* with a default {@link HttpClient} based on system properties.
@@ -202,6 +203,7 @@ public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpC
202203
this.httpContextFactory = httpContextFactory;
203204
}
204205

206+
205207
@Override
206208
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
207209
HttpClient client = getHttpClient();
@@ -309,8 +311,8 @@ else if (HttpMethod.TRACE.equals(httpMethod)) {
309311
}
310312

311313
/**
312-
* Template method that allows for manipulating the {@link ClassicHttpRequest} before it is
313-
* returned as part of a {@link HttpComponentsClientHttpRequest}.
314+
* Template method that allows for manipulating the {@link ClassicHttpRequest}
315+
* before it is returned as part of a {@link HttpComponentsClientHttpRequest}.
314316
* <p>The default implementation is empty.
315317
* @param request the request to process
316318
*/
@@ -331,8 +333,7 @@ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
331333

332334

333335
/**
334-
* Shutdown hook that closes the underlying
335-
* {@link HttpClientConnectionManager ClientConnectionManager}'s
336+
* Shutdown hook that closes the underlying {@link HttpClientConnectionManager}'s
336337
* connection pool, if any.
337338
*/
338339
@Override

spring-web/src/main/java/org/springframework/http/client/ReactorResourceFactory.java

+31-7
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public class ReactorResourceFactory
6161
private Supplier<ConnectionProvider> connectionProviderSupplier = () -> ConnectionProvider.create("webflux", 500);
6262

6363
@Nullable
64-
private ConnectionProvider connectionProvider;
64+
private volatile ConnectionProvider connectionProvider;
6565

6666
private Supplier<LoopResources> loopResourcesSupplier = () -> LoopResources.create("webflux-http");
6767

6868
@Nullable
69-
private LoopResources loopResources;
69+
private volatile LoopResources loopResources;
7070

7171
private boolean manageConnectionProvider = false;
7272

@@ -141,16 +141,22 @@ public void setConnectionProvider(ConnectionProvider connectionProvider) {
141141

142142
/**
143143
* Return the configured {@link ConnectionProvider}.
144+
* <p>Lazily tries to start the resources on demand if not initialized yet.
145+
* @see #start()
144146
*/
145147
public ConnectionProvider getConnectionProvider() {
146-
Assert.state(this.connectionProvider != null, "ConnectionProvider not initialized yet");
147-
return this.connectionProvider;
148+
if (this.connectionProvider == null) {
149+
start();
150+
}
151+
ConnectionProvider connectionProvider = this.connectionProvider;
152+
Assert.state(connectionProvider != null, "ConnectionProvider not initialized");
153+
return connectionProvider;
148154
}
149155

150156
/**
151157
* Use this when you don't want to participate in global resources and
152158
* you want to customize the creation of the managed {@code LoopResources}.
153-
* <p>By default, {@code LoopResources.create("reactor-http")} is used.
159+
* <p>By default, {@code LoopResources.create("webflux-http")} is used.
154160
* <p>Note that this option is ignored if {@code userGlobalResources=false} or
155161
* {@link #setLoopResources(LoopResources)} is set.
156162
* @param supplier the supplier to use
@@ -170,10 +176,16 @@ public void setLoopResources(LoopResources loopResources) {
170176

171177
/**
172178
* Return the configured {@link LoopResources}.
179+
* <p>Lazily tries to start the resources on demand if not initialized yet.
180+
* @see #start()
173181
*/
174182
public LoopResources getLoopResources() {
175-
Assert.state(this.loopResources != null, "LoopResources not initialized yet");
176-
return this.loopResources;
183+
if (this.loopResources == null) {
184+
start();
185+
}
186+
LoopResources loopResources = this.loopResources;
187+
Assert.state(loopResources != null, "LoopResources not initialized");
188+
return loopResources;
177189
}
178190

179191
/**
@@ -220,13 +232,25 @@ public void setApplicationContext(ApplicationContext applicationContext) {
220232
}
221233

222234

235+
/**
236+
* Starts the resources if initialized outside an ApplicationContext.
237+
* This is for backwards compatibility; the preferred way is to rely on
238+
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
239+
* @see #start()
240+
*/
223241
@Override
224242
public void afterPropertiesSet() {
225243
if (this.applicationContext == null) {
226244
start();
227245
}
228246
}
229247

248+
/**
249+
* Stops the resources if initialized outside an ApplicationContext.
250+
* This is for backwards compatibility; the preferred way is to rely on
251+
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
252+
* @see #stop()
253+
*/
230254
@Override
231255
public void destroy() {
232256
if (this.applicationContext == null) {

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ResourceFactory.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,7 +135,7 @@ public ConnectionProvider getConnectionProvider() {
135135
/**
136136
* Use this when you don't want to participate in global resources and
137137
* you want to customize the creation of the managed {@code LoopResources}.
138-
* <p>By default, {@code LoopResources.create("reactor-http")} is used.
138+
* <p>By default, {@code LoopResources.create("webflux-http")} is used.
139139
* <p>Note that this option is ignored if {@code userGlobalResources=false} or
140140
* {@link #setLoopResources(LoopResources)} is set.
141141
* @param supplier the supplier to use
@@ -170,7 +170,6 @@ public LoopResources getLoopResources() {
170170
* can also be overridden with the system property
171171
* {@link reactor.netty5.ReactorNetty#SHUTDOWN_QUIET_PERIOD
172172
* ReactorNetty.SHUTDOWN_QUIET_PERIOD}.
173-
* @since 5.2.4
174173
* @see #setShutdownTimeout(Duration)
175174
*/
176175
public void setShutdownQuietPeriod(Duration shutdownQuietPeriod) {
@@ -187,7 +186,6 @@ public void setShutdownQuietPeriod(Duration shutdownQuietPeriod) {
187186
* can also be overridden with the system property
188187
* {@link reactor.netty5.ReactorNetty#SHUTDOWN_TIMEOUT
189188
* ReactorNetty.SHUTDOWN_TIMEOUT}.
190-
* @since 5.2.4
191189
* @see #setShutdownQuietPeriod(Duration)
192190
*/
193191
public void setShutdownTimeout(Duration shutdownTimeout) {

spring-web/src/test/java/org/springframework/http/client/ReactorResourceFactoryTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,22 @@ protected void finishRefresh() {
261261
assertThat(resourceFactory.isRunning()).isFalse();
262262
}
263263

264+
@Test
265+
void lazilyStartOnConnectionProviderAccess() {
266+
assertThat(this.resourceFactory.isRunning()).isFalse();
267+
this.resourceFactory.getConnectionProvider();
268+
assertThat(this.resourceFactory.isRunning()).isTrue();
269+
this.resourceFactory.stop();
270+
assertThat(this.resourceFactory.isRunning()).isFalse();
271+
}
272+
273+
@Test
274+
void lazilyStartOnLoopResourcesAccess() {
275+
assertThat(this.resourceFactory.isRunning()).isFalse();
276+
this.resourceFactory.getLoopResources();
277+
assertThat(this.resourceFactory.isRunning()).isTrue();
278+
this.resourceFactory.stop();
279+
assertThat(this.resourceFactory.isRunning()).isFalse();
280+
}
281+
264282
}

0 commit comments

Comments
 (0)