java.nio.channels.Selector.isOpen()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(131)

本文整理了Java中java.nio.channels.Selector.isOpen()方法的一些代码示例,展示了Selector.isOpen()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Selector.isOpen()方法的具体详情如下:
包路径:java.nio.channels.Selector
类名称:Selector
方法名:isOpen

Selector.isOpen介绍

[英]Indicates whether this selector is open.
[中]

代码示例

代码示例来源:origin: netty/netty

@Override
public boolean isOpen() {
  return delegate.isOpen();
}

代码示例来源:origin: redisson/redisson

@Override
public boolean isOpen() {
  return delegate.isOpen();
}

代码示例来源:origin: wildfly/wildfly

@Override
public boolean isOpen() {
  return delegate.isOpen();
}

代码示例来源:origin: wildfly/wildfly

public boolean        selectorOpen()                {return selector != null && selector.isOpen();}
public boolean        acceptorRunning()             {return acceptor != null && acceptor.isAlive();}

代码示例来源:origin: apache/activemq

public int available() throws IOException {
  if (!rs.isOpen())
    throw new IOException("Input Stream Closed");
  return bb.remaining();
}

代码示例来源:origin: apache/ignite

/**
 * Quietly closes given resource ignoring possible checked exception.
 *
 * @param rsrc Resource to close. If it's {@code null} - it's no-op.
 */
public static void closeQuiet(@Nullable Selector rsrc) {
  if (rsrc != null)
    try {
      if (rsrc.isOpen())
        rsrc.close();
    }
    catch (IOException ignored) {
      // No-op.
    }
}

代码示例来源:origin: apache/geode

public boolean isShutdownProperly() {
 return !isRunning() && !thread.isAlive()
   && (selectorThread == null || !selectorThread.isAlive())
   && (pool == null || pool.isShutdown()) && (hsPool == null || hsPool.isShutdown())
   && (clientQueueInitPool == null || clientQueueInitPool.isShutdown())
   && (selector == null || !selector.isOpen()) && (tmpSel == null || !tmpSel.isOpen());
}

代码示例来源:origin: apache/activemq

public void close() throws IOException {
  if (rs.isOpen()) {
    rs.close();
    if (sc.isOpen()) {
      sc.socket().shutdownInput();
      sc.socket().close();
    }
    bb = null;
    sc = null;
  }
}

代码示例来源:origin: apache/geode

/**
 * wake up the selector thread
 */
private void wakeupSelector() {
 Selector s = getSelector();
 if (s != null && s.isOpen()) {
  this.selector.wakeup();
 }
}

代码示例来源:origin: apache/ignite

/**
 * Closes given resource logging possible checked exception.
 *
 * @param rsrc Resource to close. If it's {@code null} - it's no-op.
 * @param log Logger to log possible checked exception with (optional).
 */
public static void close(@Nullable Selector rsrc, @Nullable IgniteLogger log) {
  if (rsrc != null)
    try {
      if (rsrc.isOpen())
        rsrc.close();
    }
    catch (IOException e) {
      warn(log, "Failed to close resource: " + e.getMessage());
    }
}

代码示例来源:origin: apache/activemq

public int read() throws IOException {
  if (!rs.isOpen())
    throw new IOException("Input Stream Closed");
  if (!bb.hasRemaining()) {
    try {
      fill(1);
    } catch (ClosedChannelException e) {
      close();
      return -1;
    }
  }
  return (bb.get() & 0xFF);
}

代码示例来源:origin: apache/activemq

public int read(byte[] b, int off, int len) throws IOException {
  int bytesCopied = -1;
  if (!rs.isOpen())
    throw new IOException("Input Stream Closed");
  while (bytesCopied == -1) {
    if (bb.hasRemaining()) {
      bytesCopied = (len < bb.remaining() ? len : bb.remaining());
      bb.get(b, off, bytesCopied);
    } else {
      try {
        fill(1);
      } catch (ClosedChannelException e) {
        close();
        return -1;
      }
    }
  }
  return bytesCopied;
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

@Override
  public void run() {
    while (!isInterrupted()
        && isBind()) {
      try {
        while (selector.isOpen()
            && selector.select() > 0) {
          final Iterator<SelectionKey> it = selector.selectedKeys().iterator();
          while (it.hasNext()) {
            final SelectionKey key = it.next();
            it.remove();
            // do ssc accept
            if (key.isValid() && key.isAcceptable()) {
              doAccept(key, selector, configure);
            }
            // do sc read
            if (key.isValid() && key.isReadable()) {
              doRead(byteBuffer, key);
            }
          }
        }
      } catch (IOException e) {
        logger.warn("selector failed.", e);
      } catch (ClosedSelectorException e) {
        logger.debug("selector closed.", e);
      }
    }
  }
};

代码示例来源:origin: apache/nifi

private synchronized void close() {
  if (selector != null && selector.isOpen()) {
    try {
      selector.close();
    } catch (final Exception e) {
      logger.warn("Failed to close NIO Selector", e);
    }
  }
  if (channel != null && channel.isOpen()) {
    try {
      channel.close();
    } catch (final Exception e) {
      logger.warn("Failed to close Socket Channel to {} for Load Balancing", nodeIdentifier, e);
    }
  }
  channel = null;
  selector = null;
}

