From 1e40d27a2873f07411813ff5aa626c733592f400 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 25 Feb 2020 02:45:09 +0000 Subject: [PATCH] Apache channels are closeable --- .../dialogue/hc4/ApacheHttpClientBlockingChannel.java | 8 +++++++- .../com/palantir/dialogue/blocking/BlockingChannel.java | 6 +++++- .../dialogue/blocking/BlockingChannelAdapter.java | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/dialogue-apache-hc4-client/src/main/java/com/palantir/dialogue/hc4/ApacheHttpClientBlockingChannel.java b/dialogue-apache-hc4-client/src/main/java/com/palantir/dialogue/hc4/ApacheHttpClientBlockingChannel.java index d91fa3b804..92a2085ed4 100644 --- a/dialogue-apache-hc4-client/src/main/java/com/palantir/dialogue/hc4/ApacheHttpClientBlockingChannel.java +++ b/dialogue-apache-hc4-client/src/main/java/com/palantir/dialogue/hc4/ApacheHttpClientBlockingChannel.java @@ -25,6 +25,7 @@ import com.palantir.dialogue.core.BaseUrl; import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.exceptions.SafeRuntimeException; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -44,7 +45,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -final class ApacheHttpClientBlockingChannel implements BlockingChannel { +final class ApacheHttpClientBlockingChannel implements BlockingChannel, Closeable { private static final Logger log = LoggerFactory.getLogger(ApacheHttpClientBlockingChannel.class); private final CloseableHttpClient client; @@ -74,6 +75,11 @@ public Response execute(Endpoint endpoint, Request request) throws IOException { return new HttpClientResponse(client.execute(builder.build())); } + @Override + public void close() throws IOException { + client.close(); + } + private static final class HttpClientResponse implements Response { private final CloseableHttpResponse response; diff --git a/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannel.java b/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannel.java index 04afcebc98..12b5d6c982 100644 --- a/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannel.java +++ b/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannel.java @@ -18,6 +18,7 @@ import com.palantir.dialogue.Endpoint; import com.palantir.dialogue.Request; import com.palantir.dialogue.Response; +import java.io.Closeable; import java.io.IOException; /** @@ -25,6 +26,9 @@ * BlockingChannel is identical to Channel except that invocations block rather than returning * a future. */ -public interface BlockingChannel { +public interface BlockingChannel extends Closeable { Response execute(Endpoint endpoint, Request request) throws IOException; + + @Override + default void close() throws IOException {} } diff --git a/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannelAdapter.java b/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannelAdapter.java index 7d870c5160..ae6049de8f 100644 --- a/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannelAdapter.java +++ b/dialogue-blocking-channels/src/main/java/com/palantir/dialogue/blocking/BlockingChannelAdapter.java @@ -25,6 +25,7 @@ import com.palantir.dialogue.Request; import com.palantir.dialogue.Response; import com.palantir.tracing.Tracers; +import java.io.Closeable; import java.io.IOException; import java.util.concurrent.Callable; import java.util.concurrent.Executors; @@ -49,7 +50,7 @@ private static Channel of(BlockingChannel blockingChannel, ListeningExecutorServ private BlockingChannelAdapter() {} - private static final class BlockingChannelAdapterChannel implements Channel { + private static final class BlockingChannelAdapterChannel implements Channel, Closeable { private final BlockingChannel delegate; private final ListeningExecutorService executor; @@ -69,6 +70,11 @@ public String toString() { return "BlockingChannelAdapterChannel{delegate=" + delegate + ", executor=" + executor + '}'; } + @Override + public void close() throws IOException { + delegate.close(); + } + /** * Callable to run a blocking request. This could be a lambda, * but concrete classes provide better stack traces.