Apache Mina 服务器重新启动 java.net.BindException:地址已在使用中

发布于 2024-08-24 15:51:12 字数 951 浏览 4 评论 0原文

我的服务器应用程序中有一个相当烦人的问题。

我使用以下代码绑定 Apache Mina:

acceptor.bind(new InetSocketAddress(PORT));

其中acceptor 是一个NioSocketAcceptor。 通过 HTTP 接口,我可以关闭服务器,以便重新启动它。

Server.ioAcceptor.unbind(new InetSocketAddress(Server.PORT));
        for(IoSession session: Server.ioAcceptor.getManagedSessions().values()){
            if(session.isConnected() && !session.isClosing()){
                session.close(false);
            }
        }
        Server.ioAcceptor.dispose();

        Main.transport.stop();
        Logger.getRootLogger().warn("System going down. Request from "+context.getRemoteAddress());
        System.exit(10);       

这是我用来停止 Mina 服务器的代码。但是,如果我尝试在接下来的几分钟内再次启动服务器。 (5 分钟到 15 分钟之间)我在启动时遇到以下异常: java.net.BindException: 地址已在使用中

我也尝试了一个简单的 ioAcceptor.unbind() 但没有什么区别。 服务器在带有 OpenJDK 的 Centos 5 上运行。 Apache Mina 版本是 2.0 RC1。

预先感谢您提供有关如何解决此问题的任何想法。

I have a rather annoying problem in my server application.

I bind Apache Mina with the following code:

acceptor.bind(new InetSocketAddress(PORT));

Where acceptor is an NioSocketAcceptor.
Over a HTTP interface I can shutdown the server so I can restart it.

Server.ioAcceptor.unbind(new InetSocketAddress(Server.PORT));
        for(IoSession session: Server.ioAcceptor.getManagedSessions().values()){
            if(session.isConnected() && !session.isClosing()){
                session.close(false);
            }
        }
        Server.ioAcceptor.dispose();

        Main.transport.stop();
        Logger.getRootLogger().warn("System going down. Request from "+context.getRemoteAddress());
        System.exit(10);       

This is the code I use to stop the Mina server. However if I try to start the server again in the next couple of minutes. (Somewhere between 5 minutes and 15 minutes) I get the following exception on start up:
java.net.BindException: Address already in use

I also tried a simple ioAcceptor.unbind() but there was no difference.
The server runs on Centos 5 with OpenJDK. Apache Mina version is 2.0 RC1.

Thank you in advance for any ideas on how to resolve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

儭儭莪哋寶赑 2024-08-31 15:51:12

我不确定根本原因,但我在某处读到了一个似乎对我有用的修复程序:

acceptor.setReuseAddress(true);

只需添加即可让我关闭并重新启动

I'm not sure of the root cause, but I read somewhere a fix for this that seems to work for me:

acceptor.setReuseAddress(true);

Simply adding that let me shutdown and restart

别再吹冷风 2024-08-31 15:51:12

我要添加的几件事:

  1. 设置您的接受器以重用绑定地址
    acceptor.setReuseAddress(true)
  2. 在您的 close 块中,而不是
    session。 close(false)

    使用

    session.close(true)

    这将立即关闭会话,而不是等待刷新。

参考文献:

会话关闭 - http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/session/IoSession.html#close(boolean)

ServerSocket复用地址 -
http ://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html?is-external=true#setReuseAddress(boolean)

A couple things I would add:

  1. Set your acceptor to reuse the bound address
    acceptor.setReuseAddress(true)
  2. In your close block, instead of
    session.close(false)

    use

    session.close(true)

    This will close the session immediately instead of waiting for a flush.

References:

Session close - http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/session/IoSession.html#close(boolean)

ServerSocket reuse address -
http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html?is-external=true#setReuseAddress(boolean)

魂牵梦绕锁你心扉 2024-08-31 15:51:12

您需要小心关闭,否则 TCP 会以这种方式运行。请参阅

You need to be careful with the close otherwise TCP behaves in this way. See this

浅听莫相离 2024-08-31 15:51:12

并不是说我对 MINA 很熟悉,而是如何

netstat -apn | grep 端口
ps-ef | grep
java

是什么样子的?

哦,好吧。您是否使用 root 用户权限执行了该命令?

Not that I'm familiar with MINA, but how does

netstat -apn | grep PORT
ps -ef | grep
java

look like?

Oh, ok. Did you hit the command with root user privilege?

轻拂→两袖风尘 2024-08-31 15:51:12

添加以下代码:

acceptor.setReuseAddress(true)

这允许重用端口。

Add the following code:

acceptor.setReuseAddress(true)

This allows the port be reused.

花落人断肠 2024-08-31 15:51:12

首先通过“sysctl net.ipv4.tcp_fin_timeout”检查操作系统配置,然后将其修改为30秒;第二个“sysctl -a|grep net.ipv4.tcp_tw”,像这样修改值
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1

First check the OS configuration by "sysctl net.ipv4.tcp_fin_timeout",then modify it to 30 seconds;Second "sysctl -a|grep net.ipv4.tcp_tw",modify the value like this
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文