use of java.nio.channels.ClosedSelectorException in project JGroups by belaban.
the class MemcachedConnector method run.
public void run() {
System.out.println("MemcachedConnector listening on " + srv_sock.getLocalSocketAddress());
while (thread != null && Thread.currentThread().equals(thread)) {
Socket client_sock = null;
try {
client_sock = srv_sock.accept();
// System.out.println("ACCEPT: " + client_sock.getRemoteSocketAddress());
final RequestHandler handler = new RequestHandler(client_sock);
/*new Thread() {
public void run() {
handler.run();
}
}.start();
*/
thread_pool.execute(handler);
} catch (ClosedSelectorException closed) {
Util.close(client_sock);
break;
} catch (Throwable e) {
}
}
}
use of java.nio.channels.ClosedSelectorException in project Mindustry by Anuken.
the class KryoServer method host.
@Override
public void host(int port) throws IOException {
lastconnection = 0;
connections.clear();
missing.clear();
server.bind(port, port);
webServer = new SocketServer(Vars.webPort);
webServer.start();
serverThread = new Thread(() -> {
try {
server.run();
} catch (Throwable e) {
if (!(e instanceof ClosedSelectorException))
handleException(e);
}
}, "Kryonet Server");
serverThread.setDaemon(true);
serverThread.start();
}
use of java.nio.channels.ClosedSelectorException in project http-kit by http-kit.
the class HttpServer method run.
public void run() {
while (true) {
try {
PendingKey k;
while (!pending.isEmpty()) {
k = pending.poll();
if (k.Op == PendingKey.OP_WRITE) {
if (k.key.isValid()) {
k.key.interestOps(OP_WRITE);
}
} else {
closeKey(k.key, k.Op);
}
}
if (selector.select() <= 0) {
continue;
}
Set<SelectionKey> selectedKeys = selector.selectedKeys();
for (SelectionKey key : selectedKeys) {
keptAlive.remove(key);
// run hours happily after commented, but not sure.
if (!key.isValid()) {
continue;
}
if (key.isAcceptable()) {
accept(key);
} else if (key.isReadable()) {
doRead(key);
} else if (key.isWritable()) {
doWrite(key);
}
}
selectedKeys.clear();
} catch (ClosedSelectorException ignore) {
// stopped
return;
// do not exits the while IO event loop. if exits, then will not process any IO event
// jvm can catch any exception, including OOM
} catch (Throwable e) {
// catch any exception(including OOM), print it
status.set(Status.STOPPED);
errorLogger.log("http server loop error, should not happen", e);
eventLogger.log(eventNames.serverLoopError);
}
}
}
use of java.nio.channels.ClosedSelectorException in project j2objc by google.
the class SelectorTest method test_keys.
/**
* @tests java.nio.channels.Selector#keys()
*/
public void test_keys() throws IOException {
SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
Set<SelectionKey> keySet = selector.keys();
Set<SelectionKey> keySet2 = selector.keys();
assertSame(keySet, keySet2);
assertEquals(1, keySet.size());
SelectionKey key2 = keySet.iterator().next();
assertEquals(key, key2);
// Any attempt to modify keys will cause UnsupportedOperationException
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
SelectionKey key3 = sc.register(selector, SelectionKey.OP_READ);
try {
keySet2.add(key3);
fail("should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
try {
keySet2.remove(key3);
fail("should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
try {
keySet2.clear();
fail("should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
selector.close();
try {
selector.keys();
fail("should throw ClosedSelectorException");
} catch (ClosedSelectorException e) {
// expected
}
}
use of java.nio.channels.ClosedSelectorException in project j2objc by google.
the class SelectorTest method test_selectedKeys.
/**
* @tests java.nio.channels.Selector#keys()
*/
public void test_selectedKeys() throws IOException {
SocketChannel sc = SocketChannel.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
try {
int count = 0;
sc.connect(localAddress);
count = blockingSelect(SelectType.NULL, 0);
assertEquals(1, count);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Set<SelectionKey> selectedKeys2 = selector.selectedKeys();
assertSame(selectedKeys, selectedKeys2);
assertEquals(1, selectedKeys.size());
assertEquals(ssc.keyFor(selector), selectedKeys.iterator().next());
// add one key into selectedKeys
try {
selectedKeys.add(ssc.keyFor(selector));
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
// no exception should be thrown
selectedKeys.clear();
Set<SelectionKey> selectedKeys3 = selector.selectedKeys();
assertSame(selectedKeys, selectedKeys3);
ssc.keyFor(selector).cancel();
assertEquals(0, selectedKeys.size());
selector.close();
try {
selector.selectedKeys();
fail("should throw ClosedSelectorException");
} catch (ClosedSelectorException e) {
// expected
}
} finally {
sc.close();
}
}
Aggregations