Skip to content

Commit f37c48c

Browse files
author
Marcus Linke
committed
Fix connection pool shutdown when garbage collection runs
1 parent 2fb8742 commit f37c48c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import javax.ws.rs.client.WebTarget;
1919

2020
import com.github.dockerjava.api.command.UpdateContainerCmd;
21-
2221
import com.github.dockerjava.core.SSLConfig;
22+
2323
import org.apache.http.config.RegistryBuilder;
2424
import org.apache.http.conn.socket.ConnectionSocketFactory;
2525
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
@@ -115,6 +115,8 @@ public class DockerCmdExecFactoryImpl implements DockerCmdExecFactory {
115115

116116
private DockerClientConfig dockerClientConfig;
117117

118+
private PoolingHttpClientConnectionManager connManager = null;
119+
118120
@Override
119121
public void init(DockerClientConfig dockerClientConfig) {
120122
checkNotNull(dockerClientConfig, "config was not specified");
@@ -187,8 +189,21 @@ public void init(DockerClientConfig dockerClientConfig) {
187189
configureProxy(clientConfig, protocol);
188190
}
189191

190-
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(getSchemeRegistry(
191-
originalUri, sslContext));
192+
connManager = new PoolingHttpClientConnectionManager(getSchemeRegistry(
193+
originalUri, sslContext)) {
194+
195+
@Override
196+
public void close() {
197+
super.shutdown();
198+
}
199+
200+
@Override
201+
public void shutdown() {
202+
// Disable shutdown of the pool. This will be done later, when this factory is closed
203+
// This is a workaround for finalize method on jerseys ClientRuntime which
204+
// closes the client and shuts down the connection pool when it is garbage collected
205+
}
206+
};
192207

193208
if (maxTotalConnections != null) {
194209
connManager.setMaxTotal(maxTotalConnections);
@@ -412,7 +427,6 @@ public KillContainerCmd.Exec createKillContainerCmdExec() {
412427
return new KillContainerCmdExec(getBaseResource(), getDockerClientConfig());
413428
}
414429

415-
416430
@Override
417431
public UpdateContainerCmd.Exec createUpdateContainerCmdExec() {
418432
return new UpdateContainerCmdExec(getBaseResource(), getDockerClientConfig());
@@ -527,6 +541,7 @@ public DisconnectFromNetworkCmd.Exec createDisconnectFromNetworkCmdExec() {
527541
public void close() throws IOException {
528542
checkNotNull(client, "Factory not initialized. You probably forgot to call init()!");
529543
client.close();
544+
connManager.close();
530545
}
531546

532547
public DockerCmdExecFactoryImpl withReadTimeout(Integer readTimeout) {

0 commit comments

Comments
 (0)