Reputation: 333
I am trying to understand the best way to use apaches HTTPPooledConnectionManager and the Closeable HTTPClient.
All examples I have seen seem to create a new HTTPClient in the request and no closing of the HttpClient.
Shouldnt it be static or per class? If HttpClient with pooled connection manager can be reused and the connections are released back to the pool, arent we creating HttpClient everytime we make a request?
I would like to especially understand this in a spring based framework.
Here is an example code where I think HttpClient should be static.
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import java.io.IOException;
import java.net.URISyntaxException;
public class Stub2000 {
private CloseableHttpClient httpClient;
public void init() {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(10000)
.setSocketTimeout(10000)
.setConnectionRequestTimeout(10000)
.build();
PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager();
poolManager.setMaxTotal(10);
poolManager.setDefaultMaxPerRoute(10);
httpClient = HttpClients
.custom()
.setDefaultRequestConfig(config)
.setConnectionManager(poolManager)
.build();
}
public void getInfo() throws URISyntaxException, IOException {
final URIBuilder ruribuilder = new URIBuilder();
final HttpGet get = new HttpGet();
ruribuilder.setHost(host)
.setScheme("https")
.setPort(5555)
.setPath(uri)
.addParameter("txn",txn);
get.setURI(ruribuilder.build());
CloseableHttpResponse response = httpClient.execute(get);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
String result = EntityUtils.toString(response.getEntity());
}
else {
//Some Error
}
}catch(Exception e){
e.printStackTrace();
} finally {
EntityUtils.consume(response.getEntity());
}
}
Everytime a request is made, we create a new HTTPclient of pool size 10 ? If this is a bean initialized with spring framework , and init method was invoked at the time of bean initialization, would it be done only once when the spring initializes or whenever a request is made?
Upvotes: 1
Views: 3743
Reputation: 1767
Shouldnt it be static or per class?
If HttpClient with pooled connection manager can be reused and the connections are released back to the pool, arent we creating HttpClient everytime we make a request?
Everytime a request is made, we create a new HTTPclient of pool size 10 ?
If this is a bean initialized with spring framework , and init method was invoked at the time of bean initialization, would it be done only once when the spring initializes or whenever a request is made?
Please refer to Spring RestTemplate - Need to release connection? , http://www.baeldung.com/spring-scheduled-tasks
Upvotes: 2