You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Oleg Kalnichevski (JIRA)" <ji...@apache.org> on 2013/09/13 12:30:52 UTC

[jira] [Commented] (HTTPCORE-349) HttpResponse should implement Closeable (Java7 try-with-resources support)

    [ https://issues.apache.org/jira/browse/HTTPCORE-349?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13766386#comment-13766386 ] 

Oleg Kalnichevski commented on HTTPCORE-349:
--------------------------------------------

Hendy,

This is an easy subject. You see, not all HTTP response objects keep allocated system resources and therefore not all of them need to be closed. For instance HTTP response objects returned by asynchronous (non-blocking) protocol handlers are self-contained. Making them implement Closeable would add extra complexity for no good reason.

What is certainly true that blocking protocol handlers should always return CloseableHttpResponse instead of HttpResponse. However, at least in HttpClient 4.3 one can use CloseableHttpClient abstract class instead of HttpClient interface in order to be able to utilize 'try with resources' with response objects

{code}

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
    HttpGet httpGet = new HttpGet("http://targethost/homepage");
    try (CloseableHttpResponse response1 = httpclient.execute(httpGet)) {
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        EntityUtils.consume(entity1);
    }
}

{code}

Oleg
                
> HttpResponse should implement Closeable (Java7 try-with-resources support)
> --------------------------------------------------------------------------
>
>                 Key: HTTPCORE-349
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-349
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore
>    Affects Versions: 4.3
>            Reporter: Hendy Irawan
>
> Currently we've got code like this:
> {code}
> final HttpResponse response = client.execute(postReq);
> try {
> 	final StatusLine responseStatus = response.getStatusLine();
> 	final String responseBody;
> 	if (response.getEntity() != null)
> 		responseBody = IOUtils.toString(response.getEntity().getContent());
> 	else
> 		responseBody = null;
> 	if (responseStatus.getStatusCode() >= 200 && responseStatus.getStatusCode() < 300) {
> 		log.info("Job returned {}: {}", responseStatus, responseBody);
> 	} else
> 		log.error("Job returned {}: {}", responseStatus, responseBody);
> } finally {
> 	HttpClientUtils.closeQuietly(response);
> }
> {code}
> It would be great if this could be simplified to : (in Java 7)
> {code}
> try (final HttpResponse response = client.execute(postReq)) { 
>     ...
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org