I am new to nio sockets, I have written a server using nio sockets and now I am trying to write shutdown hook to make sure a graceful exit by cleaning up resources.
My question is does Selector.close()
method closes all client sockets? if not please let me know how can I access all the client sockets without having a separate list of them.
Java Doc says following for selector.close()
method
Closes this selector.
If a thread is currently blocked in one of this selector's selection methods then it is interrupted as if by invoking the selector's wakeup method.
Any uncancelled keys still associated with this selector are invalidated, their channels are deregistered, and any other resources associated with this selector are released.
If this selector is already closed then invoking this method has no effect.
After a selector is closed, any further attempt to use it, except by invoking this method or the wakeup method, will cause a ClosedSelectorException to be thrown.
Above description uses word "deregistered" which gives a feeling that it does not close the sockets but just removes their mapping from selector.
No, it only closes the Selector.
You can access all the registered socket keys via Selector.keys(), before you close the selector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With