java.nio.channels.ClosedSelectorException Java Examples

The following examples show how to use java.nio.channels.ClosedSelectorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TAsyncClientManager.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
private void transitionMethods() {
  try {
    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) {
        // this can happen if the method call experienced an error and the
        // key was cancelled. can also happen if we timeout a method, which
        // results in a channel close.
        // just skip
        continue;
      }
      TAsyncMethodCall methodCall = (TAsyncMethodCall)key.attachment();
      methodCall.transition(key);

      // If done or error occurred, remove from timeout watch set
      if (methodCall.isFinished() || methodCall.getClient().hasError()) {
        timeoutWatchSet.remove(methodCall);
      }
    }
  } catch (ClosedSelectorException e) {
    LOGGER.error("Caught ClosedSelectorException in TAsyncClientManager!", e);
  }
}
 
Example #2
Source File: TAsyncClientManager.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private void transitionMethods() {
  try {
    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) {
        // this can happen if the method call experienced an error and the
        // key was cancelled. can also happen if we timeout a method, which
        // results in a channel close.
        // just skip
        continue;
      }
      TAsyncMethodCall methodCall = (TAsyncMethodCall)key.attachment();
      methodCall.transition(key);

      // If done or error occurred, remove from timeout watch set
      if (methodCall.isFinished() || methodCall.getClient().hasError()) {
        timeoutWatchSet.remove(methodCall);
      }
    }
  } catch (ClosedSelectorException e) {
    LOGGER.error("Caught ClosedSelectorException in TAsyncClientManager!", e);
  }
}
 
Example #3
Source File: TcpConnectorService.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
private void closeAllConnections()
{
    try
    {
        if (serverSelector != null)
        {
            for (SelectionKey currentKey : serverSelector.keys())
            {
                closeConnection(currentKey, false, true);
            }
            serverSelector.close();
        }
    }
    catch (ClosedSelectorException selectExc)
    {
        // Cannot close any connections, because the selector is inoperative
        errorReporter.reportError(selectExc);
    }
    catch (IOException ioExc)
    {
        // If close() fails with an I/O error, the reason may be interesting
        // enough to file an error report
        errorReporter.reportError(ioExc);
    }
}
 
Example #4
Source File: TaskInboundTest.java    From aion with MIT License 6 votes vote down vote up
@Test(timeout = 10_000)
public void testRunException() throws InterruptedException {
    AtomicBoolean atb = new AtomicBoolean(true);
    TaskInbound ti =
            new TaskInbound(p2pLOG, surveyLog, p2pMgr, selector, atb, nodeMgr, hldrMap, rhs1, msgInQue);
    assertNotNull(ti);

    doThrow(ClosedSelectorException.class).when(selector).selectNow();

    Thread t = new Thread(ti);
    t.start();
    assertTrue(t.isAlive());
    Thread.sleep(50);

    atb.set(false);
    while (!t.getState().toString().contains("TERMINATED")) {
        Thread.sleep(10);
    }
}
 
Example #5
Source File: TaskInboundTest.java    From aion with MIT License 6 votes vote down vote up
@Test(timeout = 10_000)
public void testRunClosedSelectorException() throws InterruptedException {
    AtomicBoolean atb = new AtomicBoolean(true);
    TaskInbound ti =
            new TaskInbound(p2pLOG, surveyLog, p2pMgr, selector, atb, nodeMgr, hldrMap, rhs1, msgInQue);
    assertNotNull(ti);

    when(selector.selectNow()).thenReturn(1);
    when(selector.selectedKeys()).thenThrow(ClosedSelectorException.class);

    Thread t = new Thread(ti);
    t.start();
    assertTrue(t.isAlive());
    Thread.sleep(50);
    atb.set(false);
    while (!t.getState().toString().contains("TERMINATED")) {
        Thread.sleep(10);
    }
}
 
Example #6
Source File: WindowsSelectorImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #7
Source File: WindowsSelectorImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #8
Source File: WindowsSelectorImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #9
Source File: WindowsSelectorImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #10
Source File: WindowsSelectorImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #11
Source File: WindowsSelectorImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #12
Source File: WindowsSelectorImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #13
Source File: WindowsSelectorImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #14
Source File: WindowsSelectorImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #15
Source File: WindowsSelectorImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #16
Source File: WindowsSelectorImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #17
Source File: WindowsSelectorImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #18
Source File: WindowsSelectorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #19
Source File: SocketConnection.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int read(byte[] dest, int offset, int max) throws IOException {
    if (max == 0) {
        return 0;
    }

    if (buffer.remaining() == 0) {
        try {
            selector.select();
        } catch (ClosedSelectorException e) {
            return -1;
        }
        if (!selector.isOpen()) {
            return -1;
        }

        buffer.clear();
        int nread = socket.read(buffer);
        buffer.flip();

        if (nread < 0) {
            return -1;
        }
    }

    int count = Math.min(buffer.remaining(), max);
    buffer.get(dest, offset, count);
    return count;
}
 
