Example usage for java.nio.channels SelectionKey attachment

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

Introduction

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

Prototype

Object attachment

To view the source code for java.nio.channels SelectionKey attachment.

Click Source Link

Usage

From source file:org.apache.htrace.impl.PackedBufferManager.java

private void doRecv(SelectionKey sockKey, ByteBuffer response) throws IOException {
    sockKey.interestOps(SelectionKey.OP_READ);
    SocketChannel sock = (SocketChannel) sockKey.attachment();
    int totalRead = response.remaining();
    long startMs = TimeUtil.nowMs();
    long remainingMs = conf.ioTimeoutMs;
    while (remainingMs > 0) {
        selector.select(remainingMs);/*www.  j av  a 2  s.  c  o m*/
        for (SelectionKey key : selector.selectedKeys()) {
            if (key.isReadable()) {
                sock.read(response);
            }
        }
        if (response.remaining() == 0) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Received all " + totalRead + " bytes from " + conf.endpointStr);
            }
            return;
        }
        remainingMs = updateRemainingMs(startMs, conf.ioTimeoutMs);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Received " + (totalRead - response.remaining()) + " out of " + totalRead + " bytes from "
                    + conf.endpointStr);
        }
        if (remainingMs == 0) {
            throw new IOException("Attempt to write to " + conf.endpointStr + " timed out after "
                    + TimeUtil.deltaMs(startMs, TimeUtil.nowMs()) + " ms.");
        }
    }
}

From source file:org.apache.nifi.io.nio.ChannelListener.java

public void shutdown(final long period, final TimeUnit timeUnit) {
    channelDispatcher.stop();/*from  w  w  w .  ja va 2 s  .co  m*/
    for (SelectionKey selectionKey : socketChannelSelector.keys()) {
        final AbstractChannelReader reader = (AbstractChannelReader) selectionKey.attachment();
        selectionKey.cancel();
        if (reader != null) {
            while (!reader.isClosed()) {
                try {
                    Thread.sleep(channelReaderFrequencyMSecs);
                } catch (InterruptedException e) {
                }
            }
            final ScheduledFuture<?> readerFuture = reader.getScheduledFuture();
            readerFuture.cancel(false);
        }
        IOUtils.closeQuietly(selectionKey.channel()); // should already be closed via reader, but if reader did not exist...
    }
    IOUtils.closeQuietly(socketChannelSelector);

    for (SelectionKey selectionKey : serverSocketSelector.keys()) {
        selectionKey.cancel();
        IOUtils.closeQuietly(selectionKey.channel());
    }
    IOUtils.closeQuietly(serverSocketSelector);
    executor.shutdown();
    try {
        executor.awaitTermination(period, timeUnit);
    } catch (final InterruptedException ex) {
        LOGGER.warn("Interrupted while trying to shutdown executor");
    }
    final int currentBufferPoolSize = bufferPool.size();
    final String warning = (currentBufferPoolSize != initialBufferPoolSize)
            ? "Initial buffer count=" + initialBufferPoolSize + " Current buffer count=" + currentBufferPoolSize
                    + " Could indicate a buffer leak.  Ensure all consumers are executed until they complete."
            : "";
    LOGGER.info("Channel listener shutdown. " + warning);
}

From source file:org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher.java

@Override
public void completeConnection(SelectionKey key) {
    // connection is done. Return the buffer to the pool
    SocketChannelAttachment attachment = (SocketChannelAttachment) key.attachment();
    try {/*  w w  w. ja  v  a 2  s.  c om*/
        bufferPool.put(attachment.getByteBuffer());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    currentConnections.decrementAndGet();
}

From source file:org.apache.synapse.transport.udp.IODispatcher.java

/**
 * Remove an endpoint. This causes the corresponding UDP socket to be
 * closed.//from  w ww  .j a v  a2s.  c om
 * 
 * @param serviceName the name of the service corresponding to
 *                    the endpoint
 * @throws IOException if an error occurred when closing the socket
 */
public void removeEndpoint(final String serviceName) throws IOException {
    execute(new SelectorOperation() {
        @Override
        public void doExecute(Selector selector) throws IOException {
            Iterator<SelectionKey> it = selector.keys().iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                Endpoint endpoint = (Endpoint) key.attachment();
                if (serviceName.equals(endpoint.getService().getName())) {
                    key.cancel();
                    key.channel().close();
                    break;
                }
            }
        }
    });
}

From source file:org.cloudata.core.commitlog.WorkerPool.java

