Java NIO selector close idle connections?

Discussion in 'Java' started by meselfo, Feb 11, 2008.

  1. meselfo

    meselfo Guest

    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?
     
    meselfo, Feb 11, 2008
    #1
    1. Advertising

  2. meselfo

    EJP Guest

    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.
     
    EJP, Feb 11, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arandil
    Replies:
    1
    Views:
    1,332
    Arandil
    Apr 12, 2004
  2. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,282
    iksrazal
    Jun 18, 2004
  3. Domagoj Klepac

    NIO - Selector.select() doesn't block

    Domagoj Klepac, May 7, 2006, in forum: Java
    Replies:
    7
    Views:
    6,400
    Gordon Beaton
    May 10, 2006
  4. Gen
    Replies:
    2
    Views:
    1,059
  5. jobs
    Replies:
    2
    Views:
    898
Loading...

Share This Page