具有正常关闭的AsynchronousServerSocketChannel

时间:2015-10-11 22:55:50

标签: java multithreading java-8 nio2

my previous Question我询问了如何实现正确的多线程服务器。我得到了响应程序“优雅关闭”,我试过todo所以。但是,它没有用。我仍然在客户端处于TIME_WAIT状态的开放套接字。

客户端:

private <T extends Serializable> T sendCommand(final Command<T> command) throws ExecutionException, InterruptedException, IOException, ClassNotFoundException {
    T result = null;

    try (final AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(channelGroup)) {
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        channel.connect(this.mwInfo.getNextMiddleware()).get();

        final OutputStream os = Channels.newOutputStream(channel);
        final InputStream is = Channels.newInputStream(channel);
        final ObjectOutputStream oos = new ObjectOutputStream(os);

        oos.writeObject(command);
        oos.flush();
        channel.shutdownOutput();

        final ObjectInputStream ois = new ObjectInputStream(is);
        result = (T) ois.readObject();

        while(ois.read() != -1){
            System.out.println("busy");
        }

        try{
            channel.shutdownInput();
        }catch(Exception ex){
            ex.printStackTrace();
        }

        oos.close();
        ois.close();
    }

    return result;
}

服务器:

this.asyncSocket.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
        @Override
        public void completed(final AsynchronousSocketChannel result, Void attachment) {
            asyncSocket.accept(null, this);

            exec.submit(new Runnable() {
                @Override
                public void run() {
                    Command cmd = null;
                    ObjectInputStream ois = null;
                    ObjectOutputStream oos = null;

                    try {
                        ois = new ObjectInputStream(Channels.newInputStream(result));
                        cmd = (Command) ois.readObject();

                        while(ois.read() != -1){
                            System.out.println("busy");
                        }

                        result.shutdownInput();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                    try{
                        oos = new ObjectOutputStream(Channels.newOutputStream(result));
                        oos.writeObject("test"); //do some other work here..
                        oos.flush();
                        result.shutdownOutput();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    try {
                        oos.close();
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    try {
                        result.close();
                    }catch (IOException ex){
                        ex.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void failed(Throwable exc, Void attachment) {
        }
    });

有谁知道为什么这不是优雅的关机? 它的结构看起来不太好,因为我正在玩try-catch块。

提前致谢!

1 个答案:

答案 0 :(得分:2)

  

我仍然在客户端处于TIME_WAIT状态的开放套接字。

您将始终在TIME_WAIT的一侧或另一侧使用套接字,客户端就是您想要的位置,而不是服务器端。

状态在2 * MSL之后到期,这意味着两个最大段寿命,这意味着两次两分钟。

这里没有问题需要解决。