private void handleWrite(SelectionKey key) {
    Pipe pipe = (Pipe) key.attachment();

    try {/*from  w  w  w . j ava 2  s.c  o m*/
        pipe.write(key.channel());
    } catch (CommitLogShutDownException e) {
        workerPool.shutdown();
    } catch (Exception e) {
        LOG.error("error in handling write due to : ", e);
        cleanupErrorPipe(pipe);
    }
}

From source file:org.cloudata.core.commitlog.WorkerPool.java

private void handleConnection(SelectionKey key) {
    Pipe pipe = (Pipe) key.attachment();

    try {//ww  w . j a  v a2  s .  c o  m
        pipe.connect(key.channel());
    } catch (CommitLogShutDownException e) {
        workerPool.shutdown();
    } catch (Exception e) {
        LOG.error("error in handling connection due to : ", e);
        cleanupErrorPipe(pipe);
    }
}

From source file:org.cloudata.core.commitlog.WorkerPool.java

private void handleData(SelectionKey key) {
    Pipe pipe = (Pipe) key.attachment();

    try {/*from  ww w  . j av a 2s.co  m*/
        pipe.read(key.channel());
    } catch (CommitLogShutDownException e) {
        workerPool.shutdown();
    } catch (Exception e) {
        if (e instanceof PipeNormallyClosed) {
            pipeSet.remove(pipe);
            pipe.close();
            LOG.debug("Pipe#" + pipe.getPipeKey() + " is gracefully closed.");
        } else {
            LOG.error("error in reading data due to ", e);
            cleanupErrorPipe(pipe);
        }
    }
}

From source file:org.commoncrawl.io.internal.NIOSocketSelector.java

/** poll method  - poll the registered socket for events and potentially block for IO for the 
 *  specified timeout value /*  w  ww  .j ava 2s  . c  o m*/
 *  
 *  @param timeoutValue - amount of time in MS to wait (block) for IO 
 *  
 *  */
