|
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind(Native Method) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59) at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate.registerNew(SocketAcceptorDelegate.java:366) at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate.access$2(SocketAcceptorDelegate.java:330) at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate$Worker.run(SocketAcceptorDelegate.java:231) I've got this problem ONLY UNDER UNIX (Mac Os X and Linux), while Windows Mina behaviour is correct, it shutdowns cleanly without any problem. I experienced this problem with Mina 0.9.0 and Mina 0.91 snapshots. Did you try to call SocketAcceptor.setReuseAddress(true) before you bind?
I use the SimpleRegistry, so i think I can't test.
setReuseAddress(true) should be done by default as in 0.8 no ? You can call SimpleServiceRegistry.getAcceptor() to get the actual acceptor instance. Please downcast it to SocketAcceptor, then you can set it.
I know this inconvenient. We're looking for the best way to configure this parameter. In 0.8, there was no API to configure this kind of parameters, so I just enabled it by default. But mostly its default value of false because more than two instances can run at the same time accidently. I added a shutdown hook for unbinding the service.
Perhaps it's something nice to have in SimpleRegistry no ? That's a good idea. I guess we can add ShutdownHook in IoAcceptor implementation.
As always, any patches are welcome! :) adding :
Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { cancelKeys(); } }); att the end of the SocketAcceptorDelegate constructor should work ? well it's not working :)
How about calling unbind() for all bound SocketAddresses in your shutdown hook instead?
it's not working, prolly because Workers are stoped
I tryied :
final ServiceRegistry registry = new JxHubServiceRegistry(); registry.getAcceptor(TransportType.SOCKET).setDisconnectClientsOnUnbind(true); Runtime.getRuntime().addShutdownHook(new Thread() { // This method is called during shutdown public void run() { registry.unbindAll(); } }); but...nothing..I can see on first CTRL+C all the clients disconnecting, but I have to wait about a minute before starting again ! OK RESOLVED !
final ServiceRegistry registry = new JxHubServiceRegistry(); registry.getAcceptor(TransportType.SOCKET).setDisconnectClientsOnUnbind(true); ((SocketAcceptor)registry.getAcceptor(TransportType.SOCKET)).setReuseAddress(true); Runtime.getRuntime().addShutdownHook(new Thread() { // This method is called during shutdown @Override public void run() { registry.unbindAll(); } }); The line ((SocketAcceptor)registry.getAcceptor(TransportType.SOCKET)).setReuseAddress(true); seems to resolve the issue ! Can you use TRUE by default ? Bye :D I think the point of this issue is that you have to call setReuseAddress(true). JVM will unbind all ports automatically when it shuts down.
Please try again without the shutdown hook and let me know 'setReuseAddress(true)' solely resolves this issue. If so, I'll close this issue as 'won't fix'. trustin, i can confirm that setReuseAddress(true) is sufficient to resolve the problem on red hat enterprise linux 4 with sun jvm 1.5.04. i did not have the problem on windows XP and suse linux 9.2.
why not set it to true by default? Thank you for clarification! Now I'll mark this issue as 'won't fix' because there's an easy workaround.
sorry for late testing.
It works with reuse address. We need to note it's not activated by default like in 0.8 for avoid double binding. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate.registerNew(SocketAcceptorDelegate.java:366)
at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate.access$2(SocketAcceptorDelegate.java:330)
at org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate$Worker.run(SocketAcceptorDelegate.java:231)