use of java.nio.channels.Selector in project ignite by apache.
the class GridNioServer method createSelector.
/**
* Creates selector and binds server socket to a given address and port. If address is null
* then will not bind any address and just creates a selector.
*
* @param addr Local address to listen on.
* @return Created selector.
* @throws IgniteCheckedException If selector could not be created or port is already in use.
*/
private Selector createSelector(@Nullable SocketAddress addr) throws IgniteCheckedException {
Selector selector = null;
ServerSocketChannel srvrCh = null;
try {
// Create a new selector
selector = SelectorProvider.provider().openSelector();
if (addr != null) {
// Create a new non-blocking server socket channel
srvrCh = ServerSocketChannel.open();
srvrCh.configureBlocking(false);
if (sockRcvBuf > 0)
srvrCh.socket().setReceiveBufferSize(sockRcvBuf);
// Bind the server socket to the specified address and port
srvrCh.socket().bind(addr);
// Register the server socket channel, indicating an interest in
// accepting new connections
srvrCh.register(selector, SelectionKey.OP_ACCEPT);
}
return selector;
} catch (Throwable e) {
U.close(srvrCh, log);
U.close(selector, log);
if (e instanceof Error)
throw (Error) e;
throw new IgniteCheckedException("Failed to initialize NIO selector.", e);
}
}
use of java.nio.channels.Selector in project jetty.project by eclipse.
the class NIOTest method testSelector.
@Test
public void testSelector() throws Exception {
ServerSocket acceptor = new ServerSocket(0);
Selector selector = Selector.open();
// Create client server socket pair
SocketChannel client = SocketChannel.open(acceptor.getLocalSocketAddress());
Socket server = acceptor.accept();
server.setTcpNoDelay(true);
// Make the client non blocking and register it with selector for reads
client.configureBlocking(false);
SelectionKey key = client.register(selector, SelectionKey.OP_READ);
// assert it is not selected
assertTrue(key.isValid());
assertFalse(key.isReadable());
assertEquals(0, key.readyOps());
// try selecting and assert nothing selected
int selected = selector.selectNow();
assertEquals(0, selected);
assertEquals(0, selector.selectedKeys().size());
assertTrue(key.isValid());
assertFalse(key.isReadable());
assertEquals(0, key.readyOps());
// Write a byte from server to client
server.getOutputStream().write(42);
server.getOutputStream().flush();
// select again and assert selection found for read
selected = selector.select(1000);
assertEquals(1, selected);
assertEquals(1, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// select again and see that it is not reselect, but stays selected
selected = selector.select(100);
assertEquals(0, selected);
assertEquals(1, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// read the byte
ByteBuffer buf = ByteBuffer.allocate(1024);
int len = client.read(buf);
assertEquals(1, len);
buf.flip();
assertEquals(42, buf.get());
buf.clear();
// But this does not change the key
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// Even if we select again ?
selected = selector.select(100);
assertEquals(0, selected);
assertEquals(1, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// Unless we remove the key from the select set
// and then it is still flagged as isReadable()
selector.selectedKeys().clear();
assertEquals(0, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// Now if we select again - it is still flagged as readable!!!
selected = selector.select(100);
assertEquals(0, selected);
assertEquals(0, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isReadable());
assertEquals(1, key.readyOps());
// Only when it is selected for something else does that state change.
key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
selected = selector.select(1000);
assertEquals(1, selected);
assertEquals(1, selector.selectedKeys().size());
assertTrue(key.isValid());
assertTrue(key.isWritable());
assertFalse(key.isReadable());
assertEquals(SelectionKey.OP_WRITE, key.readyOps());
}
use of java.nio.channels.Selector in project hazelcast by hazelcast.
the class SocketAcceptorThread method rebuildSelector.
private void rebuildSelector() throws IOException {
selectorRecreateCount.inc();
// cancel existing selection key, register new one on the new selector
selectionKey.cancel();
closeSelector();
Selector newSelector = Selector.open();
selector = newSelector;
selectionKey = serverSocketChannel.register(newSelector, SelectionKey.OP_ACCEPT);
}
use of java.nio.channels.Selector in project tomcat by apache.
the class NioReceiver method closeSelector.
private void closeSelector() throws IOException {
Selector selector = this.selector.getAndSet(null);
if (selector == null)
return;
try {
Iterator<SelectionKey> it = selector.keys().iterator();
// look at each key in the selected set
while (it.hasNext()) {
SelectionKey key = it.next();
key.channel().close();
key.attach(null);
key.cancel();
}
} catch (IOException ignore) {
if (log.isWarnEnabled()) {
log.warn(sm.getString("nioReceiver.cleanup.fail"), ignore);
}
} catch (ClosedSelectorException ignore) {
// Ignore
}
try {
selector.selectNow();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
// Ignore everything else
}
selector.close();
}
use of java.nio.channels.Selector in project tomcat by apache.
the class NioReceiver method stopListening.
/**
* Close Selector.
*
* @see org.apache.catalina.tribes.transport.ReceiverBase#stop()
*/
protected void stopListening() {
setListen(false);
Selector selector = this.selector.get();
if (selector != null) {
try {
// Unlock the thread if is is blocked waiting for input
selector.wakeup();
// Wait for the receiver thread to finish
int count = 0;
while (running && count < 50) {
Thread.sleep(100);
count++;
}
if (running) {
log.warn(sm.getString("nioReceiver.stop.threadRunning"));
}
closeSelector();
} catch (Exception x) {
log.error(sm.getString("nioReceiver.stop.fail"), x);
} finally {
this.selector.set(null);
}
}
}
Aggregations