Example usage for java.nio.channels SelectionKey isConnectable

List of usage examples for java.nio.channels SelectionKey isConnectable

Introduction

In this page you can find the example usage for java.nio.channels SelectionKey isConnectable.

Prototype

public final boolean isConnectable() 

Source Link

Document

Tests whether this key's channel has either finished, or failed to finish, its socket-connection operation.

Usage

From source file:voldemort.common.nio.AbstractSelectorManager.java

public void run() {
    threadName = Thread.currentThread().getName();

    try {/*  ww  w  . j a v a2 s  . c  om*/
        while (true) {
            if (isClosed.get()) {
                logger.debug("SelectorManager is closed, exiting");

                break;
            }

            lastHeartBeatTimeMs = System.currentTimeMillis();
            processEvents();

            try {
                selectTimeMs = System.currentTimeMillis();
                int selected = selector.select(SELECTOR_POLL_MS);
                selectTimeMs = System.currentTimeMillis() - selectTimeMs;
                selectCount = selected;

                if (isClosed.get()) {
                    logger.debug("SelectorManager is closed, exiting");

                    break;
                }

                if (selected > 0) {
                    processingTimeMs = System.currentTimeMillis();
                    Iterator<SelectionKey> i = selector.selectedKeys().iterator();

                    while (i.hasNext()) {
                        SelectionKey selectionKey = i.next();
                        i.remove();

                        if (selectionKey.isValid() && (selectionKey.isConnectable() || selectionKey.isReadable()
                                || selectionKey.isWritable())) {
                            Runnable worker = (Runnable) selectionKey.attachment();
                            worker.run();
                        }
                    }
                    processingTimeMs = System.currentTimeMillis() - processingTimeMs;
                }
            } catch (ClosedSelectorException e) {
                logger.debug("SelectorManager is closed, exiting");

                break;
            } catch (Throwable t) {
                if (logger.isEnabledFor(Level.ERROR))
                    logger.error(t.getMessage(), t);
            }
        }
    } catch (Throwable t) {
        if (logger.isEnabledFor(Level.ERROR))
            logger.error(t.getMessage(), t);
    } finally {
        try {
            close();
        } catch (Exception e) {
            if (logger.isEnabledFor(Level.ERROR))
                logger.error(e.getMessage(), e);
        }
    }
}

From source file:voldemort.common.nio.SelectorManagerWorker.java

public void run() {
    try {//from   w  w w .j a v  a2s.c om
        SelectionKey selectionKey = socketChannel.keyFor(selector);

        if (selectionKey.isConnectable())
            connect(selectionKey);
        else if (selectionKey.isReadable())
            read(selectionKey);
        else if (selectionKey.isWritable())
            write(selectionKey);
        else if (!selectionKey.isValid())
            throw new IllegalStateException("Selection key not valid for " + socketChannel.socket());
        else
            throw new IllegalStateException(
                    "Unknown state, not readable, writable, or valid for " + socketChannel.socket());
    } catch (ClosedByInterruptException e) {
        reportException(e);
        close();
    } catch (CancelledKeyException e) {
        close();
    } catch (EOFException e) {
        // EOFException is expected, hence no logging, otherwise this block
        // could be combined with IOException
        reportException(e);
        close();
    } catch (IOException e) {
        logger.info("Connection reset from " + socketChannel.socket() + " with message - " + e.getMessage());
        reportException(e);
        close();
    } catch (Throwable t) {
        logger.error("Caught throwable from " + socketChannel.socket(), t);
        close();
    }
}