代码示例来源:origin: apache/nifi

if (this.isRunning.compareAndSet(true, false)) {
  try {
    if (this.selector != null && this.selector.isOpen()) { // since stop must be idempotent, we need to check if selector is open to avoid ClosedSelectorException
      Set<SelectionKey> selectionKeys = new HashSet<>(this.selector.keys());
      for (SelectionKey key : selectionKeys) {

代码示例来源:origin: apache/activemq

public long skip(long n) throws IOException {
  long skiped = 0;
  if (!rs.isOpen())
    throw new IOException("Input Stream Closed");
  while (n > 0) {
    if (n <= bb.remaining()) {
      skiped += n;
      bb.position(bb.position() + (int) n);
      n = 0;
    } else {
      skiped += bb.remaining();
      n -= bb.remaining();
      bb.position(bb.limit());
      try {
        fill((int) n);
      } catch (ClosedChannelException e) {
        close();
        return skiped;
      }
    }
  }
  return skiped;
}

代码示例来源:origin: apache/nifi

/**
 * Once the handler is constructed this should be called to start the handler. Although
 * this method is safe to be called by multiple threads, it should only be called once.
 *
 * @throws IllegalStateException if it fails to start listening on the port that is configured
 *
 */
public void start() {
  if (this.isRunning.compareAndSet(false, true)) {
    try {
      if (this.selector == null || !this.selector.isOpen()) {
        this.selector = Selector.open();
        InetSocketAddress connectedAddress = this.connect();
        this.listenerTaskExecutor = Executors.newCachedThreadPool();
        this.listenerTaskExecutor.execute(this.listenerTask);
        if (logger.isDebugEnabled()) {
          logger.debug("Started listener for " + AbstractSocketHandler.this.getClass().getSimpleName());
        }
        if (logger.isInfoEnabled()) {
          logger.info("Successfully bound to " + connectedAddress);
        }
      }
    } catch (Exception e) {
      this.stop();
      throw new IllegalStateException("Failed to start " + this.getClass().getName(), e);
    }
  }
}

代码示例来源:origin: apache/nifi

@Override
public void run() {
  try {
    while (AbstractSocketHandler.this.rootChannel != null && AbstractSocketHandler.this.rootChannel.isOpen() && AbstractSocketHandler.this.selector.isOpen()) {
      if (AbstractSocketHandler.this.selector.isOpen() && AbstractSocketHandler.this.selector.select(10) > 0) {
        Iterator<SelectionKey> keys = AbstractSocketHandler.this.selector.selectedKeys().iterator();
        while (keys.hasNext()) {
          SelectionKey selectionKey = keys.next();
          keys.remove();
          if (selectionKey.isValid()) {
            if (selectionKey.isAcceptable()) {
              this.accept(selectionKey);
            } else if (selectionKey.isReadable()) {
              this.read(selectionKey);
            } else if (selectionKey.isConnectable()) {
              this.connect(selectionKey);
            }
          }
        }
      }
    }
  } catch (Exception e) {
    logger.error("Exception in socket listener loop", e);
  }
  logger.debug("Exited Listener loop.");
  AbstractSocketHandler.this.stop();
}

代码示例来源:origin: rapidoid/rapidoid

@Override
public synchronized Server shutdown() {
  Log.info("Shutting down the server...");
  stopLoop();
  if (ioWorkers != null) {
    for (RapidoidWorker worker : ioWorkers) {
      worker.shutdown();
    }
  }
  if (serverSocketChannel != null && selector != null && serverSocketChannel.isOpen() && selector.isOpen()) {
    try {
      selector.close();
      serverSocketChannel.close();
    } catch (IOException e) {
      Log.warn("Cannot close socket or selector!", e);
    }
  }
  super.shutdown();
  Log.info("!The server is down.");
  return this;
}

代码示例来源:origin: rapidoid/rapidoid

private void openSocket() throws IOException {
  U.notNull(net.protocol(), "protocol");
  U.notNull(net.helperClass(), "helperClass");
  String blockingInfo = net.blockingAccept() ? "blocking" : "non-blocking";
  Log.debug("Initializing server", "address", net.address(), "port", net.port(), "sync", net.syncBufs(), "accept", blockingInfo);
  serverSocketChannel = ServerSocketChannel.open();
  if ((serverSocketChannel.isOpen()) && (selector.isOpen())) {
    serverSocketChannel.configureBlocking(net.blockingAccept());
    ServerSocket socket = serverSocketChannel.socket();
    Log.info("!Starting server", "!address", net.address(), "!port", net.port(), "I/O workers", net.workers(), "sync", net.syncBufs(), "accept", blockingInfo);
    InetSocketAddress addr = new InetSocketAddress(net.address(), net.port());
    socket.setReceiveBufferSize(16 * 1024);
    socket.setReuseAddress(true);
    socket.bind(addr, MAX_PENDING_CONNECTIONS);
    Log.debug("Opened server socket", "address", addr);
    if (!net.blockingAccept()) {
      Log.debug("Registering accept selector");
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    }
    initWorkers();
  } else {
    throw U.rte("Cannot open socket!");
  }
}

相关文章