Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Selector.close() closes all client sockets?

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.

like image 614
Mubashar Avatar asked Dec 25 '22 07:12

Mubashar


1 Answers

No, it only closes the Selector.

You can access all the registered socket keys via Selector.keys(), before you close the selector.

like image 198
user207421 Avatar answered Dec 28 '22 06:12

user207421