@SuppressWarnings("unchecked")
public int poll(long timeoutValue, TimeUsageDetail timeUsageDetailOut) throws IOException {

    long timeStart = System.currentTimeMillis();

    if (_lastPollTime != -1 && (timeStart - _lastPollTime) >= 30000) {
        LOG.error("POLL Delta Too Long:" + (timeStart - _lastPollTime));
    }
    _lastPollTime = timeStart;

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.blockedTime = 0;
        timeUsageDetailOut.unblockedTime = 0;
    }

    if (_selector == null || !_selector.isOpen()) {
        IOException e = new IOException("Selector NULL or Selector is Not Open!");
        LOG.error(e);
        throw e;
    }

    processPendingRegistrations();
    long timeEnd = System.currentTimeMillis();

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.unblockedTime += (timeEnd - timeStart);
    }

    /*
    if (timeoutWatchSet.size() != 0) { 
      // We have a timeout pending, so calculate the time until then and select appropriately
      long nextTimeout = timeoutWatchSet.first().getTimeoutTimestamp();
      long selectTime = nextTimeout - System.currentTimeMillis();
      if (selectTime < timeoutValue) {
        timeoutValue = Math.max(selectTime,0);
      }
    }
    */
    timeStart = System.currentTimeMillis();

    int count = 0;
    if (timeoutValue <= 0) {
        count = _selector.selectNow();
    } else {
        if (timeoutValue == Long.MAX_VALUE)
            timeoutValue = 0;
        count = _selector.select(timeoutValue);
    }
    timeEnd = System.currentTimeMillis();

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.blockedTime += (timeEnd - timeStart);
    }

    long unblockedTimeStart = System.currentTimeMillis();

    // if (count != 0 ) { 

    Set<SelectionKey> selectionSet = _selector.selectedKeys();

    for (Iterator<SelectionKey> i = selectionSet.iterator(); i.hasNext();) {

        SelectionKey selectionKey = i.next();

        i.remove();

        if (selectionKey.isValid()) {

            Object attachment = selectionKey.attachment();
            /*
            if (attachment instanceof TAsyncMethodCall) {
              transitionThriftMethod((TAsyncMethodCall)attachment,selectionKey);
            }
            */
            if (attachment instanceof NIOSocket) {
                NIOSocket theSocket = (NIOSocket) selectionKey.attachment();

                if (theSocket != null && theSocket.getListener() != null) {

                    // reset interest ops 
                    selectionKey.interestOps(0);

                    // process events in key ... 
                    if (selectionKey.isConnectable()) {

                        boolean connected = false;
                        Exception disconnectException = null;
                        try {
                            if (((NIOClientSocket) theSocket).finishConnect()) {
                                connected = true;
                                // log it ... 
                                // LOG.info("Connected to:"+((NIOClientSocket)theSocket).getSocketAddress());
                                // reset the selection key's ops.. otherwise, select keeps returning on an already connected socket (since we have registered for CONNECT)
                                System.out.println(
                                        "Connected to:" + ((NIOClientSocket) theSocket).getSocketAddress());
                                timeStart = System.currentTimeMillis();
                                ((NIOClientSocketListener) theSocket.getListener())
                                        .Connected((NIOClientSocket) theSocket);
                                if (timeUsageDetailOut != null) {
                                    timeUsageDetailOut.timeInConnectedEvt += System.currentTimeMillis()
                                            - timeStart;
                                }
                            } else {
                                //LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() + " - finishConnect returned false");
                                theSocket.close();
                            }
                        } catch (IOException e) {
                            //LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() + " with Exception:"+e);
                            theSocket.close();
                            disconnectException = e;
                        } catch (RuntimeException e) {
                            LOG.error("Caught Runtime Exception in Connected Event:"
                                    + StringUtils.stringifyException(e));
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Excepted((NIOClientSocket) theSocket, e);
                            theSocket.close();
                            disconnectException = e;
                            //KILL THE SERVER 
                            //throw e;
                        }

                        // if we were unable to properly establish the connection, trigger the Disconnected notification ... 
                        if (!connected) {
                            //LOG.error("Failed to Complete Connection in Finish Connect- Calling Disconnected");
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Disconnected((NIOClientSocket) theSocket, disconnectException);
                            // continue to the next socket ... 
                            continue;
                        }
                    }

                    // now always set the socket to readable state ... 
                    if ((theSocket instanceof NIOClientSocket) && selectionKey.isValid()) {
                        selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ);
                    }

                    if (selectionKey.isValid() && selectionKey.isReadable()) {
                        int bytesRead = -1;

                        try {

                            timeStart = System.currentTimeMillis();
                            // track the number of actual bytes read in the callback ... 
                            bytesRead = ((NIOClientSocketListener) theSocket.getListener())
                                    .Readable((NIOClientSocket) theSocket);
                            //System.out.println("Readable Took:" + (System.currentTimeMillis() - timeStart));
                            if (timeUsageDetailOut != null) {
                                timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart;
                            }

                            if (bytesRead == -1) {
                                // log it ... 
                                // LOG.error("Abnormal Disconnect Detected on Socket:"+ ((NIOClientSocket)theSocket).getSocketAddress());
                                // trigger a disconnect event ...
                                ((NIOClientSocketListener) theSocket.getListener())
                                        .Disconnected((NIOClientSocket) theSocket, null);
                                // close the socket ... 
                                theSocket.close();
                            }
                        } catch (RuntimeException e) {
                            LOG.error("Caught Runtime Exception in Readable Event:"
                                    + StringUtils.stringifyException(e));
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Excepted((NIOClientSocket) theSocket, e);
                            theSocket.close();
                            //KILL THE SERVER 
                            // throw e;
                        }
                        // if bytesRead == -1 then this means that the underlying connection has gone bad ... 
                    }

                    if (selectionKey.isValid() && selectionKey.isWritable()) {
                        try {

                            timeStart = System.currentTimeMillis();
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Writeable((NIOClientSocket) theSocket);
                            // System.out.println("Writable Took:" + (System.currentTimeMillis() - timeStart));
                            if (timeUsageDetailOut != null) {
                                timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart;
                            }
                        } catch (RuntimeException e) {
                            LOG.error("Caught Runtime Exception in Readable Event:"
                                    + StringUtils.stringifyException(e));
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Excepted((NIOClientSocket) theSocket, e);
                            theSocket.close();
                            //KILL THE SERVER ? 
                            //throw e;
                        }

                    }

                    if (selectionKey.isValid() && selectionKey.isAcceptable()) {
                        ((NIOServerSocket) theSocket).acceptable();
                        // re-register for accept on this socket 
                        selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT);
                    }
                }
            }
            // exernally managed socket (thrift client socket)
            else if (attachment instanceof NIOClientSocketListener) {
                NIOClientSocketListener listener = (NIOClientSocketListener) attachment;
                // reset interest ops 
                selectionKey.interestOps(0);
                // now always set the socket to readable state ... 
                selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ);

                if (selectionKey.isValid() && selectionKey.isReadable()) {
                    int bytesRead = -1;

                    try {

                        timeStart = System.currentTimeMillis();
                        // track the number of actual bytes read in the callback ... 
                        bytesRead = listener.Readable(null);
                        //System.out.println("Readable Took:" + (System.currentTimeMillis() - timeStart));
                        if (timeUsageDetailOut != null) {
                            timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart;
                        }

                        if (bytesRead == -1) {
                            // log it ... 
                            // LOG.error("Abnormal Disconnect Detected on Socket:"+ ((NIOClientSocket)theSocket).getSocketAddress());
                            // trigger a disconnect event ...
                            listener.Disconnected(null, null);
                        }
                    } catch (RuntimeException e) {
                        LOG.error("Caught Runtime Exception in Readable Event:"
                                + StringUtils.stringifyException(e));
                        listener.Excepted(null, e);
                    }
                    // if bytesRead == -1 then this means that the underlying connection has gone bad ... 
                }

                if (selectionKey.isValid() && selectionKey.isWritable()) {
                    try {

                        timeStart = System.currentTimeMillis();
                        listener.Writeable(null);
                        // System.out.println("Writable Took:" + (System.currentTimeMillis() - timeStart));
                        if (timeUsageDetailOut != null) {
                            timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart;
                        }
                    } catch (RuntimeException e) {
                        LOG.error("Caught Runtime Exception in Readable Event:"
                                + StringUtils.stringifyException(e));
                        listener.Excepted(null, e);
                    }
                }
            }
        } else {
            LOG.error("Invalid Socket Detected. Calling Disconnect");
            NIOSocket theSocket = (NIOSocket) selectionKey.attachment();
            if (theSocket != null && theSocket.getListener() != null) {
                theSocket.getListener().Disconnected(theSocket, null);
            }
        }
    }
    //timeoutThriftMethods();
    //startPendingThriftMethods();      

    long unblockedTimeEnd = System.currentTimeMillis();
    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.unblockedTime += (unblockedTimeEnd - unblockedTimeStart);
    }

    // }
    return count;
}

