Browse Source

Merge branch '6.1.x'

pull/32969/head
Juergen Hoeller 2 years ago
parent
commit
09c1081645
  1. 11
      spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java
  2. 38
      spring-web/src/main/java/org/springframework/http/client/ReactorResourceFactory.java
  3. 6
      spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ResourceFactory.java
  4. 18
      spring-web/src/test/java/org/springframework/http/client/ReactorResourceFactoryTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,6 +73,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
private long connectionRequestTimeout = -1; private long connectionRequestTimeout = -1;
/** /**
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} * Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
* with a default {@link HttpClient} based on system properties. * with a default {@link HttpClient} based on system properties.
@ -202,6 +203,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
this.httpContextFactory = httpContextFactory; this.httpContextFactory = httpContextFactory;
} }
@Override @Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpClient client = getHttpClient(); HttpClient client = getHttpClient();
@ -309,8 +311,8 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
} }
/** /**
* Template method that allows for manipulating the {@link ClassicHttpRequest} before it is * Template method that allows for manipulating the {@link ClassicHttpRequest}
* returned as part of a {@link HttpComponentsClientHttpRequest}. * before it is returned as part of a {@link HttpComponentsClientHttpRequest}.
* <p>The default implementation is empty. * <p>The default implementation is empty.
* @param request the request to process * @param request the request to process
*/ */
@ -331,8 +333,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
/** /**
* Shutdown hook that closes the underlying * Shutdown hook that closes the underlying {@link HttpClientConnectionManager}'s
* {@link HttpClientConnectionManager ClientConnectionManager}'s
* connection pool, if any. * connection pool, if any.
*/ */
@Override @Override

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

