Roma
Roma

Reputation: 333

Java HTTP Client Initialization with Spring

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

Answers (1)

GiorgosDev
GiorgosDev

Reputation: 1767

Shouldnt it be static or per class?

  • For better performance it is better to reuse the client. The way how you will reuse - make it static or create a Bean which will be injected - is up to you. Sometimes you might need several different clients with different configuration i.e. timeouts, etc.

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?

  • Connection Manager can be reused, this is configurable.

Everytime a request is made, we create a new HTTPclient of pool size 10 ?

  • With default configuration - yes.

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?

  • It depends not only on bean to be created once, but also on configuration of http client and connection manager

Please refer to Spring RestTemplate - Need to release connection? , http://www.baeldung.com/spring-scheduled-tasks

Upvotes: 2

Related Questions