use of java.nio.channels.ClosedSelectorException in project tomcat70 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("Unable to cleanup on selector close.", ignore);
}
} catch (ClosedSelectorException ignore) {
}
selector.close();
}
use of java.nio.channels.ClosedSelectorException in project jeromq by zeromq.
the class Poller method run.
@Override
public void run() {
int returnsImmediately = 0;
while (!stopping.get()) {
// Execute any due timers.
long timeout = executeTimers();
if (retired) {
retired = false;
Iterator<Handle> iter = fdTable.iterator();
while (iter.hasNext()) {
Handle handle = iter.next();
SelectionKey key = handle.fd.keyFor(selector);
if (handle.cancelled || !handle.fd.isOpen()) {
if (key != null) {
key.cancel();
}
iter.remove();
continue;
}
if (key == null) {
if (handle.fd.isOpen()) {
try {
key = handle.fd.register(selector, handle.ops, handle);
assert (key != null);
} catch (CancelledKeyException | ClosedSelectorException | ClosedChannelException e) {
exnotification.uncaughtException(worker, e);
}
}
} else if (key.isValid()) {
key.interestOps(handle.ops);
}
}
}
// Wait for events.
int rc;
long start = System.currentTimeMillis();
try {
rc = selector.select(timeout);
} catch (ClosedSelectorException e) {
rebuildSelector();
exnotification.uncaughtException(worker, e);
ctx.errno().set(ZError.EINTR);
continue;
} catch (IOException e) {
throw new ZError.IOException(e);
}
// in checking the keys.
if (rc == 0) {
returnsImmediately = maybeRebuildSelector(returnsImmediately, timeout, start);
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
Handle pollset = (Handle) key.attachment();
it.remove();
if (pollset.cancelled) {
continue;
}
try {
if (key.isValid() && key.isAcceptable()) {
pollset.handler.acceptEvent();
}
if (key.isValid() && key.isConnectable()) {
pollset.handler.connectEvent();
}
if (key.isValid() && key.isWritable()) {
pollset.handler.outEvent();
}
if (key.isValid() && key.isReadable()) {
pollset.handler.inEvent();
}
} catch (RuntimeException e) {
exnotification.uncaughtException(worker, e);
}
}
}
stopped.countDown();
}
use of java.nio.channels.ClosedSelectorException in project jeromq by zeromq.
the class ZMQ method poll.
/**
* Polling on items with given selector
* CAUTION: This could be affected by jdk epoll bug
*
* @param selector Open and reuse this selector and do not forget to close when it is not used.
* @param items
* @param count
* @param timeout
* @return number of events
*/
public static int poll(Selector selector, PollItem[] items, int count, long timeout) {
Utils.checkArgument(items != null, "items have to be supplied for polling");
if (count == 0) {
if (timeout <= 0) {
return 0;
}
LockSupport.parkNanos(TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS));
return 0;
}
long now = 0L;
long end = 0L;
HashMap<SelectableChannel, SelectionKey> saved = new HashMap<SelectableChannel, SelectionKey>();
for (SelectionKey key : selector.keys()) {
if (key.isValid()) {
saved.put(key.channel(), key);
}
}
for (int i = 0; i < count; i++) {
PollItem item = items[i];
if (item == null) {
continue;
}
// mailbox channel if ZMQ socket
SelectableChannel ch = item.getChannel();
SelectionKey key = saved.remove(ch);
if (key != null) {
if (key.interestOps() != item.interestOps()) {
key.interestOps(item.interestOps());
}
key.attach(item);
} else {
try {
ch.register(selector, item.interestOps(), item);
} catch (ClosedSelectorException e) {
// context was closed asynchronously, exit gracefully
return -1;
} catch (ClosedChannelException e) {
throw new ZError.IOException(e);
}
}
}
if (!saved.isEmpty()) {
for (SelectionKey deprecated : saved.values()) {
deprecated.cancel();
}
}
boolean firstPass = true;
int nevents = 0;
int ready;
while (true) {
// Compute the timeout for the subsequent poll.
long waitMillis;
if (firstPass) {
waitMillis = 0L;
} else if (timeout < 0L) {
waitMillis = -1L;
} else {
waitMillis = TimeUnit.NANOSECONDS.toMillis(end - now);
if (waitMillis == 0) {
waitMillis = 1L;
}
}
// Wait for events.
try {
int rc;
if (waitMillis < 0) {
rc = selector.select(0);
} else if (waitMillis == 0) {
rc = selector.selectNow();
} else {
rc = selector.select(waitMillis);
}
for (SelectionKey key : selector.keys()) {
PollItem item = (PollItem) key.attachment();
ready = item.readyOps(key, rc);
if (ready < 0) {
return -1;
}
if (ready > 0) {
nevents++;
}
}
selector.selectedKeys().clear();
} catch (ClosedSelectorException e) {
// context was closed asynchronously, exit gracefully
return -1;
} catch (IOException e) {
throw new ZError.IOException(e);
}
// If timeout is zero, exit immediately whether there are events or not.
if (timeout == 0) {
break;
}
if (nevents > 0) {
break;
}
// If timeout is infinite we can just loop until we get some events.
if (timeout < 0) {
if (firstPass) {
firstPass = false;
}
continue;
}
// when the polling should time out.
if (firstPass) {
now = Clock.nowNS();
end = now + TimeUnit.MILLISECONDS.toNanos(timeout);
if (now == end) {
break;
}
firstPass = false;
continue;
}
// Find out whether timeout have expired.
now = Clock.nowNS();
if (now >= end) {
break;
}
}
return nevents;
}
use of java.nio.channels.ClosedSelectorException in project jeromq by zeromq.
the class PollerTest method catchNotification.
@Test(timeout = 1000)
public void catchNotification() throws IOException, InterruptedException {
CountDownLatch catched = new CountDownLatch(1);
AtomicReference<Selector> selectorRef = new AtomicReference<>();
Ctx ctx = new Ctx() {
@Override
public Selector createSelector() {
selectorRef.set(super.createSelector());
return selectorRef.get();
}
};
ctx.setNotificationExceptionHandler((t, e) -> {
if (e instanceof ClosedSelectorException) {
catched.countDown();
}
});
Poller poller = new Poller(ctx, "test");
poller.start();
selectorRef.get().close();
catched.await();
}
use of java.nio.channels.ClosedSelectorException in project zm-mailbox by Zimbra.
the class Client method run.
@Override
public void run() {
while (!shutdown) {
try {
// thread or timeout expires
synchronized (clientThread) {
if (!hasBusyPeer()) {
clientThread.wait(waitInterval);
}
}
selector.select();
for (Iterator<SelectionKey> iter = selector.selectedKeys().iterator(); iter.hasNext(); ) {
SelectionKey key = iter.next();
iter.remove();
if (key.isValid()) {
PeerServer peer = (PeerServer) key.attachment();
try {
if (key.isConnectable()) {
log.debug("client:connecting to %s %s:%d", peer.id, peer.hostname, peer.port);
peer.finishConnect();
}
if (peer.isBacklogEmpty()) {
peer.unsetActive();
continue;
}
if (key.isWritable()) {
peer.write();
}
} catch (IOException e) {
log.debug("socket operation failed. retry connect %s", peer.id, e);
// retry
key.cancel();
peer.unsetActive();
peer.connect();
continue;
}
}
}
} catch (Throwable e) {
if (e instanceof OutOfMemoryError) {
break;
}
if (!(e instanceof ClosedSelectorException)) {
// log the error and continue
log.warn("writing to peer server", e);
continue;
}
}
}
}
Aggregations