Search in sources :

Example 16 with SelectableChannel

use of java.nio.channels.SelectableChannel in project jetty.project by eclipse.

the class ManagedSelector method processAccept.

private void processAccept(SelectionKey key) {
    SelectableChannel server = key.channel();
    SelectableChannel channel = null;
    try {
        channel = _selectorManager.doAccept(server);
        if (channel != null)
            _selectorManager.accepted(channel);
    } catch (Throwable x) {
        closeNoExceptions(channel);
        LOG.warn("Accept failed for channel " + channel, x);
    }
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel)

Example 17 with SelectableChannel

use of java.nio.channels.SelectableChannel in project hs4j by killme2008.

the class Reactor method lookJVMBug.

/**
	 * Look jvm bug
	 * 
	 * @param before
	 * @param selected
	 * @param wait
	 * @return
	 * @throws IOException
	 */
private boolean lookJVMBug(long before, int selected, long wait) throws IOException {
    boolean seeing = false;
    long now = System.currentTimeMillis();
    if (JVMBUG_THRESHHOLD > 0 && selected == 0 && wait > JVMBUG_THRESHHOLD && now - before < wait / 4 && !wakenUp.get() && /* waken up */
    !Thread.currentThread().isInterrupted()) /* Interrupted */
    {
        jvmBug.incrementAndGet();
        if (jvmBug.get() >= JVMBUG_THRESHHOLD2) {
            gate.lock();
            try {
                lastJVMBug = now;
                log.warn("JVM bug occured at " + new Date(lastJVMBug) + ",http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933,reactIndex=" + reactorIndex);
                if (jvmBug1) {
                    log.debug("seeing JVM BUG(s) - recreating selector,reactIndex=" + reactorIndex);
                } else {
                    jvmBug1 = true;
                    log.info("seeing JVM BUG(s) - recreating selector,reactIndex=" + reactorIndex);
                }
                seeing = true;
                final Selector new_selector = SystemUtils.openSelector();
                for (SelectionKey k : selector.keys()) {
                    if (!k.isValid() || k.interestOps() == 0) {
                        continue;
                    }
                    final SelectableChannel channel = k.channel();
                    final Object attachment = k.attachment();
                    channel.register(new_selector, k.interestOps(), attachment);
                }
                selector.close();
                selector = new_selector;
            } finally {
                gate.unlock();
            }
            jvmBug.set(0);
        } else if (jvmBug.get() == JVMBUG_THRESHHOLD || jvmBug.get() == JVMBUG_THRESHHOLD1) {
            if (jvmBug0) {
                log.debug("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + reactorIndex);
            } else {
                jvmBug0 = true;
                log.info("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + reactorIndex);
            }
            gate.lock();
            seeing = true;
            try {
                for (SelectionKey k : selector.keys()) {
                    if (k.isValid() && k.interestOps() == 0) {
                        k.cancel();
                    }
                }
            } finally {
                gate.unlock();
            }
        }
    } else {
        jvmBug.set(0);
    }
    return seeing;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) Date(java.util.Date) Selector(java.nio.channels.Selector)

Example 18 with SelectableChannel

use of java.nio.channels.SelectableChannel in project netty by netty.

the class NioEventLoop method processSelectedKeysPlain.

private void processSelectedKeysPlain(Set<SelectionKey> selectedKeys) {
    // See https://github.com/netty/netty/issues/597
    if (selectedKeys.isEmpty()) {
        return;
    }
    Iterator<SelectionKey> i = selectedKeys.iterator();
    for (; ; ) {
        final SelectionKey k = i.next();
        final Object a = k.attachment();
        i.remove();
        if (a instanceof AbstractNioChannel) {
            processSelectedKey(k, (AbstractNioChannel) a);
        } else {
            @SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
            processSelectedKey(k, task);
        }
        if (!i.hasNext()) {
            break;
        }
        if (needsToSelectAgain) {
            selectAgain();
            selectedKeys = selector.selectedKeys();
            // Create the iterator again to avoid ConcurrentModificationException
            if (selectedKeys.isEmpty()) {
                break;
            } else {
                i = selectedKeys.iterator();
            }
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel)

Example 19 with SelectableChannel

use of java.nio.channels.SelectableChannel in project netty by netty.

the class NioEventLoop method rebuildSelector0.

private void rebuildSelector0() {
    final Selector oldSelector = selector;
    final Selector newSelector;
    if (oldSelector == null) {
        return;
    }
    try {
        newSelector = openSelector();
    } catch (Exception e) {
        logger.warn("Failed to create a new Selector.", e);
        return;
    }
    // Register all channels to the new Selector.
    int nChannels = 0;
    for (SelectionKey key : oldSelector.keys()) {
        Object a = key.attachment();
        try {
            if (!key.isValid() || key.channel().keyFor(newSelector) != null) {
                continue;
            }
            int interestOps = key.interestOps();
            key.cancel();
            SelectionKey newKey = key.channel().register(newSelector, interestOps, a);
            if (a instanceof AbstractNioChannel) {
                // Update SelectionKey
                ((AbstractNioChannel) a).selectionKey = newKey;
            }
            nChannels++;
        } catch (Exception e) {
            logger.warn("Failed to re-register a Channel to the new Selector.", e);
            if (a instanceof AbstractNioChannel) {
                AbstractNioChannel ch = (AbstractNioChannel) a;
                ch.unsafe().close(ch.unsafe().voidPromise());
            } else {
                @SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
                invokeChannelUnregistered(task, key, e);
            }
        }
    }
    selector = newSelector;
    try {
        // time to close the old selector as everything else is registered to the new one
        oldSelector.close();
    } catch (Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn("Failed to close the old Selector.", t);
        }
    }
    logger.info("Migrated " + nChannels + " channel(s) to the new Selector.");
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) CancelledKeyException(java.nio.channels.CancelledKeyException) EventLoopException(io.netty.channel.EventLoopException) IOException(java.io.IOException) ChannelException(io.netty.channel.ChannelException) Selector(java.nio.channels.Selector)

Example 20 with SelectableChannel

use of java.nio.channels.SelectableChannel in project netty by netty.

the class NioEventLoop method closeAll.

private void closeAll() {
    selectAgain();
    Set<SelectionKey> keys = selector.keys();
    Collection<AbstractNioChannel> channels = new ArrayList<AbstractNioChannel>(keys.size());
    for (SelectionKey k : keys) {
        Object a = k.attachment();
        if (a instanceof AbstractNioChannel) {
            channels.add((AbstractNioChannel) a);
        } else {
            k.cancel();
            @SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
            invokeChannelUnregistered(task, k, null);
        }
    }
    for (AbstractNioChannel ch : channels) {
        ch.unsafe().close(ch.unsafe().voidPromise());
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) ArrayList(java.util.ArrayList)

Aggregations

SelectableChannel (java.nio.channels.SelectableChannel)25 SelectionKey (java.nio.channels.SelectionKey)13 IOException (java.io.IOException)5 Selector (java.nio.channels.Selector)4 SocketChannel (java.nio.channels.SocketChannel)4 Test (org.junit.Test)4 SocketTimeoutException (java.net.SocketTimeoutException)3 CancelledKeyException (java.nio.channels.CancelledKeyException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 Socket (org.zeromq.ZMQ.Socket)3 ConnectException (java.net.ConnectException)2 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)2 ChannelException (io.netty.channel.ChannelException)1 EventLoopException (io.netty.channel.EventLoopException)1 FileDescriptor (java.io.FileDescriptor)1 Socket (java.net.Socket)1