关闭netty连接会在TIMED_WAIT中保留连接

时间:2016-05-17 18:26:54

标签: java jax-rs netty

我有以下代码来启动netty服务器:

    Application application = ApplicationTypeFactory.getApplication(type);
    resteasyDeployment = new ResteasyDeployment();

    // Explicitly setting the Application should prevent scanning
    resteasyDeployment.setApplication(application);

    // Need to set the provider factory to the default, otherwise the
    // providers we need won't be registered, such as JSON mapping
    resteasyDeployment.setProviderFactory(ResteasyProviderFactory.getInstance());

    netty = new NettyJaxrsServer();

    netty.setHostname(HOST);
    netty.setPort(port);        
    netty.setDeployment(resteasyDeployment);        

    // Some optional extra configuration
    netty.setKeepAlive(true);
    netty.setRootResourcePath("/");
    netty.setSecurityDomain(null);
    netty.setIoWorkerCount(16);
    netty.setExecutorThreadCount(16);

    LOGGER.info("Starting REST server on: " + System.getenv("HOSTNAME") + ", port:" + port);        
    // Start the server
    //("Starting REST server on " + System.getenv("HOSTNAME"));
    netty.start();

    LOGGER.info("Started!");  

当我这样做时:

   netty.stop()

似乎没有关闭连接:

[john@dub-001948-VM01:~/workspace/utf/atf/src/test/resources ]$ netstat    -anp | grep 8888
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 ::ffff:10.0.21.88:8888      ::ffff:10.0.21.88:60654     TIME_WAIT   -                   
tcp        0      0 ::ffff:10.0.21.88:8888      ::ffff:10.0.21.88:60630     TIME_WAIT   -                    
tcp        0      0 ::ffff:10.0.21.88:8888      ::ffff:10.0.21.88:60629     TIME_WAIT   -                   
tcp        0      0 ::ffff:10.0.21.88:8888      ::ffff:10.0.21.88:60637     TIME_WAIT   -                   
tcp        0      0 ::ffff:10.0.21.88:8888      ::ffff:10.0.21.88:60640     TIME_WAIT   -  

即使程序退出。在其他帖子中,我已经读过netty不会在停止时关闭客户端连接。如何干净地关闭它?

1 个答案:

答案 0 :(得分:1)

Time_Wait是套接字状态之一。应用程序无法做任何事情。更多信息:http://www.isi.edu/touch/pubs/infocomm99/infocomm99-web/。对于繁忙的服务器,您可以调整一些tcp / ip参数以减轻其影响。