use of java.nio.channels.CancelledKeyException in project tomee by apache.
the class MultipointServer method _run.
private void _run() {
// The selectorTimeout ensures that even when there are no IO events,
// this loop will "wake up" and execute at least as frequently as the
// expected heartrate.
//
// We initiate WRITE events (the heartbeats we send) in this loop, so that
// detail is critical.
long selectorTimeout = tracker.getHeartRate();
// For reliability purposes we will actually adjust the selectorTimeout
// on each iteration of the loop, shrinking it down just a little to a
// account for the execution time of the loop itself.
int failed = 0;
while (running.get()) {
runs.record();
final long start = System.nanoTime();
try {
selector.select(selectorTimeout);
failed = 0;
} catch (IOException ex) {
if (failed++ > 100) {
log.fatal("Too many Multipoint Failures. Terminating service.", ex);
return;
}
log.error("Multipoint Failure.", ex);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
final Set keys = selector.selectedKeys();
final Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
final SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
try {
if (key.isAcceptable())
doAccept(key);
if (key.isConnectable())
doConnect(key);
if (key.isReadable())
doRead(key);
if (key.isWritable())
doWrite(key);
} catch (CancelledKeyException | ClosedChannelException ex) {
synchronized (connect) {
final Session session = (Session) key.attachment();
if (session.state != State.CLOSED) {
close(key);
}
}
} catch (IOException ex) {
final Session session = (Session) key.attachment();
session.trace(ex.getClass().getSimpleName() + ": " + ex.getMessage());
close(key);
}
}
// This loop can generate WRITE keys (the heartbeats we send)
for (final SelectionKey key : selector.keys()) {
final Session session = (Session) key.attachment();
try {
if (session != null && session.state == State.HEARTBEAT)
session.tick();
} catch (IOException ex) {
close(key);
}
}
// Here is where we actually will expire missing services
tracker.checkServices();
// Fill 'connections' list if we are fully disconnected
rejoin();
// Connect to anyone in the 'connections' list
initiateConnections();
// Adjust selector timeout so we execute in even increments
// This keeps the heartbeat and rejoin regular
selectorTimeout = adjustedSelectorTimeout(start);
}
log.info("MultipointServer has terminated.");
}
use of java.nio.channels.CancelledKeyException in project webpieces by deanhiller.
the class Helper method processKeys.
public static void processKeys(Set<SelectionKey> keySet, SelectorManager2 mgr, BufferPool pool) {
Iterator<SelectionKey> iter = keySet.iterator();
while (iter.hasNext()) {
SelectionKey key = null;
try {
key = iter.next();
final SelectionKey current = key;
log.trace(() -> current.attachment() + " ops=" + Helper.opType(current.readyOps()) + " acc=" + current.isAcceptable() + " read=" + current.isReadable() + " write" + current.isWritable());
processKey(key, mgr, pool);
// } catch(CancelledKeyException e) {
// log.log(Level.INFO, "Cancelled key may be normal", e);
} catch (IOException e) {
log.error(key.attachment() + "Processing of key failed, closing channel", e);
try {
if (key != null)
key.channel().close();
} catch (Throwable ee) {
log.error(key.attachment() + "Close of channel failed", ee);
}
} catch (CancelledKeyException e) {
final SelectionKey current = key;
//TODO: get rid of this if...else statement by fixing
//CancelledKeyException on linux so the tests don't fail
log.trace(() -> current.attachment() + "Processing of key failed, but continuing channel manager loop", e);
} catch (Throwable e) {
log.error(key.attachment() + "Processing of key failed, but continuing channel manager loop", e);
try {
key.cancel();
} catch (Throwable ee) {
log.info("cancelling key failed. exception type=" + ee.getClass() + " msg=" + ee.getMessage());
}
}
}
//clear the whole keySet as we processed them all in the while loop.
//If you do not clear the keySet, keys that have been already processed stay
//in the selected Key set. If another key gets added to this set, the selector
//goes off again and has the stale key plus the new key and the stale key
//is processed again.
keySet.clear();
}
Aggregations