Spring RestTemplate - Need to release connection?

前端 未结 3 1530
情深已故
情深已故 2021-02-05 20:44

This is my Configuration for Rest Template,

    @Bean
    @Qualifier(\"myRestService\")
    public RestTemplate createRestTemplate(@Value(\"${connection.timeout}         


        
3条回答
  •  渐次进展
    2021-02-05 21:25

    The question which you have asked: Do i need to release the connection after the above call or is it taken care by RestTemplate. If we need to take care of releasing connection.

    No, you do not need to close the connection on the response, if you use resttemplate.

    From the apache httpclient, you need to consume the complete response (EntityUtils.consume(HttpEntity) and close the response.

    This can be verified in the ClientConnectionRelease.java

    But RestTemplate does this for you, to verify the same have a look into RestTemplate.java

    Look for method

    protected  T doExecute(URI url,...) {
     try {
            ClientHttpRequest request = this.createRequest(url, method);
            ...
            response = request.execute();
            ...
            if(responseExtractor != null) {
                var7 = responseExtractor.extractData(response);
                return var7;
            }
           ...
           ...
        } finally {
            if(response != null) {
                response.close();
            }
    
        }
    }
    

    Where response extractor does the work for you by consuming the response using responseExtractor.extractData(response);

    And after extracting the data completely it is closing response.close() as well.

提交回复
热议问题