Skip to content

Commit b70ed45

Browse files
committed
Issue docker-java#1: use HttpClient 4.2 with connecting pooling
1 parent 465a935 commit b70ed45

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959

6060

6161
<dependency>
62-
<groupId>commons-httpclient</groupId>
63-
<artifactId>commons-httpclient</artifactId>
64-
<version>3.1</version>
65-
<scope>compile</scope>
62+
<groupId>org.apache.httpcomponents</groupId>
63+
<artifactId>httpclient</artifactId>
64+
<version>4.2.5</version>
6665
</dependency>
66+
6767
<dependency>
6868
<groupId>org.apache.commons</groupId>
6969
<artifactId>commons-compress</artifactId>

src/main/java/com/kpelykh/docker/client/DockerClient.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
import com.sun.jersey.api.client.config.DefaultClientConfig;
1010
import com.sun.jersey.api.json.JSONConfiguration;
1111
import com.sun.jersey.client.apache4.ApacheHttpClient4;
12+
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
1213
import com.sun.jersey.core.util.MultivaluedMapImpl;
1314
import org.apache.commons.io.FileUtils;
1415
import org.apache.commons.lang.StringUtils;
16+
import org.apache.http.client.HttpClient;
17+
import org.apache.http.conn.scheme.PlainSocketFactory;
18+
import org.apache.http.conn.scheme.Scheme;
19+
import org.apache.http.conn.scheme.SchemeRegistry;
20+
import org.apache.http.impl.client.DefaultHttpClient;
21+
import org.apache.http.impl.conn.PoolingClientConnectionManager;
1522
import org.codehaus.jettison.json.JSONException;
1623
import org.codehaus.jettison.json.JSONObject;
1724
import org.slf4j.Logger;
@@ -43,12 +50,23 @@ public DockerClient(String serverUrl) {
4350
restEndpointUrl = serverUrl + "/v1.3";
4451
ClientConfig clientConfig = new DefaultClientConfig();
4552
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
46-
client = ApacheHttpClient4.create(clientConfig);
53+
54+
SchemeRegistry schemeRegistry = new SchemeRegistry();
55+
schemeRegistry.register(new Scheme("http", 4243, PlainSocketFactory.getSocketFactory()));
56+
57+
PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
58+
// Increase max total connection
59+
cm.setMaxTotal(1000);
60+
// Increase default max connection per route
61+
cm.setDefaultMaxPerRoute(1000);
62+
63+
HttpClient httpClient = new DefaultHttpClient(cm);
64+
client = new ApacheHttpClient4(new ApacheHttpClient4Handler(httpClient, null, false), clientConfig);
65+
4766
client.addFilter(new JsonClientFilter());
4867
//client.addFilter(new LoggingFilter());
4968
}
5069

51-
5270
/**
5371
** MISC API
5472
**
@@ -91,15 +109,15 @@ public Version version() throws DockerException {
91109
**
92110
**/
93111

94-
public String pull(String repository) throws DockerException {
112+
public ClientResponse pull(String repository) throws DockerException {
95113
return this.pull(repository, null, null);
96114
}
97115

98-
public String pull(String repository, String tag) throws DockerException {
116+
public ClientResponse pull(String repository, String tag) throws DockerException {
99117
return this.pull(repository, tag, null);
100118
}
101119

102-
public String pull(String repository, String tag, String registry) throws DockerException {
120+
public ClientResponse pull(String repository, String tag, String registry) throws DockerException {
103121
Preconditions.checkNotNull(repository, "Repository was not specified");
104122

105123
if (StringUtils.countMatches(repository, ":") == 1) {
@@ -118,7 +136,7 @@ public String pull(String repository, String tag, String registry) throws Docker
118136

119137
try {
120138
LOGGER.trace("POST: " + webResource.toString());
121-
return webResource.accept(MediaType.APPLICATION_JSON).post(String.class);
139+
return webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(ClientResponse.class);
122140
} catch (UniformInterfaceException exception) {
123141
if (exception.getResponse().getStatus() == 500) {
124142
throw new DockerException("Server error.", exception);
@@ -424,11 +442,7 @@ public ClientResponse logContainer(String containerId) throws DockerException {
424442
params.add("logs", "1");
425443
params.add("stdout", "1");
426444
params.add("stderr", "1");
427-
//params.add("stream", "1");
428-
429-
Client client = Client.create();
430-
ClientConfig clientConfig = new DefaultClientConfig();
431-
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
445+
params.add("stream", "1");
432446

433447
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/attach", containerId))
434448
.queryParams(params);
@@ -580,10 +594,6 @@ public ClientResponse build(File dockerFolder, String tag) throws DockerExceptio
580594
//We need to use Jersey HttpClient here, since ApacheHttpClient4 will not add boundary filed to
581595
//Content-Type: multipart/form-data; boundary=Boundary_1_372491238_1372806136625
582596

583-
Client client = Client.create();
584-
ClientConfig clientConfig = new DefaultClientConfig();
585-
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
586-
587597
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
588598
params.add("t", tag);
589599

0 commit comments

Comments
 (0)