Example #20
Source File: SocketConnection.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int read(byte[] dest, int offset, int max) throws IOException {
    if (max == 0) {
        return 0;
    }

    if (buffer.remaining() == 0) {
        try {
            selector.select();
        } catch (ClosedSelectorException e) {
            return -1;
        }
        if (!selector.isOpen()) {
            return -1;
        }

        buffer.clear();
        int nread = socket.read(buffer);
        buffer.flip();

        if (nread < 0) {
            return -1;
        }
    }

    int count = Math.min(buffer.remaining(), max);
    buffer.get(dest, offset, count);
    return count;
}
 
Example #21
Source File: WindowsSelectorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #22
Source File: WindowsSelectorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #23
Source File: WindowsSelectorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #24
Source File: ReaderThread.java    From kieker with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	this.active = true;
	while (this.isAlive() && this.active) {
		try {
			final int readReady = this.readSelector.select();

			if (readReady > 0) {
				final Set<SelectionKey> selectedKeys = this.readSelector.selectedKeys();
				final Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

				while (keyIterator.hasNext()) {
					final SelectionKey key = keyIterator.next();

					this.readFromSocket(key);

					keyIterator.remove();
				}

				selectedKeys.clear();
			} else {
				Thread.sleep(100); // wait for the registration of the new key in MultipleConnectionTypSourceStage
			}
		} catch (final ClosedSelectorException e1) {
			this.logger.error("Selector has already been closed.", e1);
		} catch (final IOException e2) {
			this.logger.error("IO error while reading from connection.", e2);
		} catch (final InterruptedException e) {
			this.logger.warn("Thread.sleep was interrupted.");
		}
	}
}
 
Example #25
Source File: WindowsSelectorImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int doSelect(long timeout) throws IOException {
    if (channelArray == null)
        throw new ClosedSelectorException();
    this.timeout = timeout; // set selector timeout
    processDeregisterQueue();
    if (interruptTriggered) {
        resetWakeupSocket();
        return 0;
    }
    // Calculate number of helper threads needed for poll. If necessary
    // threads are created here and start waiting on startLock
    adjustThreadsCount();
    finishLock.reset(); // reset finishLock
    // Wakeup helper threads, waiting on startLock, so they start polling.
    // Redundant threads will exit here after wakeup.
    startLock.startThreads();
    // do polling in the main thread. Main thread is responsible for
    // first MAX_SELECTABLE_FDS entries in pollArray.
    try {
        begin();
        try {
            subSelector.poll();
        } catch (IOException e) {
            finishLock.setException(e); // Save this exception
        }
        // Main thread is out of poll(). Wakeup others and wait for them
        if (threads.size() > 0)
            finishLock.waitForHelperThreads();
      } finally {
          end();
      }
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    finishLock.checkForException();
    processDeregisterQueue();
    int updated = updateSelectedKeys();
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
Example #26
Source File: WindowsSelectorImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #27
Source File: WindowsSelectorImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
 
Example #28
Source File: ClosedSelectorExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @tests {@link java.nio.channels.ClosedSelectorException#ClosedSelectorException()}
 */
public void test_Constructor() {
    ClosedSelectorException e = new ClosedSelectorException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
 
Example #29
Source File: SelectorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void assert_select_SelectorClosed(SelectType type, int timeout)
        throws IOException {
    // selector is closed
    selector.close();
    try {
        selectOnce(type, timeout);
        fail("should throw ClosedSelectorException");
    } catch (ClosedSelectorException e) {
        // expected
    }
}
 
Example #30
Source File: NioSocketConnector.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
	for (;;) {
		try {
			if (!register() || selector.keys().isEmpty()) {
				connectorRef.set(null);
				if (registerQueue.isEmpty() || !connectorRef.compareAndSet(null, this))
					break;
			}

			// the timeout for select shall be smaller of the connect timeout or 1 second...
			selector.select(this, Math.min(getConnectTimeoutMillis(), 1000));
			processTimedOutSessions();
		} catch (ClosedSelectorException cse) {
			ExceptionMonitor.getInstance().exceptionCaught(cse);
			break;
		} catch (Exception e) {
			ExceptionMonitor.getInstance().exceptionCaught(e);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				connectorRef.compareAndSet(this, null);
				ExceptionMonitor.getInstance().exceptionCaught(e1);
				break;
			}
		}
	}
}