From source file:org.commoncrawl.io.NIOSocketSelector.java

/**
 * poll method - poll the registered socket for events and potentially block
 * for IO for the specified timeout value
 * /*from w  w w  . j a v a2 s .  c o  m*/
 * @param timeoutValue
 *          - amount of time in MS to wait (block) for IO
 * 
 * */
public int poll(long timeoutValue, TimeUsageDetail timeUsageDetailOut) throws IOException {

    long timeStart = System.currentTimeMillis();

    if (_lastPollTime != -1 && (timeStart - _lastPollTime) >= 30000) {
        LOG.error("POLL Delta Too Long:" + (timeStart - _lastPollTime));
    }
    _lastPollTime = timeStart;

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.blockedTime = 0;
        timeUsageDetailOut.unblockedTime = 0;
    }

    if (_selector == null || !_selector.isOpen()) {
        IOException e = new IOException("Selector NULL or Selector is Not Open!");
        LOG.error(e);
        throw e;
    }

    processPendingRegistrations();
    long timeEnd = System.currentTimeMillis();

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.unblockedTime += (timeEnd - timeStart);
    }

    timeStart = System.currentTimeMillis();
    int count = _selector.select(timeoutValue);
    timeEnd = System.currentTimeMillis();

    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.blockedTime += (timeEnd - timeStart);
    }

    long unblockedTimeStart = System.currentTimeMillis();

    // if (count != 0 ) {

    Set<SelectionKey> selectionSet = _selector.selectedKeys();

    for (Iterator<SelectionKey> i = selectionSet.iterator(); i.hasNext();) {

        SelectionKey selectionKey = i.next();

        i.remove();

        if (selectionKey.isValid()) {

            NIOSocket theSocket = (NIOSocket) selectionKey.attachment();

            if (theSocket != null && theSocket.getListener() != null) {

                // reset interest ops
                selectionKey.interestOps(0);

                // process events in key ...
                if (selectionKey.isConnectable()) {

                    boolean connected = false;
                    Exception disconnectException = null;
                    try {
                        if (((NIOClientSocket) theSocket).finishConnect()) {
                            connected = true;
                            // log it ...
                            // LOG.info("Connected to:"+((NIOClientSocket)theSocket).getSocketAddress());
                            // reset the selection key's ops.. otherwise, select keeps
                            // returning on an already connected socket (since we have
                            // registered for CONNECT)
                            System.out.println(
                                    "Connected to:" + ((NIOClientSocket) theSocket).getSocketAddress());
                            timeStart = System.currentTimeMillis();
                            ((NIOClientSocketListener) theSocket.getListener())
                                    .Connected((NIOClientSocket) theSocket);
                            if (timeUsageDetailOut != null) {
                                timeUsageDetailOut.timeInConnectedEvt += System.currentTimeMillis() - timeStart;
                            }
                        } else {
                            // LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress()
                            // + " - finishConnect returned false");
                            theSocket.close();
                        }
                    } catch (IOException e) {
                        // LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress()
                        // + " with Exception:"+e);
                        theSocket.close();
                        disconnectException = e;
                    } catch (RuntimeException e) {
                        LOG.error("Caught Runtime Exception in Connected Event:"
                                + StringUtils.stringifyException(e));
                        ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e);
                        theSocket.close();
                        disconnectException = e;
                        // KILL THE SERVER
                        throw e;
                    }

                    // if we were unable to properly establish the connection, trigger
                    // the Disconnected notification ...
                    if (!connected) {
                        // LOG.error("Failed to Complete Connection in Finish Connect- Calling Disconnected");
                        ((NIOClientSocketListener) theSocket.getListener()).Disconnected(theSocket,
                                disconnectException);
                        // continue to the next socket ...
                        continue;
                    }
                }

                // now always set the socket to readable state ...
                if ((theSocket instanceof NIOClientSocket) && selectionKey.isValid()) {
                    selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ);
                }

                if (selectionKey.isValid() && selectionKey.isReadable()) {
                    int bytesRead = -1;

                    try {

                        timeStart = System.currentTimeMillis();
                        // track the number of actual bytes read in the callback ...
                        bytesRead = ((NIOClientSocketListener) theSocket.getListener())
                                .Readable((NIOClientSocket) theSocket);
                        // System.out.println("Readable Took:" +
                        // (System.currentTimeMillis() - timeStart));
                        if (timeUsageDetailOut != null) {
                            timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart;
                        }

                        if (bytesRead == -1) {
                            // log it ...
                            // LOG.error("Abnormal Disconnect Detected on Socket:"+
                            // ((NIOClientSocket)theSocket).getSocketAddress());
                            // trigger a disconnect event ...
                            ((NIOClientSocketListener) theSocket.getListener()).Disconnected(theSocket, null);
                            // close the socket ...
                            theSocket.close();
                        }
                    } catch (RuntimeException e) {
                        LOG.error("Caught Runtime Exception in Readable Event:"
                                + StringUtils.stringifyException(e));
                        ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e);
                        theSocket.close();
                        // KILL THE SERVER
                        throw e;
                    }
                    // if bytesRead == -1 then this means that the underlying connection
                    // has gone bad ...
                }

                if (selectionKey.isValid() && selectionKey.isWritable()) {
                    try {

                        timeStart = System.currentTimeMillis();
                        ((NIOClientSocketListener) theSocket.getListener())
                                .Writeable((NIOClientSocket) theSocket);
                        // System.out.println("Writable Took:" +
                        // (System.currentTimeMillis() - timeStart));
                        if (timeUsageDetailOut != null) {
                            timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart;
                        }
                    } catch (RuntimeException e) {
                        LOG.error("Caught Runtime Exception in Readable Event:"
                                + StringUtils.stringifyException(e));
                        ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e);
                        theSocket.close();
                        // KILL THE SERVER
                        throw e;
                    }

                }

                if (selectionKey.isValid() && selectionKey.isAcceptable()) {
                    ((NIOServerSocket) theSocket).acceptable();
                    // re-register for accept on this socket
                    selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT);
                }
            }
        } else {
            LOG.error("Invalid Socket Detected. Calling Disconnect");
            NIOSocket theSocket = (NIOSocket) selectionKey.attachment();
            if (theSocket != null && theSocket.getListener() != null) {
                theSocket.getListener().Disconnected(theSocket, null);
            }
        }
    }

    long unblockedTimeEnd = System.currentTimeMillis();
    if (timeUsageDetailOut != null) {
        timeUsageDetailOut.unblockedTime += (unblockedTimeEnd - unblockedTimeStart);
    }

    // }
    return count;
}

From source file:org.commoncrawl.rpc.thriftrpc.ThriftRPCServerChannel.java

/**
 * Do connection-close cleanup on a given SelectionKey.
 *//*  w  ww .  ja  v a 2 s.  com*/
private void cleanupSelectionkey(SelectionKey key) {
    // remove the records from the two maps
    ThriftRPCClientChannel buffer = (ThriftRPCClientChannel) key.attachment();
    if (buffer != null) {
        // close the buffer
        buffer.close();
    }
    // cancel the selection key
    key.cancel();
}