use of java.nio.channels.Selector in project cobar by alibaba.
the class NIOAcceptor method run.
@Override
public void run() {
final Selector selector = this.selector;
for (; ; ) {
++acceptCount;
try {
selector.select(1000L);
Set<SelectionKey> keys = selector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Throwable e) {
LOGGER.warn(getName(), e);
}
}
}
use of java.nio.channels.Selector in project cobar by alibaba.
the class NIOConnector method run.
@Override
public void run() {
final Selector selector = this.selector;
for (; ; ) {
++connectCount;
try {
selector.select(1000L);
connect(selector);
Set<SelectionKey> keys = selector.selectedKeys();
try {
for (SelectionKey key : keys) {
Object att = key.attachment();
if (att != null && key.isValid() && key.isConnectable()) {
finishConnect(key, att);
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Throwable e) {
LOGGER.warn(name, e);
}
}
}
use of java.nio.channels.Selector in project robovm by robovm.
the class AbstractSelectableChannelTest method test_configureBlocking_Z_IllegalBlockingMode.
/**
* @tests AbstractSelectableChannel#configureBlocking(boolean)
*/
public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
Selector acceptSelector = SelectorProvider.provider().openSelector();
SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null);
assertEquals(sc.keyFor(acceptSelector), acceptKey);
SelectableChannel getChannel = sc.configureBlocking(false);
assertEquals(getChannel, sc);
try {
sc.configureBlocking(true);
fail("Should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
}
use of java.nio.channels.Selector in project robovm by robovm.
the class AbstractSelectableChannelTest method test_register_LSelectorILObject_IllegalArgument.
/**
* @tests AbstractSelectableChannel#register(Selector, int, Object)
*/
public void test_register_LSelectorILObject_IllegalArgument() throws IOException {
Selector acceptSelector = SelectorProvider.provider().openSelector();
assertTrue(acceptSelector.isOpen());
MockSelectableChannel msc = new MockSelectableChannel(SelectorProvider.provider());
msc.configureBlocking(false);
// in nonblocking mode
try {
//different SelectionKey with validOps
msc.register(acceptSelector, SelectionKey.OP_READ, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
msc.register(null, 0, null);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
// in nonblocking mode, if selector closed
acceptSelector.close();
try {
msc.register(acceptSelector, SelectionKey.OP_READ, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
msc.register(null, 0, null);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
try {
msc.register(acceptSelector, 0, null);
fail("Should throw IllegalSelectorException");
} catch (IllegalSelectorException e) {
// expected
}
acceptSelector = SelectorProvider.provider().openSelector();
// test in blocking mode
msc.configureBlocking(true);
try {
msc.register(acceptSelector, SelectionKey.OP_READ, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
msc.register(null, 0, null);
fail("Should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
acceptSelector.close();
// in blocking mode, if selector closed
try {
msc.register(acceptSelector, SelectionKey.OP_READ, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
msc.register(null, 0, null);
fail("Should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
// register with an object
Object argObj = new Object();
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
try {
sc.register(null, SelectionKey.OP_READ, argObj);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
// if channel closed
msc.close();
try {
msc.register(acceptSelector, SelectionKey.OP_READ, null);
fail("Should throw ClosedChannelException");
} catch (ClosedChannelException e) {
// expected
}
}
use of java.nio.channels.Selector in project robovm by robovm.
the class AbstractSelectorTest method test_provider.
/**
* @tests AbstractSelector#provider()
*/
public void test_provider() throws IOException {
Selector mockSelector = new MockAbstractSelector(SelectorProvider.provider());
assertTrue(mockSelector.isOpen());
assertSame(SelectorProvider.provider(), mockSelector.provider());
mockSelector = new MockAbstractSelector(null);
assertNull(mockSelector.provider());
}
Aggregations