@ -61,12 +61,12 @@ public class ReactorResourceFactory
private Supplier<ConnectionProvider> connectionProviderSupplier = () -> ConnectionProvider.create("webflux", 500); private Supplier<ConnectionProvider> connectionProviderSupplier = () -> ConnectionProvider.create("webflux", 500);
@Nullable @Nullable
private ConnectionProvider connectionProvider; private volatile ConnectionProvider connectionProvider;
private Supplier<LoopResources> loopResourcesSupplier = () -> LoopResources.create("webflux-http"); private Supplier<LoopResources> loopResourcesSupplier = () -> LoopResources.create("webflux-http");
@Nullable @Nullable
private LoopResources loopResources; private volatile LoopResources loopResources;
private boolean manageConnectionProvider = false; private boolean manageConnectionProvider = false;
@ -141,16 +141,22 @@ public class ReactorResourceFactory
/** /**
* Return the configured {@link ConnectionProvider}. * Return the configured {@link ConnectionProvider}.
* <p>Lazily tries to start the resources on demand if not initialized yet.
* @see #start()
*/ */
public ConnectionProvider getConnectionProvider() { public ConnectionProvider getConnectionProvider() {
Assert.state(this.connectionProvider != null, "ConnectionProvider not initialized yet"); if (this.connectionProvider == null) {
return this.connectionProvider; start();
}
ConnectionProvider connectionProvider = this.connectionProvider;
Assert.state(connectionProvider != null, "ConnectionProvider not initialized");
return connectionProvider;
} }
/** /**
* Use this when you don't want to participate in global resources and * Use this when you don't want to participate in global resources and
* you want to customize the creation of the managed {@code LoopResources}. * you want to customize the creation of the managed {@code LoopResources}.
* <p>By default, {@code LoopResources.create("reactor-http")} is used. * <p>By default, {@code LoopResources.create("webflux-http")} is used.
* <p>Note that this option is ignored if {@code userGlobalResources=false} or * <p>Note that this option is ignored if {@code userGlobalResources=false} or
* {@link #setLoopResources(LoopResources)} is set. * {@link #setLoopResources(LoopResources)} is set.
* @param supplier the supplier to use * @param supplier the supplier to use
@ -170,10 +176,16 @@ public class ReactorResourceFactory
/** /**
* Return the configured {@link LoopResources}. * Return the configured {@link LoopResources}.
* <p>Lazily tries to start the resources on demand if not initialized yet.
* @see #start()
*/ */
public LoopResources getLoopResources() { public LoopResources getLoopResources() {
Assert.state(this.loopResources != null, "LoopResources not initialized yet"); if (this.loopResources == null) {
return this.loopResources; start();
}
LoopResources loopResources = this.loopResources;
Assert.state(loopResources != null, "LoopResources not initialized");
return loopResources;
} }
/** /**
@ -220,6 +232,12 @@ public class ReactorResourceFactory
} }
/**
* Starts the resources if initialized outside an ApplicationContext.
* This is for backwards compatibility; the preferred way is to rely on
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
* @see #start()
*/
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
if (this.applicationContext == null) { if (this.applicationContext == null) {
@ -227,6 +245,12 @@ public class ReactorResourceFactory
} }
} }
/**
* Stops the resources if initialized outside an ApplicationContext.
* This is for backwards compatibility; the preferred way is to rely on
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
* @see #stop()
*/
@Override @Override
public void destroy() { public void destroy() {
if (this.applicationContext == null) { if (this.applicationContext == null) {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -135,7 +135,7 @@ public class ReactorNetty2ResourceFactory implements InitializingBean, Disposabl
/** /**
* Use this when you don't want to participate in global resources and * Use this when you don't want to participate in global resources and
* you want to customize the creation of the managed {@code LoopResources}. * you want to customize the creation of the managed {@code LoopResources}.
* <p>By default, {@code LoopResources.create("reactor-http")} is used. * <p>By default, {@code LoopResources.create("webflux-http")} is used.
* <p>Note that this option is ignored if {@code userGlobalResources=false} or * <p>Note that this option is ignored if {@code userGlobalResources=false} or
* {@link #setLoopResources(LoopResources)} is set. * {@link #setLoopResources(LoopResources)} is set.
* @param supplier the supplier to use * @param supplier the supplier to use
@ -170,7 +170,6 @@ public class ReactorNetty2ResourceFactory implements InitializingBean, Disposabl
* can also be overridden with the system property * can also be overridden with the system property
* {@link reactor.netty5.ReactorNetty#SHUTDOWN_QUIET_PERIOD * {@link reactor.netty5.ReactorNetty#SHUTDOWN_QUIET_PERIOD
* ReactorNetty.SHUTDOWN_QUIET_PERIOD}. * ReactorNetty.SHUTDOWN_QUIET_PERIOD}.
* @since 5.2.4
* @see #setShutdownTimeout(Duration) * @see #setShutdownTimeout(Duration)
*/ */
public void setShutdownQuietPeriod(Duration shutdownQuietPeriod) { public void setShutdownQuietPeriod(Duration shutdownQuietPeriod) {
@ -187,7 +186,6 @@ public class ReactorNetty2ResourceFactory implements InitializingBean, Disposabl
* can also be overridden with the system property * can also be overridden with the system property
* {@link reactor.netty5.ReactorNetty#SHUTDOWN_TIMEOUT * {@link reactor.netty5.ReactorNetty#SHUTDOWN_TIMEOUT
* ReactorNetty.SHUTDOWN_TIMEOUT}. * ReactorNetty.SHUTDOWN_TIMEOUT}.
* @since 5.2.4
* @see #setShutdownQuietPeriod(Duration) * @see #setShutdownQuietPeriod(Duration)
*/ */
public void setShutdownTimeout(Duration shutdownTimeout) { public void setShutdownTimeout(Duration shutdownTimeout) {

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

@ -261,4 +261,22 @@ class ReactorResourceFactoryTests {
assertThat(resourceFactory.isRunning()).isFalse(); assertThat(resourceFactory.isRunning()).isFalse();
} }
@Test
void lazilyStartOnConnectionProviderAccess() {
assertThat(this.resourceFactory.isRunning()).isFalse();
this.resourceFactory.getConnectionProvider();
assertThat(this.resourceFactory.isRunning()).isTrue();
this.resourceFactory.stop();
assertThat(this.resourceFactory.isRunning()).isFalse();
}
@Test
void lazilyStartOnLoopResourcesAccess() {
assertThat(this.resourceFactory.isRunning()).isFalse();
this.resourceFactory.getLoopResources();
assertThat(this.resourceFactory.isRunning()).isTrue();
this.resourceFactory.stop();
assertThat(this.resourceFactory.isRunning()).isFalse();
}
} }

Loading…
Cancel
Save