简体   繁体   中英

Java, HttpURLConnection handle CLOSE_WAIT connections

I am using java HttpURLConnection for REST calls. After working(testing of rest calls using ant targets) with all the rest api's I see there are many socket connections that are in 'CLOSE_WAIT' state.

I tried by calling the close() methods on the InputStream or OutputStream of an HttpURLConnection, but still the connections are in CLOSE_WAIT state only.

One other observation is even with con.disconnect() method also the connection is not being closed.

Please help me regarding this issue as CLOSE_WAIT connections indicate an error in the software.

Below is the code for get call. Other POST/PUT/DELETE calls are also like 'get'

public void get(String url, Header[] headers,
            NameValuePair[] data, ResponseHandler handler) throws HttpException {       
        HttpURLConnection con = null;
        try
        {
        String query = null;
        if (data != null) {
                query = getQueryString(data);
            }
        URL obj;
        if(query == null || query == "")            
            obj = new URL(url);
        else
            obj = new URL(url+"?"+query);       
        con = (HttpURLConnection) obj.openConnection();     
        setDoAuthentication(con);
        con.setRequestMethod("GET");
        con.setDoOutput(true);      
        if (headers != null) {
            for (int i = 0; i < headers.length; i++) {
                con.setRequestProperty(headers[i].getName(), headers[i].getValue());
            }           
        }                       
        con.setRequestProperty("Accept-encoding", "gzip");      
        con.setRequestProperty("Authorization", this.auth);     
        int responseCode = con.getResponseCode();       
        if (responseCode == HttpURLConnection.HTTP_OK) {
            if (handler!=null) handler.handleResponse(con);  // in handleResponse(con) method, I am closing the con input stream
        } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new HttpException(new UnauthorizedException());
        } else if (!is2xx(responseCode)) {
            ErrorHandler errHandler = new ErrorHandler();
            errHandler.handleResponse(con);
            throw errHandler.getResult();
        }       
    } 
        catch (MalformedURLException e) {
            throw new HttpException(e);
        }
        catch (IOException e) {
            handleSessionConnectionError(e, url);
        }       
        finally {
              con.disconnect();
            }       
        }

Thanks

Mohan G

Calling con.disconnect() may cause the issue. Check this out this link link .

It will create sockets on and on, and if the connections are multiple, it will overpass the max limit of files open per process, eg in linux is usually 1024. Instead of using disconnect, always close the input stream, the values defined in http.maxConnection in java system properties will do the work of closing idle connections when the limit is reach.

Check also javadoc of HttpUrlConnection in class and the method disconnect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM