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)
try/catch/finally. Otherwise you keep opened things that should be closed, leading to a resource exhaustion, in your case opened files. - Eugène Adell