SocketException : TOO MANY OPEN FILES

我怕爱的太早我们不能终老 提交于 2021-02-19 06:21:40

问题


Web Service - Tomcat deployed in LINUX Below is my code that sends HTTP request i read that TOO MANY OPEN FILES causes because of client does not close the stream and leave it open then I tried close my stream in below codes And increased ulimit -n number to 4096, still got this error

if ("GET".equals(methodType)) { //req
                        System.setProperty("http.keepAlive", "false");
                        logger.info("<--------CALLING TAX-CLIENT REQUEST------------>");
                        URL url = new URL(api_url + "?" + queryParams);
        //                        URLEncoder.encode(queryParams, "UTF-8"
                        logger.debug("API_URL TO SEND REQUEST : " + url);
                        logger.debug("Received TOKEN IS : " + encoding_token);
                        conn = (HttpsURLConnection) url.openConnection();
                        conn.setRequestProperty("Authorization", "Bearer " + encoding_token);
                        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                        conn.setRequestProperty("Accept-Charset", "charset=UTF-8");
                        conn.setRequestMethod("GET");
                        conn.setDoInput(true);
                        InputStream _is;
                        /* error from server */
                        if (conn.getResponseCode() == HttpsURLConnection.HTTP_INTERNAL_ERROR) {
                            _is = conn.getErrorStream();
                            logger.error("ERROR : " + _is.toString());
                            throw new BillingException("501", _is.toString(), new BillingExceptionBean());
                        } else {
                            _is = conn.getInputStream();
                        }

                        try (BufferedReader in = new BufferedReader(
                                new InputStreamReader(_is, Charset.forName("UTF-8")), BUFFER_SIZE)) {
                            inputLine = in.readLine();
                            logger.info("Response from tax : " + inputLine);
                            if (inputLine.contains("default message")) {
                                JSONObject jsonObject = new JSONObject(inputLine);
                                String error = jsonObject.getString("code") + ":" + jsonObject.getString("message") + "\n";
                                this.setErrorMessage(error);
                            } else if (inputLine.contains("error") & inputLine.contains("default")) {
                                String str = inputLine;
                                String[] parts = str.split("default message");
                                String str1 = parts[2];
                                String[] parts2 = str1.split("\"");
                                this.setErrorMessage(parts2[0].toString());
                            }
                            logger.info("Buffered Reader is closed");
                            in.close();
                        }
                        logger.info("Input stream is closing connection");
        //                _is.close();
        //                conn.disconnect();

This is ERROR i GOT :

Feb 21, 2019 9:04:56 AM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
    at java.net.ServerSocket.implAccept(ServerSocket.java:545)
    at java.net.ServerSocket.accept(ServerSocket.java:513)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
    at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:220)
    at java.lang.Thread.run(Thread.java:745)

回答1:


The error indicates that the accept() call is unable to accept the socket connection. Per the documentation of the underlying system call there are two potential causes for this exception: either you have too many open files in your process or there are too many open files system-wide.

It's possible that there are too many open connections. You could use netstat -anp | grep TOMCAT_PROCESS_ID to look at how many connections involve the Tomcat server. This will include inbound connections from client, as well as outbound connections from your web-app to external services; inbound connections will show the Tomcat port as the destination. If you have a lot of inbound connections, then it's a problem of too many clients or clients that aren't closing the connection (which your example appears to do).

More likely is that you're not properly closing files in your web-application (that you've deployed to the Tomcat server). To diagnose that I'd run ls -l /proc/TOMCAT_PROCESS_ID/fd, which will give you a list of all the files and sockets that are open by that process. You will see the application WAR in this list, along with some JARs that are used by Tomcat. If you see a lot of files from your filesystem, look at where they're opened.



来源:https://stackoverflow.com/questions/54801335/socketexception-too-many-open-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!