4

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.

Kosaki
  • 103
  • 1
  • 4

6 Answers6

6

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

Eugene Marcotte
  • 751
  • 8
  • 21
3

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)

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
0

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?

Enno Shioji
  • 26,542
  • 13
  • 70
  • 109
0

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

LenW
  • 3,054
  • 1
  • 24
  • 25
  • Thanks. I am trying this now, but I must wait to see the results. Any ideas on how I would go about testing this without putting it on the production server? I normally have around 2000 sockets open, and it is hard to replicate in a test environment. – Kosaki Mar 11 '10 at 20:22
0

Add the following code:

acceptor.setReuseAddress(true)

This allows the port be reused.

joinky
  • 1
  • 2
0

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

joinky
  • 1
  • 2