use of java.nio.channels.ClosedSelectorException in project voltdb by VoltDB.
the class SocketJoiner method runPrimary.
/*
* After startup everything is a primary and can accept
* new nodes into the cluster. This loop accepts the new socket
* and passes it off the HostMessenger via the JoinHandler interface
*/
private void runPrimary() throws Exception {
try {
// start the server socket on the right interface
doBind();
while (true) {
try {
final int selectedKeyCount = m_selector.select();
if (selectedKeyCount == 0)
continue;
Set<SelectionKey> selectedKeys = m_selector.selectedKeys();
try {
for (SelectionKey key : selectedKeys) {
processSSC((ServerSocketChannel) key.channel());
}
} finally {
selectedKeys.clear();
}
} catch (ClosedByInterruptException e) {
throw new InterruptedException();
} catch (ClosedSelectorException e) {
throw new InterruptedException();
} catch (Exception e) {
LOG.error("fault occurrent in the connection accept loop", e);
}
}
} finally {
for (ServerSocketChannel ssc : m_listenerSockets) {
try {
ssc.close();
} catch (IOException e) {
}
}
m_listenerSockets.clear();
try {
m_selector.close();
} catch (IOException e) {
}
m_selector = null;
}
}
use of java.nio.channels.ClosedSelectorException in project robovm by robovm.
the class ClosedSelectorExceptionTest method test_Constructor.
/**
* @tests {@link java.nio.channels.ClosedSelectorException#ClosedSelectorException()}
*/
public void test_Constructor() {
ClosedSelectorException e = new ClosedSelectorException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
use of java.nio.channels.ClosedSelectorException in project robovm by robovm.
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();
}
}
use of java.nio.channels.ClosedSelectorException in project jdk8u_jdk by JetBrains.
the class WindowsSelectorImpl method implRegister.
protected void implRegister(SelectionKeyImpl ski) {
synchronized (closeLock) {
if (pollWrapper == null)
throw new ClosedSelectorException();
growIfNeeded();
channelArray[totalChannels] = ski;
ski.setIndex(totalChannels);
fdMap.put(ski);
keys.add(ski);
pollWrapper.addEntry(totalChannels, ski);
totalChannels++;
}
}
use of java.nio.channels.ClosedSelectorException in project geode by apache.
the class AcceptorImpl method runSelectorLoop.
public void runSelectorLoop() {
// int zeroEventsCount = 0;
try {
logger.info(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_SELECTOR_ENABLED));
while (this.selector.isOpen() && !Thread.currentThread().isInterrupted()) {
{
SystemFailure.checkFailure();
// this.cache.getDistributedSystem().getCancelCriterion().checkCancelInProgress(null);
if (this.cache.isClosed()) {
// TODO should just ask cache's CancelCriterion
break;
}
if (this.cache.getCancelCriterion().isCancelInProgress()) {
break;
}
ServerConnection sc;
registeredKeys = checkRegisteredKeys(registeredKeys);
if (registeredKeys == 0) {
// do blocking wait on queue until we get some guys registered
// with the selector
sc = (ServerConnection) this.selectorQueue.take();
} else {
// we already have some guys registered so just do a poll on queue
sc = (ServerConnection) this.selectorQueue.poll();
}
while (sc != null) {
try {
sc.registerWithSelector2(this.selector);
registeredKeys++;
this.selectorRegistrations.add(sc);
} catch (ClosedChannelException cce) {
// for bug bug 38474
finishCon(sc);
} catch (IOException ex) {
finishCon(sc);
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING, ex));
} catch (RuntimeException ex) {
finishCon(sc);
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING, ex));
}
sc = (ServerConnection) this.selectorQueue.poll();
}
}
if (registeredKeys == 0) {
continue;
}
int events = this.selector.select();
// select() could have returned due to wakeup() during close of cache
if (this.cache.getCancelCriterion().isCancelInProgress()) {
break;
}
if (events == 0) {
// zeroEventsCount++;
// if (zeroEventsCount > 0) {
// zeroEventsCount = 0;
checkForStuckKeys();
// try {
// this.selector.close(); // this selector is sick!
// } catch (IOException ignore) {
// }
// this.selector = Selector.open();
// {
// Iterator it = selectorRegistrations.iterator();
// while (it.hasNext()) {
// ServerConnection sc = (ServerConnection)it.next();
// sc.registerWithSelector2(this.selector);
// }
// }
// }
// ArrayList al = new ArrayList();
// Iterator keysIt = this.selector.keys().iterator();
// while (keysIt.hasNext()) {
// SelectionKey sk = (SelectionKey)keysIt.next();
// al.add(sk.attachment());
// sk.cancel();
// }
// events = this.selector.selectNow();
// Iterator alIt = al.iterator();
// while (alIt.hasNext()) {
// ServerConnection sc = (ServerConnection)alIt.next();
// sc.registerWithSelector2(this.selector);
// }
// events = this.selector.select();
// } else {
// zeroEventsCount = 0;
}
while (events > 0) {
int cancelCount = 0;
Set sk = this.selector.selectedKeys();
if (sk == null) {
// something really bad has happened I'm not even sure this is possible
// but lhughes so an NPE during close one time so perhaps it can happen
// during selector close.
events = 0;
break;
}
Iterator keysIterator = sk.iterator();
while (keysIterator.hasNext()) {
SelectionKey key = (SelectionKey) keysIterator.next();
// Remove the key from the selector's selectedKeys
keysIterator.remove();
final ServerConnection sc = (ServerConnection) key.attachment();
try {
if (key.isValid() && key.isReadable()) {
// this is the only event we currently register for
try {
key.cancel();
this.selectorRegistrations.remove(sc);
registeredKeys--;
cancelCount++;
sc.makeBlocking();
// we need to say we are processing a message
// so that that client health monitor will not
// kill us while we wait for a thread in the thread pool.
// This is also be used to determine how long we are
// in the thread pool queue and to cancel operations that
// have waited too long in the queue.
sc.setProcessingMessage();
} catch (ClosedChannelException ignore) {
finishCon(sc);
continue;
} catch (IOException ex) {
finishCon(sc);
if (isRunning()) {
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, ex));
}
continue;
}
try {
AcceptorImpl.this.stats.incThreadQueueSize();
AcceptorImpl.this.pool.execute(sc);
} catch (RejectedExecutionException rejected) {
finishCon(sc);
AcceptorImpl.this.stats.decThreadQueueSize();
if (!isRunning()) {
break;
}
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, rejected));
}
// } else if (key.isValid() && key.isConnectable()) {
// logger.info("DEBUG isConnectable and isValid key=" + key);
// finishCon(sc);
} else {
finishCon(sc);
if (key.isValid()) {
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING_EVENT_ON_SELECTOR_KEY__0, key));
// } else {
// logger.info("DEBUG !isValid key=" + key);
}
}
} catch (CancelledKeyException ex) {
// fix for bug 37739
finishCon(sc);
}
}
if (cancelCount > 0 && this.selector.isOpen()) {
// we need to do a select to cause the cancel to be unregisters.
events = this.selector.selectNow();
} else {
events = 0;
}
}
}
} catch (InterruptedException ex) {
// allow this thread to die
Thread.currentThread().interrupt();
} catch (ClosedSelectorException ex) {
// allow this thread to exit
} catch (IOException ex) {
logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, ex));
} finally {
try {
drainSelectorQueue();
} finally {
// note that if this method was called by close then the
// following call is a noop since the first thing it does
// is call isRunning.
// make sure this is called to fix bug 37749
close();
}
}
}
Aggregations