Search This Blog

Java: how is HttpURLConnection reused (connection pooling)

  1. According to this:
    The JDK supports both HTTP/1.1 and HTTP/1.0 persistent connections.
    
    When the application finishes reading the response body or when the application calls close() on the InputStream returned by URLConnection.getInputStream(), the JDK's HTTP protocol handler will try to clean up the connection and if successful, put the connection into a connection cache for reuse by future HTTP requests.
    
    The support for HTTP keep-Alive is done transparently. However, it can be controlled by system properties http.keepAlive, and http.maxConnections, as well as by HTTP/1.1 specified request and response headers.
    
    The system properties that control the behavior of Keep-Alive are:
    
    http.keepAlive=
    default: true
    
    Indicates if keep alive (persistent) connections should be supported.
    
    http.maxConnections=
    default: 5
    
    Indicates the maximum number of connections per destination to be kept alive at any given time
    
    HTTP header that influences connection persistence is:
    
    Connection: close
    
    If the "Connection" header is specified with the value "close" in either the request or the response header fields, it indicates that the connection should not be considered 'persistent' after the current request/response is complete.
    
    The current implementation doesn't buffer the response body. Which means that the application has to finish reading the response body or call close() to abandon the rest of the response body, in order for that connection to be reused. Furthermore, current implementation will not try block-reading when cleaning up the connection, meaning if the whole response body is not available, the connection will not be reused.
    






See also

No comments:

Post a Comment