类java.nio.channels.ClosedSelectorException源码实例Demo

下面列出了怎么用java.nio.channels.ClosedSelectorException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: aion   文件: TaskInboundTest.java
@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);
    }
}
 
源代码2 项目: aion   文件: TaskInboundTest.java
@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);
    }
}
 
源代码3 项目: linstor-server   文件: TcpConnectorService.java
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);
    }
}
 
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);
  }
}
 
源代码5 项目: galaxy-sdk-java   文件: TAsyncClientManager.java
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);
  }
}
 
源代码6 项目: MediaSDK   文件: AsyncServer.java
private static void run(final AsyncServer server, final SelectorWrapper selector, final PriorityQueue<Scheduled> queue) {
//        Log.i(LOGTAG, "****AsyncServer is starting.****");
        // at this point, this local queue and selector are owned
        // by this thread.
        // if a stop is called, the instance queue and selector
        // will be replaced and nulled respectively.
        // this will allow the old queue and selector to shut down
        // gracefully, while also allowing a new selector thread
        // to start up while the old one is still shutting down.
        while(true) {
            try {
                runLoop(server, selector, queue);
            }
            catch (AsyncSelectorException e) {
                if (!(e.getCause() instanceof ClosedSelectorException))
                    Log.i(LOGTAG, "Selector exception, shutting down", e);
                StreamUtility.closeQuietly(selector);
            }
            // see if we keep looping, this must be in a synchronized block since the queue is accessed.
            synchronized (server) {
                if (selector.isOpen() && (selector.keys().size() > 0 || queue.size() > 0))
                    continue;

                shutdownEverything(selector);
                if (server.mSelector == selector) {
                    server.mQueue = new PriorityQueue<Scheduled>(1, Scheduler.INSTANCE);
                    server.mSelector = null;
                    server.mAffinity = null;
                }
                break;
            }
        }
//        Log.i(LOGTAG, "****AsyncServer has shut down.****");
    }
 
源代码7 项目: dragonwell8_jdk   文件: WindowsSelectorImpl.java
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;
}
 
源代码8 项目: dragonwell8_jdk   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码9 项目: dragonwell8_jdk   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码10 项目: TencentKona-8   文件: WindowsSelectorImpl.java
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;
}
 
源代码11 项目: TencentKona-8   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码12 项目: TencentKona-8   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码13 项目: pushfish-android   文件: SocketConnection.java
@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;
}
 
源代码14 项目: pushfish-android   文件: SocketConnection.java
@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;
}
 
源代码15 项目: jdk8u60   文件: WindowsSelectorImpl.java
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;
}
 
源代码16 项目: jdk8u60   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码17 项目: jdk8u60   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码18 项目: openjdk-jdk8u   文件: WindowsSelectorImpl.java
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;
}
 
源代码19 项目: openjdk-jdk8u   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码20 项目: openjdk-jdk8u   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码21 项目: openjdk-jdk8u-backup   文件: WindowsSelectorImpl.java
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;
}
 
源代码22 项目: openjdk-jdk8u-backup   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码23 项目: openjdk-jdk8u-backup   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码24 项目: openjdk-jdk9   文件: WindowsSelectorImpl.java
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;
}
 
源代码25 项目: openjdk-jdk9   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码26 项目: openjdk-jdk9   文件: WindowsSelectorImpl.java
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);
    }
}
 
源代码27 项目: openjdk-jdk9   文件: WakeupAfterClose.java
public static void main(String[] args) throws Exception {
    final Selector sel = Selector.open();

    Runnable r = new Runnable() {
        public void run() {
            try {
                sel.select();
            } catch (IOException x) {
                x.printStackTrace();
            } catch (ClosedSelectorException y) {
                System.err.println
                    ("Caught expected ClosedSelectorException");
            }
        }
    };

    // start thread to block in Selector
    Thread t = new Thread(r);
    t.start();

    // give thread time to start
    Thread.sleep(1000);

    // interrupt, close, and wakeup is the magic sequence to provoke the NPE
    t.interrupt();
    sel.close();
    sel.wakeup();
}
 
源代码28 项目: jdk8u-jdk   文件: WindowsSelectorImpl.java
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;
}
 
源代码29 项目: jdk8u-jdk   文件: WindowsSelectorImpl.java
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++;
    }
}
 
源代码30 项目: jdk8u-jdk   文件: WindowsSelectorImpl.java
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);
    }
}
 
 类所在包
 同包方法