I am trying to create my code so that it can be "turned off" with interruption. This is mainly due to the fact that within the framework of the Executor , the Java concurrency package uses interrupt to cancel the tasks that are performed. In addition, the shutdown task should not know any internal components of the task being killed.
However, an accept call will not respond to an interrupt if it is not created using ServerSocketChannel . The created server with the ServerSocket constructor will ignore interrupts, and I did not find a way to reconfigure it.
If you cannot change the code creating the server, create another thread to call close on the server socket. This will also throw an exception in the thread blocked on accept , regardless of the method used to create the server socket.
This turns out to be a real pain when using SSL. The JSSE socket is not created from the InterruptibleChannel and will not respond to a simple interrupt in the stream.
I just noticed that the question says that the server cannot be closed without notifying the client. Successful interruption of a socket leads to its closure.
When accept is invoked, this should not be a problem since the client is not connected if the server socket is blocked in accept. This should only be a problem for Socket instances that represent current connections.
If this does not meet the notification requirements, a rework may be required to use the NIO ServerSocketChannel in non-blocking mode.
erickson
source share