2

我有一个 Java 应用程序,它可以打开许多到一个地址的连接,可能一次大约有 2,000 个,几乎没有任何活动,主要是为了监视目的而打开,不时传递几个字节。当需要打开新连接时,它会自动打开它们并将它们添加到其池中。但有时,由于未知原因,应用程序在创建到远程地址的套接字期间/之后立即收到 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;
}
4

1 回答 1

0

你在什么操作系统上运行这个?我不了解 Windows,但在 Linux(可能还有其他类似 Unix 的操作系统)上,您可以通过拥有大量套接字来耗尽文件句柄。ulimit -n 8192在运行 Java 应用程序之前,您可以通过执行或类似操作来解决此问题。或者,编辑/etc/security/limits.conf并设置nofile. 所有这一切,ClosedByInterruptedException将是一种奇怪的方式来注意到这一点。

如果上述不是问题,我接下来要尝试运行tcpdump(如果我们谈论的是无 GUI 机器)或Wireshark(如果不是)并捕获程序生成的流量,寻找连接开始时发生的奇怪事情。

于 2011-09-14T21:07:13.007 回答