Close resources in HttpComponents client connector

Prior to this commit, the `HttpComponentsClientHttpConnector`
implementation could accept or create a default `HttpClient` instance
but not expose it as part of its API. This effectively prevents
applications from properly closing the associated resources when
disposing of the connector.

This commit implements the `Closeable` interface on the connector to
allow this use case.

Closes gh-27032
This commit is contained in:
Brian Clozel 2021-06-08 11:22:30 +02:00
parent 3fa4e4168d
commit 05eca05671
1 changed files with 7 additions and 1 deletions

View File

@ -16,6 +16,8 @@
package org.springframework.http.client.reactive; package org.springframework.http.client.reactive;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@ -48,7 +50,7 @@ import org.springframework.util.Assert;
* @since 5.3 * @since 5.3
* @see <a href="https://hc.apache.org/index.html">Apache HttpComponents</a> * @see <a href="https://hc.apache.org/index.html">Apache HttpComponents</a>
*/ */
public class HttpComponentsClientHttpConnector implements ClientHttpConnector { public class HttpComponentsClientHttpConnector implements ClientHttpConnector, Closeable {
private final CloseableHttpAsyncClient client; private final CloseableHttpAsyncClient client;
@ -126,6 +128,10 @@ public class HttpComponentsClientHttpConnector implements ClientHttpConnector {
}); });
} }
@Override
public void close() throws IOException {
this.client.close();
}
private static class MonoFutureCallbackAdapter private static class MonoFutureCallbackAdapter
implements FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>> { implements FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>> {