Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java NIO selector close idle connections?

Reply
Thread Tools

Java NIO selector close idle connections?

 
 
meselfo
Guest
Posts: n/a
 
      02-11-2008
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?
 
Reply With Quote
 
 
 
 
EJP
Guest
Posts: n/a
 
      02-11-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
NIO - Selector.select() doesn't block Domagoj Klepac Java 7 05-10-2006 06:36 AM
NIO with timeouts != NIO? iksrazal Java 1 06-18-2004 02:28 PM
selector.select() in NIO and high cpu usage Arandil Java 1 04-12-2004 02:04 PM
java.nio as opposed to java.net - basic difference in program logic? Chris Berg Java 1 11-23-2003 11:09 PM



Advertisments