这是一个很好的做法,在每个新请求时初始化一个新的httpClient

时间:2016-02-22 10:17:09

标签: java apache http

我在我的java项目中使用apache httpClient lib。

当我没有清理HttpClient时,我收到了一个错误

df %>% count(Rptname)

Timeout waiting for connection

所以我将代码更改为

public class RoutingUrlHttpClient implements IRoutingUrlHttpClient {

    final static Logger logger = Logger.getLogger(RoutingUrlHttpClient.class);


    private IRoutingResponseFromStringFetcher routingResponseParser;
    private IRoutingResponseConverter routingResponseConverter;
    private IUrlUtils urlUtils;
    private CloseableHttpClient client;
    private ILogUtils logUtils;


    @Inject
    @Singleton
    public RoutingUrlHttpClient(IRoutingResponseFromStringFetcher routingResponseParser,
                                IRoutingResponseConverter routingResponseConverter, IUrlUtils urlUtils,
                                ILogUtils logUtils) {
        this.routingResponseParser = routingResponseParser;
        this.routingResponseConverter = routingResponseConverter;
        this.urlUtils = urlUtils;
        this.logUtils = logUtils;

        RequestConfig requestConfig = RequestConfig.custom()
                //time till handshake
                .setConnectTimeout(40 * 1000)
                //happens when you have a pool of connections and they are all busy, not allowing the connection
                // manager to give you one connection to make the request.
                .setConnectionRequestTimeout(40 * 1000)
                //time till response
                .setSocketTimeout(40 * 1000)
                .build();
        client = HttpClientBuilder
                .create()
                .setDefaultRequestConfig(requestConfig)
                .build();
    }


//    public CompleteRoutingResponseDtoWrapper sendAndReturnDtoWrapper(String routingRequestUrl) {
//        CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper = sendRoutingRequestString
// (routingRequestUrl);
//        completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
//        return completeRoutingResponseDtoWrapper;
//    }

    @Override
    public CompleteRoutingResponseLong sendRoutingRequestStringWithFullResponse(String routingRequestUrl) {
        CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper =
                sendRoutingRequestString(routingRequestUrl);
        completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
        return routingResponseConverter.toCompleteRoutingResponseFull(completeRoutingResponseDtoWrapper);
    }



    private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(String routingRequestUrl) {
        return sendRoutingRequestString(Constants.NUM_OF_RETRIES, routingRequestUrl);
    }

    private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(int numberOfTriesLeft,
                                                                       String routingRequestUrl) {
        routingRequestUrl = urlUtils.getHttpUrl(routingRequestUrl);

        CompleteRoutingResponseDtoWrapper answer = new CompleteRoutingResponseDtoWrapper();
        CloseableHttpResponse response = null;
        try {

            logger.debug("before sending http");

            Stopwatch stopWatch = Stopwatch.createStarted();
            response = client.execute(new HttpGet(routingRequestUrl));
            stopWatch.stop();
//            String latencyMsg = "after sending http. client-latency: "+stopWatch.elapsed(TimeUnit.MILLISECONDS) +" server-latency: "+response.getHeaders("Latency")[0].getValue();

            logUtils.addLongToLongStatisticCollector("http.client.latency", (int)stopWatch.elapsed(TimeUnit.MILLISECONDS));
            logUtils.addLongToLongStatisticCollector("http.server.latency", Integer.parseInt(response.getHeaders("Latency")[0].getValue()));

            answer = analyzeStatusCodeAndMsgBody(numberOfTriesLeft, routingRequestUrl, answer, response, stopWatch);


        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage());
            answer.errorMsg = e.getMessage();
            answer.latency = null;
        }

        handleNullResponse(answer);
        return answer;
    }

我想问:

  • 这是在每次新请求时初始化新httpClient的好习惯吗?

  • 我是否正确清理资源?

  • 是否有更有效的方式发送HTTP请求?也许是一个nio http客户端库?

2 个答案:

答案 0 :(得分:2)

我在HTTP Client requests done right中对此明确要求表明,由于CloseableHttpClient是线程安全的,它可能/应该用作单例,所以 - 不,你不需要 init a新的httpClient

建议阅读对该问题的回答 - 非常有用。

答案 1 :(得分:0)

CloseableHttpClient是线程安全的,因此您可以安全地重用实例,而不是基于每个请求创建实例。您还可以调查连接池。我经常使用这样的模式:

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build();

阅读this了解更多背景信息。