Any way to get read timeouts with Java NIO/selectors?

Posted by mmebane on Stack Overflow See other posts from Stack Overflow or by mmebane
Published on 2010-04-18T03:32:16Z Indexed on 2010/04/18 3:43 UTC
Read the original article Hit count: 382

Filed under:
|
|
|
|

I'm converting a Java server application which used blocking IO and thread-per-client to NIO and a single IO thread (probably a thread pool after I get the basic implementation done). The one thing I am having an issue with is disconnecting clients after they have been idle for a period.

I had previously been using SO_TIMEOUT and blocking reads. However, with selector-based IO, reads don't block... I was hoping that I'd be able to set a timeout and be able to select on read timeout, with something like SelectionKey.isReadTimeout(), but nothing like that seems to exist.

The current best solution I have come up with is to have a Timer with a TimerTask which keeps track of the keys which are waiting on read, and then canceling them and re-scheduling them on each read. Is there a better solution?

© Stack Overflow or respective owner

Related posts about java

Related posts about nio