Reputation: 5786
When I run my tests they hang at the same point: when I try to send a request. I understand that there are no free connections in the pool but I don't know how to fix this. Any ideas? Code:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
public class ClientTest {
private static String username = ITProperties.getInstance().getProperty("username");
private static String password = ITProperties.getInstance().getProperty("password");
private static CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
private static UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
public static final HttpClient HTTP_CLIENT = buildHttpClientWithCredentials();
public static HttpClient buildHttpClientWithCredentials() {
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
return HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
}
@Test
public void testWhenClientNotFound() throws JSONException, IOException {
String requestBody = new RequestBody().setSum(100000L).setDay(7).toString();
HttpPost request = RequestProvider.getPostRequest(URL, requestBody);
HttpResponse response = HTTP_CLIENT.execute(request); //HERE EXECUTION HANGS
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(404);
}
...
}
From log:
2017-08-10 12:24:13,787 DEBUG | main | org.apache.http.client.protocol.RequestAddCookies.process.123 | CookieSpec selected: default
2017-08-10 12:24:13,787 DEBUG | main | org.apache.http.client.protocol.RequestAddCookies.process.168 | Cookie [version: 0][name: JSESSIONID][value: 00000000000000000000000000000][domain: localhost][path: /][expiry: null] match [localhost:9900/path/to/my/resource]
2017-08-10 12:24:13,787 DEBUG | main | org.apache.http.client.protocol.RequestAuthCache.process.77 | Auth cache not set in the context
2017-08-10 12:24:13,787 DEBUG | main | org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection.255 | Connection request: [route: {}->http://localhost:9900][total kept alive: 0; route allocated: 2 of 2; total allocated: 2 of 20]
2017-08-10 12:24:33,919 DEBUG | HikariPool-1 housekeeper | com.zaxxer.hikari.pool.HikariPool.logPoolState.378 | HikariPool-1 - Pool stats (total=2, active=0, idle=2, waiting=0)
Upvotes: 1
Views: 5422
Reputation: 397
If you don't want to write the code to CloseableHttpResponse
. You can use EntityUtils.consume(response.getEntity)
call – necessary to consume the entire content of the response (entity) so that the manager can release the connection back to the pool.
Add this line after the utilization of the client response.
Upvotes: 0
Reputation: 1807
First of all you have to close your response:
CloseableHttpResponse response = httpclient.execute(httpget);
try {
<...>
} finally {
response.close();
}
Then you can configure a thread pool as described here:
PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
HttpHost localhost = new HttpHost("locahost", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
Upvotes: 2