Search in sources :

Example 16 with Selector

use of java.nio.channels.Selector in project hazelcast by hazelcast.

the class NonBlockingIOThread method rebuildSelector.

// this method is always invoked in this thread
// after we have blocked for selector.select in #runSelectLoopWithSelectorFix
private void rebuildSelector() {
    selectorRebuildCount.inc();
    Selector newSelector = newSelector(logger);
    Selector oldSelector = this.selector;
    // reset each handler's selectionKey, cancel the old keys
    for (SelectionKey key : oldSelector.keys()) {
        AbstractHandler handler = (AbstractHandler) key.attachment();
        SelectableChannel channel = key.channel();
        try {
            int ops = key.interestOps();
            SelectionKey newSelectionKey = channel.register(newSelector, ops, handler);
            handler.setSelectionKey(newSelectionKey);
        } catch (ClosedChannelException e) {
            logger.info("Channel was closed while trying to register with new selector.");
        } catch (CancelledKeyException e) {
            // a CancelledKeyException may be thrown in key.interestOps
            // in this case, since the key is already cancelled, just do nothing
            EmptyStatement.ignore(e);
        }
        key.cancel();
    }
    // close the old selector and substitute with new one
    closeSelector();
    this.selector = newSelector;
    logger.warning("Recreated Selector because of possible java/network stack bug.");
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) SelectableChannel(java.nio.channels.SelectableChannel) CancelledKeyException(java.nio.channels.CancelledKeyException) Selector(java.nio.channels.Selector)

Example 17 with Selector

use of java.nio.channels.Selector in project hazelcast by hazelcast.

the class SelectorOptimizerTest method optimize.

@Test
public void optimize() throws Exception {
    Selector selector = Selector.open();
    assumeTrue(findOptimizableSelectorClass(selector) != null);
    SelectorOptimizer.SelectionKeysSet keys = SelectorOptimizer.optimize(selector, logger);
    assertNotNull(keys);
}
Also used : Selector(java.nio.channels.Selector) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with Selector

use of java.nio.channels.Selector in project hazelcast by hazelcast.

the class SocketAcceptorThread method shutdown.

public synchronized void shutdown() {
    if (!live) {
        return;
    }
    logger.finest("Shutting down SocketAcceptor thread.");
    live = false;
    Selector sel = selector;
    if (sel != null) {
        sel.wakeup();
    }
    try {
        join(SHUTDOWN_TIMEOUT_MILLIS);
    } catch (InterruptedException e) {
        logger.finest(e);
    }
}
Also used : Selector(java.nio.channels.Selector)

Example 19 with Selector

use of java.nio.channels.Selector in project robovm by robovm.

the class SelectorTest method testLeakingPipes.

// We previously leaked a file descriptor for each selector instance created.
//
// http://code.google.com/p/android/issues/detail?id=5993
// http://code.google.com/p/android/issues/detail?id=4825
public void testLeakingPipes() throws IOException {
    for (int i = 0; i < 2000; i++) {
        Selector selector = Selector.open();
        selector.close();
    }
}
Also used : Selector(java.nio.channels.Selector)

Example 20 with Selector

use of java.nio.channels.Selector in project robovm by robovm.

the class SelectorTest method testNonBlockingConnect_slow.

public void testNonBlockingConnect_slow() throws Exception {
    // Test the case where we have to wait for the connection.
    Selector selector = Selector.open();
    StuckServer ss = new StuckServer(true);
    try {
        SocketChannel sc = SocketChannel.open();
        sc.configureBlocking(false);
        sc.connect(ss.getLocalSocketAddress());
        SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
        assertEquals(1, selector.select());
        assertEquals(SelectionKey.OP_CONNECT, key.readyOps());
        sc.finishConnect();
    } finally {
        selector.close();
        ss.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) StuckServer(tests.net.StuckServer) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)186 SelectionKey (java.nio.channels.SelectionKey)84 IOException (java.io.IOException)70 SocketChannel (java.nio.channels.SocketChannel)51 InetSocketAddress (java.net.InetSocketAddress)38 ByteBuffer (java.nio.ByteBuffer)28 ServerSocketChannel (java.nio.channels.ServerSocketChannel)27 Test (org.junit.Test)14 DatagramChannel (java.nio.channels.DatagramChannel)13 ClosedChannelException (java.nio.channels.ClosedChannelException)12 CancelledKeyException (java.nio.channels.CancelledKeyException)11 NioEndpoint (org.apache.tomcat.util.net.NioEndpoint)10 ClosedSelectorException (java.nio.channels.ClosedSelectorException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 EOFException (java.io.EOFException)7 ServerSocket (java.net.ServerSocket)7 SelectableChannel (java.nio.channels.SelectableChannel)7 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)5 Iterator (java.util.Iterator)5 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)5