I have the following structure for TCP client-server communication:
- On server startup server starts acceptor thread, that accepts client connections and passes ServerSocket to it.
- When a client connection arrives, acceptor thread calls accept() on ServerSocket and submits client processing job to worker thread (by executor/thread pool) and provides client socket to it.
- Worker in loop reads data from client socket stream, processes it and sends replies.
The question is how to gracefully stop the whole system? I can stop acceptor thread by just closing ServerSocket. It will cause accept() blocking call to throw SocketException. But how to stop workers? They read from stream and this call is blocking. According to this streams does not throw InterruptedException and thus worker cannot be interrupt()'ed.
It looks like I need to close worker socket from another thread, right? For this, socket should be made a public field or a method should be provided in worker to close it. Will this be nice? Or may be my whole design is flawed?