KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > nio > channels > Selector

java.nio.channels
Class Selector

java.lang.Object
  extended by java.nio.channels.Selector
Direct Known Subclasses:
AbstractSelector
See Also:
Top Examples, Source Code, ConcurrentModificationException, iterator, interrupt, wakeup, selectNow(), select(long), select(), remove, remove, cancel, register, selectedKeys, keys, close, openSelector, open

public abstract void close()
                    throws IOException
See Also:
ClosedSelectorException, wakeup
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[240]Non-blocking I/O
By Anonymous on 2004/12/17 01:04:51  Rate
// non-blocking I/O: 
  
  
  
 import java.nio.channels.*; 
 import java.util.*; 
  
  
 // Create a selector object to monitor all open client connections. 
 Selector selector = Selector.open (  ) ; 
  
  
 // Create a new non-blocking ServerSocketChannel, bind it to port 8000, and 
 // register it with the Selector 
 ServerSocketChannel server = ServerSocketChannel.open (  ) ; // Open channel 
 server.configureBlocking ( false ) ; // Non-blocking 
 server.socket (  ) .bind ( new java.net.InetSocketAddress ( 8000 )  ) ; // Bind to port 
 SelectionKey serverkey = server.register ( selector, SelectionKey.OP_ACCEPT ) ; 
  
  
 for ( ;; )   {   // The main server loop. The server runs forever. 
     // This call blocks until there is activity on one of the  
     // registered channels. This is the key method in non-blocking I/O. 
     selector.select (  ) ; 
  
  
     // Get a java.util.Set containing the SelectionKey objects for 
     // all channels that are ready for I/O. 
     Set keys = selector.selectedKeys (  ) ; 
  
  
     // Use a java.util.Iterator to loop through the selected keys 
     for ( Iterator i = keys.iterator (  ) ; i.hasNext (  ) ;  )   {  
     SelectionKey key =  ( SelectionKey )  i.next (  ) ; 
     i.remove (  ) ; // Remove the key from the set of selected keys 
  
  
     // Check whether this key is the SelectionKey we got when 
     // we registered the ServerSocketChannel. 
     if  ( key == serverkey )   {  
         // Activity on the ServerSocketChannel means a client 
         // is trying to connect to the server. 
         if  ( key.isAcceptable (  )  )   {  
         // Accept the client connection 
         SocketChannel client = server.accept (  ) ; 
         // Put the client channel in non-blocking mode. 
         client.configureBlocking ( false ) ; 
         // Now register the client channel with the Selector object 
         SelectionKey clientkey = 
                     client.register ( selector, SelectionKey.OP_READ ) ; 
          }  
      }  
     else  {   // Otherwise, there must be activity on a client channel 
         // Double-check that there is data to read 
         if  ( !key.isReadable (  )  )  continue; 
             // Get the client channel that has data to read 
         SocketChannel client =  ( SocketChannel )  key.channel (  ) ; 
  
  
         // Now read bytes from the client into a buffer allocated elsewhere 
         int bytesread = client.read ( buffer ) ; 
  
  
         // If read (  )  returns -1, it indicates end-of-stream, which 
         // means the client has disconnected, so de-register the 
         // selection key and close the channel. 
         if  ( bytesread == -1 )   {    
         key.cancel (  ) ; 
         client.close (  ) ; 
         continue; 
          }  
      }  
      }  
  }  
  
  
  
 


public abstract boolean isOpen()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Set<SelectionKey> keys()
See Also:
ClosedSelectorException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Selector open()
                     throws IOException
See Also:
SelectorProvider, openSelector
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract SelectorProvider provider()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int select()
                    throws IOException
See Also:
ClosedSelectorException, wakeup
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1764]The Selector on which the SocketChannel has been registered is exhausted,
By Anonymous on 2006/05/31 11:10:09  Rate
while  (  bb.hasRemaining (  )   )   {  
    // *** socketChannel will always return 0 
    int len = socketChannel.write ( bb ) ;        
    if  ( len  <  0 )  {  
       throw new EOFException (  ) ; 
     }   
  }  
  
  
 This code will works most of the time....until the Selector on which the SocketChannel has been registered is exhausted, e.g the Selector isn't able to let the socketChannel flush the content of the ByteBuffer. , which means: he CPU will be consumed by looping over and over, producing disastrous performance problem  
 


public abstract int select(long timeout)
                    throws IOException
See Also:
IllegalArgumentException, ClosedSelectorException, Object.wait(long), wakeup
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Set<SelectionKey> selectedKeys()
See Also:
ClosedSelectorException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int selectNow()
                       throws IOException
See Also:
ClosedSelectorException, wakeup
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Selector()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1765]Use a temporary Selector
By Anonymous on 2006/05/31 11:14:51  Rate
If the main Selector is exhausted, a temporary Selector will be used to handle the OP_WRITE.  
  
  
 try  {  
     while  (  bb.hasRemaining (  )   )   {  
   int len = socketChannel.write ( bb ) ; 
   attempts++; 
   if  ( len  <  0 )  {  
       throw new EOFException (  ) ; 
    }  
  
  
   if  ( len == 0 )   {  
       if  (  writeSelector == null  )  {  
     writeSelector = SelectorFactory.getSelector (  ) ; 
     if  (  writeSelector == null )  {  
         // Continue using the main one. 
         continue; 
      }  
        }  
  
  
       key = socketChannel.register ( writeSelector, key.OP_WRITE ) ; 
  
  
       if  ( writeSelector.select ( 30 * 1000 )  == 0 )   {  
     if  ( attempts  >  2 )  
         throw new IOException ( "Client disconnected" ) ; 
        }  else  {  
     attempts--; 
        }  
    }  else  {  
       attempts = 0; 
    }  
      }  
  }  finally  {  
     if  ( key != null )   {  
   key.cancel (  ) ; 
   key = null; 
      }  
  
  
     if  (  writeSelector != null  )   {  
   // Flush the key. 
   writeSelector.selectNow (  ) ; 
   SelectorFactory.returnSelector ( writeSelector ) ; 
      }  
  }  
 


public abstract Selector wakeup()
See Also:
select(long), select(), selectNow()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags