Java NIO selector close idle connections?  
Author Message
meselfo





PostPosted: 2008-2-12 4:47:00 Top

java-programmer, Java NIO selector close idle connections? I have an object attached to all selectionKeys with a timestamp
showing last activity
on the connection. So how/when do i run through the keys to find
connections
that have been idle too long? Can i do it in the same main selector
loop that handles accept/read etc like this:

while (true) {

selector.select(sometimeout);
Iterator selectedKeys = selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
..process the selected key... accept/read etc..
}

if (timeToCheckForIdleConnections) {
Set<SelectionKey> k = selector.keys();
.. get attachment of all keys and check their timestamp for last
activity.. if idle for too long then close them!
}

}

or should I use a timethread that signals back to the main selector
thread which connections to close?
 
EJP





PostPosted: 2008-2-12 6:09:00 Top

java-programmer >> Java NIO selector close idle connections? meselfo wrote:
> or should I use a timethread that signals back to the main selector
> thread which connections to close?

Do it in the same thread. Much simpler. If you're concerned about
performance, use a timed select and only do it when select() returns
zero, i.e. there are no ready channels and the selector is idle: that
way you don't hold anything up.