我有一個應用程序Tomcat上運行,有時我有以下錯誤:錯誤雄貓「打開的文件太多」
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:398)
at java.net.ServerSocket.implAccept(ServerSocket.java:522)
at java.net.ServerSocket.accept(ServerSocket.java:490)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216)
at java.lang.Thread.run(Thread.java:722)
....
SEVERE: Error processed default web.xml named conf/web.xml at /local/myApp/apache-tomcat/conf/web.xml
java.io.FileNotFoundException: /local/myApp/apache-tomcat/conf/web.xml (Too many open files)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.catalina.startup.ContextConfig.getWebXmlSource(ContextConfig.java:1838)
at org.apache.catalina.startup.ContextConfig.getGlobalWebXmlSource(ContextConfig.java:1745)
at org.apache.catalina.startup.ContextConfig.getDefaultWebXmlFragment(ContextConfig.java:1418)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1253)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3926)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:722)
我檢查的範圍打開的文件和它的1024,但是當我用lsof檢查應用程序的打開文件數量將近200時,爲什麼會發生這種情況,如果它沒有達到極限? 我應該增加限制嗎?是否有任何其他原因得到這個錯誤? 讓服務再次正確運行的唯一方法是重新啓動tomcat,還有另一種恢復正常的方法嗎?
在此先感謝。
編輯: 這裏是處理doPost方法servlet的,一開始我並沒有關閉所有的數據流,它可能是?我添加了finally語句做這件事:
InputStream is = null;
DataInputStream dis = null;
OutputStream os = null;
DataOutputStream dos = null;
String paramName = "";
try {
os = response.getOutputStream();
is = request.getInputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
.....
}catch (Throwable e) {
LOGGER.error(e.getMessage());
} finally {
if (dis != null) {
dis.close();
}
else if(is != null) {
is.close();
}
if (dos != null) {
dos.close();
}
else if(os != null) {
os.close();
}
}
EDIT2: 做一些測試中,我認識到,以後如果我先關閉DataInputStream和再InputStream的,我在通信的一定數目的其他部分獲得在消息之前(我不知道爲什麼)。我改變了關閉流的順序,看起來好像什麼都沒有。但我仍然有這個問題。任何想法?
finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
}
是的,我檢查了這個網頁,但作爲服務它也沒有使用超過200的最大值,爲什麼要增加到4096的限制? – alicia
您應該首先在網站上搜索您的錯誤:http://stackoverflow.com/questions/5656458/java-net-socketexception-too-many-open-files – developerwjk
分享您的代碼!我們不能沒有任何代碼片段。 – tokhi