问题描述
Web 服务 - 部署在 LINUX 中的 Tomcat下面是我发送 HTTP 请求的代码我读到 TOO MANY OPEN FILES 原因是因为客户端没有关闭流并保持打开状态我尝试在下面的代码中关闭我的流并将 ulimit -n 数字增加到 4096,仍然出现此错误
Web Service - Tomcat deployed in LINUXBelow is my code that sends HTTP requesti read that TOO MANY OPEN FILES causes because of client does not close the stream and leave it open thenI tried close my stream in below codesAnd 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();
这是我得到的错误:
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)
推荐答案
该错误表明 accept()
调用无法接受套接字连接.根据底层系统调用的文档,有导致此异常的两个潜在原因:您的进程中打开的文件过多,或者系统范围内打开的文件过多.
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.
可能打开的连接太多.你可以使用 netstat -anp |grep TOMCAT_PROCESS_ID
查看有多少连接涉及到Tomcat服务器.这将包括来自客户端的入站连接,以及从您的网络应用程序到外部服务的出站连接;入站连接将 Tomcat 端口显示为目的地.如果您有很多入站连接,那么问题是客户端过多或未关闭连接的客户端(您的示例似乎就是这样做的).
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).
更有可能是您没有正确关闭 Web 应用程序(已部署到 Tomcat 服务器)中的文件.为了诊断我将运行 ls -l/proc/TOMCAT_PROCESS_ID/fd
,它将为您提供该进程打开的所有文件和套接字的列表.您将在此列表中看到应用程序 WAR,以及 Tomcat 使用的一些 JAR.如果您在文件系统中看到大量文件,请查看它们的打开位置.
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.
这篇关于SocketException:打开的文件太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!