Apache Mina 服务器重新启动 java.net.BindException:地址已在使用中
我的服务器应用程序中有一个相当烦人的问题。
我使用以下代码绑定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不确定根本原因,但我在某处读到了一个似乎对我有用的修复程序:
只需添加即可让我关闭并重新启动
I'm not sure of the root cause, but I read somewhere a fix for this that seems to work for me:
Simply adding that let me shutdown and restart
我要添加的几件事:
使用
这将立即关闭会话,而不是等待刷新。
参考文献:
会话关闭 - 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:
use
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)
您需要小心关闭,否则 TCP 会以这种方式运行。请参阅此
You need to be careful with the close otherwise TCP behaves in this way. See this
并不是说我对 MINA 很熟悉,而是如何
是什么样子的?
哦,好吧。您是否使用 root 用户权限执行了该命令?
Not that I'm familiar with MINA, but how does
look like?
Oh, ok. Did you hit the command with root user privilege?
添加以下代码:
这允许重用端口。
Add the following code:
This allows the port be reused.
首先通过“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