Search in sources :

Example 56 with Selector

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

the class SelectorFactory method reduce.

/**
     * Decrease <code>Selector</code> pool size
     */
private static void reduce(int size) {
    for (int i = 0; i < maxSelectors - size; i++) {
        try {
            Selector selector = selectors.pop();
            selector.close();
        } catch (IOException e) {
            logger.error("SelectorFactory.reduce", e);
        }
    }
}
Also used : IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 57 with Selector

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

the class SelectorFactory method getSelector.

/**
     * Get a exclusive <code>Selector</code>
     * 
     * @return <code>Selector</code>
     */
public static final Selector getSelector() {
    synchronized (selectors) {
        Selector s = null;
        try {
            if (selectors.size() != 0) {
                s = selectors.pop();
            }
        } catch (EmptyStackException ex) {
        }
        int attempts = 0;
        try {
            while (s == null && attempts < 2) {
                selectors.wait(timeout);
                try {
                    if (selectors.size() != 0) {
                        s = selectors.pop();
                    }
                } catch (EmptyStackException ex) {
                    break;
                }
                attempts++;
            }
        } catch (InterruptedException ex) {
        }
        return s;
    }
}
Also used : EmptyStackException(java.util.EmptyStackException) Selector(java.nio.channels.Selector)

Example 58 with Selector

use of java.nio.channels.Selector in project blade by biezhi.

the class ManagedSelector method dump.

@Override
public void dump(Appendable out, String indent) throws IOException {
    out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_id)).append(System.lineSeparator());
    Selector selector = _selector;
    if (selector != null && selector.isOpen()) {
        final ArrayList<Object> dump = new ArrayList<>(selector.keys().size() * 2);
        DumpKeys dumpKeys = new DumpKeys(dump);
        submit(dumpKeys);
        dumpKeys.await(5, TimeUnit.SECONDS);
        ContainerLifeCycle.dump(out, indent, dump);
    }
}
Also used : ArrayList(java.util.ArrayList) Selector(java.nio.channels.Selector)

Example 59 with Selector

use of java.nio.channels.Selector in project blade by biezhi.

the class ManagedSelector method submit.

public void submit(Runnable change) {
    if (LOG.isDebugEnabled())
        LOG.debug("Queued change {} on {}", change, this);
    Selector selector = null;
    try (Locker.Lock lock = _locker.lock()) {
        _actions.offer(change);
        if (_selecting) {
            selector = _selector;
            // To avoid the extra select wakeup.
            _selecting = false;
        }
    }
    if (selector != null)
        selector.wakeup();
}
Also used : Locker(org.eclipse.jetty.util.thread.Locker) Selector(java.nio.channels.Selector)

Example 60 with Selector

use of java.nio.channels.Selector in project http-kit by http-kit.

the class PerformanceBench method main.

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println("concurrency: " + concurrency + "; total requests: " + total);
    Selector selector = Selector.open();
    for (int i = 0; i < concurrency; ++i) {
        if (i % 100 == 0) {
            Thread.sleep(10);
        }
        connect(addr, selector);
    }
    start = System.currentTimeMillis();
    while (true) {
        int select = selector.select();
        D("selec return " + select);
        if (select > 0) {
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> it = selectedKeys.iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                if (key.isConnectable()) {
                    SocketChannel ch = (SocketChannel) key.channel();
                    try {
                        if (ch.finishConnect()) {
                            key.interestOps(SelectionKey.OP_WRITE);
                        }
                    } catch (Exception e) {
                        ch.close();
                        e.printStackTrace();
                        D(e.getMessage() + "\t" + "close and reconnect");
                        // reconnect
                        connect(addr, selector);
                    }
                } else if (key.isWritable()) {
                    doWrite(key);
                } else if (key.isReadable()) {
                    doRead(selector, key);
                }
            }
            selectedKeys.clear();
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)85 SelectionKey (java.nio.channels.SelectionKey)43 IOException (java.io.IOException)26 SocketChannel (java.nio.channels.SocketChannel)24 InetSocketAddress (java.net.InetSocketAddress)17 ServerSocketChannel (java.nio.channels.ServerSocketChannel)17 ByteBuffer (java.nio.ByteBuffer)10 ClosedChannelException (java.nio.channels.ClosedChannelException)8 DatagramChannel (java.nio.channels.DatagramChannel)8 CancelledKeyException (java.nio.channels.CancelledKeyException)6 ClosedSelectorException (java.nio.channels.ClosedSelectorException)4 SelectableChannel (java.nio.channels.SelectableChannel)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Test (org.junit.Test)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)3 BigDecimal (java.math.BigDecimal)2 ConnectException (java.net.ConnectException)2 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2