Apache Mina Server Restart java.net.BindException: address is already in use

I have a pretty nasty problem in my server application.

I associate Apache Mina with the following code:

acceptor.bind(new InetSocketAddress(PORT)); 

Where the acceptor is a NioSocketAcceptor. Through the HTTP interface, I can shut down the server to 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 and 15 minutes) When I start, I get the following exception: java.net.BindException: the address is 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 2.0 RC1 version.

Thanks in advance for any ideas on how to resolve this.

+4
source share
6 answers

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

 acceptor.setReuseAddress(true); 

Just adding that let me shut down and reboot

+6
source

A few things I would add:

  • Set the acceptor to reuse the associated address
     acceptor.setReuseAddress(true) 
  • In your closed block instead
     session.close(false) 
    use
     session.close(true) 
    This closes the session immediately, rather than waiting for a flash.

Literature:

Session closed - 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 )

+3
source

Not that I'm familiar with MINA, but how

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

to look like?

Oh, OK. Are you in a team with root privileges?

0
source

You have to be careful with closing, otherwise TCP behaves this way. See this

0
source

Add the following code:

 acceptor.setReuseAddress(true) 

This allows port reuse.

0
source

First check the OS configuration for "sysctl net.ipv4.tcp_fin_timeout" , then change it to 30 seconds, second "sysctl -a | grep net.ipv4.tcp_tw" , change a value similar to this net.ipv4.tcp_tw_reuse = 1 net.ipv4 .tcp_tw_recycle = 1

0
source

Source: https://habr.com/ru/post/1303579/


All Articles