我有一个 Java 应用程序,该应用程序保持与一个地址的许多连接,可能一次大约有 2,000 个连接,几乎没有任何 Activity ,主要是出于监视目的而打开,时不时地传递几个字节。当需要打开新连接时,它会自动打开它们并将它们添加到它的池中。但有时,由于未知原因,应用程序会在创建到远程地址的套接字期间/之后立即收到 ClosedByInterruptException。据我所知,这只发生在客户端,作为线程中断信号的结果。我检查并重新检查了问题区域周围的源代码,看起来没问题。我希望我能得到某人的专业知识,看看除了源代码之外是否还有其他原因,例如,是否有系统原因导致这种情况?有硬件原因吗?服务器级别/路由器级别?我的网络知识我认为是业余的,但是 2K 连接对于路由器来说是否太多了?
INFO [08 Sep 2011 23:11:45,982]: Reconnecting id 20831
ERROR [08 Sep 2011 23:11:45,990]: IOException while creating plain socket channel
java.nio.channels.ClosedByInterruptException
at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:184)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:518)
at com.*.createSocketChannelPlain(MyTask.java:441)
at com.*._executeTask(MyTask.java:176)
at com.*.executeTask(MyTask.java:90)
at com.*.ThreadPool$WorkerThread.run(ThreadPool.java:55)
ERROR [08 Sep 2011 23:11:45,990]: Could not open socket
WARN [08 Sep 2011 23:11:45,990]: WorkerThread_24 received interrupted exception in ThreadPool
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at com.*.TaskQueue.getTask(TaskQueue.java:39)
at com.*.ThreadPool$WorkerThread.run(ThreadPool.java:48)
更新:我想尽我所能帮助其他人做出诊断。所以这是发生异常的实际函数,唯一不同的是我添加到第 441 行的行标记。
private SocketChannel createSocketChannelPlain() throws TaskFailedException {
SocketChannel socketChannel = null;
try {
// Create a non-blocking socket channel to use to communicate for imap connection
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
try {socketChannel.socket().setSoLinger(true, 0);} catch (Exception e) {}
try {socketChannel.socket().setKeepAlive(true);} catch (Exception e) {}
/*Line 441*/ socketChannel.connect(new InetSocketAddress(_HOSTNAME, _PORT));
//System.out.println("Started connection");
// Complete connection
while (!socketChannel.finishConnect()) {
// do something until connect completed
try {
//do what you want to do before sleeping
Thread.sleep(500);//sleep for 500 ms
//do what you want to do after sleeping
} catch(InterruptedException ie){
//If this thread was interrupted by another thread
try { socketChannel.close(); } catch (Exception e) {}
finally { socketChannel = null; }
break;
}
}
//System.out.println("Finished connecting");
return socketChannel;
} catch (IOException e) {
logger.error("IOException while creating plain socket channel to gmail", e);
try { socketChannel.close(); } catch (Exception e1) {}
finally { socketChannel = null; }
//throw new TaskFailedException("IOException occurred in createSocketChannel");
}
return socketChannel;
}
最佳答案
你在什么操作系统上运行这个?我不知道 Windows,但在 Linux(可能还有其他类 Unix 操作系统)上,您可以通过拥有大量套接字来耗尽文件句柄。在运行 Java 应用程序之前,您可以通过执行 ulimit -n 8192
或类似操作来解决此问题。或者,编辑 /etc/security/limits.conf
并设置 nofile
。综上所述,ClosedByInterruptedException
将是一种奇怪的方式来注意到这一点。
如果以上不是问题,接下来我会尝试运行 tcpdump
(如果我们谈论的是无 GUI 机器)或 Wireshark (如果我们没有)并捕获您的程序生成的流量,寻找连接开始时发生的奇怪事情。
关于Java 应用程序在打开套接字时立即抛出 ClosedByInterruptException,原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418976/