CloseableHttpClient and CloseableHttpAsyncClient to implement ModalClosable
This commit is contained in:
parent
d929456278
commit
39cec46715
|
@ -99,7 +99,7 @@ public abstract class AbstractHttp1IntegrationTestBase extends AbstractServerTes
|
|||
@Override
|
||||
protected void after() {
|
||||
if (httpclient != null) {
|
||||
httpclient.shutdown(CloseMode.GRACEFUL);
|
||||
httpclient.close(CloseMode.GRACEFUL);
|
||||
httpclient = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public abstract class AbstractIntegrationTestBase<T extends CloseableHttpAsyncCl
|
|||
@Override
|
||||
protected void after() {
|
||||
if (httpclient != null) {
|
||||
httpclient.shutdown(CloseMode.GRACEFUL);
|
||||
httpclient.close(CloseMode.GRACEFUL);
|
||||
httpclient = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -112,19 +112,23 @@ abstract class AbstractHttpAsyncClientBase extends CloseableHttpAsyncClient {
|
|||
ioReactor.initiateShutdown();
|
||||
}
|
||||
|
||||
void internalClose(final CloseMode closeMode) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void shutdown(final CloseMode closeMode) {
|
||||
public final void close(final CloseMode closeMode) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Shutdown " + closeMode);
|
||||
}
|
||||
ioReactor.initiateShutdown();
|
||||
ioReactor.close(closeMode);
|
||||
executorService.shutdownNow();
|
||||
internalClose(closeMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
shutdown(CloseMode.GRACEFUL);
|
||||
close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
*/
|
||||
package org.apache.hc.client5.http.impl.async;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.hc.client5.http.async.HttpAsyncClient;
|
||||
|
@ -45,17 +44,18 @@ import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
|||
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.io.ModalCloseable;
|
||||
import org.apache.hc.core5.reactor.IOReactorStatus;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
import org.apache.hc.core5.util.TimeValue;
|
||||
|
||||
/**
|
||||
* Base implementation of {@link HttpAsyncClient} that also implements {@link Closeable}.
|
||||
* Base implementation of {@link HttpAsyncClient} that also implements {@link ModalCloseable}.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
@Contract(threading = ThreadingBehavior.STATELESS)
|
||||
public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Closeable {
|
||||
public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, ModalCloseable {
|
||||
|
||||
public abstract void start();
|
||||
|
||||
|
@ -65,7 +65,13 @@ public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Close
|
|||
|
||||
public abstract void initiateShutdown();
|
||||
|
||||
public abstract void shutdown(CloseMode closeMode);
|
||||
/**
|
||||
* @deprecated Use {@link #close(CloseMode)}
|
||||
*/
|
||||
@Deprecated
|
||||
public void shutdown(final CloseMode closeMode) {
|
||||
close(closeMode);
|
||||
}
|
||||
|
||||
public final <T> Future<T> execute(
|
||||
final AsyncRequestProducer requestProducer,
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.io.Closeable;
|
|||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
@ -64,6 +65,8 @@ import org.apache.hc.core5.http.nio.DataStreamChannel;
|
|||
import org.apache.hc.core5.http.nio.HandlerFactory;
|
||||
import org.apache.hc.core5.http.nio.RequestChannel;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.io.ModalCloseable;
|
||||
import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
|
||||
|
||||
abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBase {
|
||||
|
@ -74,7 +77,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
|
|||
private final CookieStore cookieStore;
|
||||
private final CredentialsProvider credentialsProvider;
|
||||
private final RequestConfig defaultConfig;
|
||||
private final List<Closeable> closeables;
|
||||
private final ConcurrentLinkedQueue<Closeable> closeables;
|
||||
|
||||
InternalAbstractHttpAsyncClient(
|
||||
final DefaultConnectingIOReactor ioReactor,
|
||||
|
@ -94,18 +97,22 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
|
|||
this.cookieStore = cookieStore;
|
||||
this.credentialsProvider = credentialsProvider;
|
||||
this.defaultConfig = defaultConfig;
|
||||
this.closeables = closeables;
|
||||
this.closeables = closeables != null ? new ConcurrentLinkedQueue<>(closeables) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
if (closeables != null) {
|
||||
for (final Closeable closeable: closeables) {
|
||||
void internalClose(final CloseMode closeMode) {
|
||||
if (this.closeables != null) {
|
||||
Closeable closeable;
|
||||
while ((closeable = this.closeables.poll()) != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
if (closeable instanceof ModalCloseable) {
|
||||
((ModalCloseable) closeable).close(closeMode);
|
||||
} else {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (final IOException ex) {
|
||||
log.error(ex.getMessage(), ex);
|
||||
this.log.error(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
|
||||
package org.apache.hc.client5.http.impl.classic;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
@ -44,17 +43,18 @@ import org.apache.hc.core5.http.HttpHost;
|
|||
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.io.ModalCloseable;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Base implementation of {@link HttpClient} that also implements {@link Closeable}.
|
||||
* Base implementation of {@link HttpClient} that also implements {@link ModalCloseable}.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
@Contract(threading = ThreadingBehavior.SAFE)
|
||||
public abstract class CloseableHttpClient implements HttpClient, Closeable {
|
||||
public abstract class CloseableHttpClient implements HttpClient, ModalCloseable {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ package org.apache.hc.client5.http.impl.classic;
|
|||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.HttpRoute;
|
||||
|
@ -59,6 +60,8 @@ import org.apache.hc.core5.http.config.Lookup;
|
|||
import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.io.ModalCloseable;
|
||||
import org.apache.hc.core5.net.URIAuthority;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -88,7 +91,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
|
|||
private final CookieStore cookieStore;
|
||||
private final CredentialsProvider credentialsProvider;
|
||||
private final RequestConfig defaultConfig;
|
||||
private final List<Closeable> closeables;
|
||||
private final ConcurrentLinkedQueue<Closeable> closeables;
|
||||
|
||||
public InternalHttpClient(
|
||||
final HttpClientConnectionManager connManager,
|
||||
|
@ -111,7 +114,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
|
|||
this.cookieStore = cookieStore;
|
||||
this.credentialsProvider = credentialsProvider;
|
||||
this.defaultConfig = defaultConfig;
|
||||
this.closeables = closeables;
|
||||
this.closeables = closeables != null ? new ConcurrentLinkedQueue<>(closeables) : null;
|
||||
}
|
||||
|
||||
private HttpRoute determineRoute(
|
||||
|
@ -186,10 +189,20 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
|
|||
|
||||
@Override
|
||||
public void close() {
|
||||
close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(final CloseMode closeMode) {
|
||||
if (this.closeables != null) {
|
||||
for (final Closeable closeable: this.closeables) {
|
||||
Closeable closeable;
|
||||
while ((closeable = this.closeables.poll()) != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
if (closeable instanceof ModalCloseable) {
|
||||
((ModalCloseable) closeable).close(closeMode);
|
||||
} else {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (final IOException ex) {
|
||||
this.log.error(ex.getMessage(), ex);
|
||||
}
|
||||
|
|
|
@ -186,4 +186,9 @@ public class MinimalHttpClient extends CloseableHttpClient {
|
|||
this.connManager.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(final CloseMode closeMode) {
|
||||
this.connManager.close(closeMode);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ public class AsyncClientAuthentication {
|
|||
future.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
httpclient.shutdown(CloseMode.GRACEFUL);
|
||||
httpclient.close(CloseMode.GRACEFUL);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,7 +121,7 @@ public class AsyncClientConnectionEviction {
|
|||
future2.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -133,7 +133,7 @@ public class AsyncClientCustomSSL {
|
|||
future.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -151,7 +151,7 @@ public class AsyncClientFullDuplexExchange {
|
|||
latch.await(1, TimeUnit.MINUTES);
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -147,7 +147,7 @@ public class AsyncClientH2FullDuplexExchange {
|
|||
latch.await(1, TimeUnit.MINUTES);
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ public class AsyncClientH2Multiplexing {
|
|||
}
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -155,7 +155,7 @@ public class AsyncClientH2ServerPush {
|
|||
future.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -103,7 +103,7 @@ public class AsyncClientHttp1Pipelining {
|
|||
}
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ public class AsyncClientHttpExchange {
|
|||
}
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ public class AsyncClientHttpExchangeStreaming {
|
|||
}
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ public class AsyncClientInterceptors {
|
|||
}
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ public class AsyncClientMessageTrailers {
|
|||
future.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ public class AsyncClientTlsAlpn {
|
|||
future.get();
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -111,6 +111,6 @@ public class ReactiveClientFullDuplexExchange {
|
|||
requestFuture.get(1, TimeUnit.MINUTES);
|
||||
|
||||
System.out.println("Shutting down");
|
||||
client.shutdown(CloseMode.GRACEFUL);
|
||||
client.close(CloseMode.GRACEFUL);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue