Java 8 SelectionKey - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SelectionKey class.

This code example shows how to use the following methods: attachment, interestOps, isValid, isWritable
    } 
 
    @Override 
    @edu.umd.cs.findbugs.annotations.SuppressWarnings({"VO_VOLATILE_INCREMENT" }) 
    protected void handleSelectionKey(SelectionKey sk) {
        if (sk.isValid() && sk.isWritable()) {
            writeEvents++;
            sk.interestOps(sk.interestOps() & ~SelectionKey.OP_WRITE);
            SelectionHandler handler = (SelectionHandler) sk.attachment();
            handler.handle();
        } 
    } 
} 
Full Snippet Info
7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: interestOps, isReadable, isValid, isWritable, readyOps
        // Make the client non blocking and register it with selector for reads 
        client.configureBlocking(false);
        SelectionKey key = client.register(selector,SelectionKey.OP_READ);
 
        // assert it is not selected 
        assertTrue(key.isValid());
        assertFalse(key.isReadable());
        assertEquals(0,key.readyOps());
 
        // try selecting and assert nothing selected 
        int selected = selector.selectNow();
        assertEquals(0,selected);
        assertEquals(0,selector.selectedKeys().size());
        assertTrue(key.isValid());
        assertFalse(key.isReadable());
        assertEquals(0,key.readyOps());
 
        // Write a byte from server to client 
        server.getOutputStream().write(42);
        server.getOutputStream().flush();

7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: attach, attachment, cancel, channel, interestOps
        closeSelector(); 
        shutdownComplete(); 
    } 
 
    private SocketChannel channelFor(SelectionKey key) {
        return (SocketChannel) key.channel();
    } 
 
    private void close(SelectionKey key) {
        SocketChannel channel = (SocketChannel) key.channel();
        if (logger.isDebugEnabled()) { 
            logger.debug("Closing connection from " + channel.socket().getRemoteSocketAddress());
        } 
        Closer.closeQuietly(channel.socket());
        Closer.closeQuietly(channel);
        key.attach(null);
        key.cancel();
    } 
 
    private void write(SelectionKey key) throws IOException {

7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: attachment, isReadable, isValid
    } 
 
    @Override 
    @edu.umd.cs.findbugs.annotations.SuppressWarnings({"VO_VOLATILE_INCREMENT" }) 
    protected void handleSelectionKey(SelectionKey sk) {
        if (sk.isValid() && sk.isReadable()) {
            readEvents++;
            SelectionHandler handler = (SelectionHandler) sk.attachment();
            handler.handle();
        } 
    } 
} 
Full Snippet Info
7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: interestOps, isConnectable, isReadable, isValid, isWritable
     * @param key SelectionKey 
     * @return boolean 
     * @throws IOException 
     */ 
    public boolean process(SelectionKey key, boolean waitForAck) throws IOException {
        int ops = key.readyOps();
        key.interestOps(key.interestOps() & ~ops);
        //in case disconnect has been called 
        if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key.");
        if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled.");
        if ( key.isConnectable() ) {
            if ( socketChannel.finishConnect() ) {
                completeConnect(); 
                if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
                return false; 
            } else  { 
                //wait for the connection to finish 
                key.interestOps(key.interestOps() | SelectionKey.OP_CONNECT);
                return false; 
            }//end if 

7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE

Related Java 8 SelectionKey Questions & Answers:

See Code Examples for Java 8 SelectionKey Methods: