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

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

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

Selector.keys介绍

[英]Gets the set of registered keys. The set is immutable and is not thread- safe.
[中]获取注册密钥集。集合是不可变的,并且不是线程安全的。

代码示例

代码示例来源:origin: org.mongodb/mongo-java-driver

private void processPendingInterests() {
  for (SelectionKey key : selector.keys()) {
    RegisteredSocket socket = (RegisteredSocket) key.attachment();
    int pending = socket.pendingOps.getAndSet(0);
    if (pending != 0) {
      key.interestOps(key.interestOps() | pending);
    }
  }
}

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

/**
 * If there were some calls that have not been sent out for a long time, we close the connection.
 * @return the time of the purge.
 */
private long purge(long lastPurgeTime) {
 long now = System.currentTimeMillis();
 if (now < lastPurgeTime + this.simpleRpcServer.purgeTimeout) {
  return lastPurgeTime;
 }
 ArrayList<SimpleServerRpcConnection> conWithOldCalls = new ArrayList<>();
 // get the list of channels from list of keys.
 synchronized (writeSelector.keys()) {
  for (SelectionKey key : writeSelector.keys()) {
   SimpleServerRpcConnection connection = (SimpleServerRpcConnection) key.attachment();
   if (connection == null) {
    throw new IllegalStateException("Coding error: SelectionKey key without attachment.");
   }
   if (connection.lastSentTime > 0 &&
     now > connection.lastSentTime + this.simpleRpcServer.purgeTimeout) {
    conWithOldCalls.add(connection);
   }
  }
 }
 // Seems safer to close the connection outside of the synchronized loop...
 for (SimpleServerRpcConnection connection : conWithOldCalls) {
  this.simpleRpcServer.closeConnection(connection);
 }
 return now;
}

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

private void closeAll() {
  selectAgain();
  Set<SelectionKey> keys = selector.keys();
  Collection<AbstractNioChannel> channels = new ArrayList<AbstractNioChannel>(keys.size());
  for (SelectionKey k: keys) {
    Object a = k.attachment();
    if (a instanceof AbstractNioChannel) {
      channels.add((AbstractNioChannel) a);
    } else {
      k.cancel();
      @SuppressWarnings("unchecked")
      NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
      invokeChannelUnregistered(task, k, null);
    }
  }
  for (AbstractNioChannel ch: channels) {
    ch.unsafe().close(ch.unsafe().voidPromise());
  }
}

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

for(SelectionKey sk: selector.keys()) {
  try {
    if(logger.isTraceEnabled())
    Object attachment = sk.attachment();
    if(attachment instanceof Closeable) {
      IOUtils.closeQuietly((Closeable) attachment);

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

private void closeAll() {
  selectAgain();
  Set<SelectionKey> keys = selector.keys();
  Collection<AbstractNioChannel> channels = new ArrayList<AbstractNioChannel>(keys.size());
  for (SelectionKey k: keys) {
    Object a = k.attachment();
    if (a instanceof AbstractNioChannel) {
      channels.add((AbstractNioChannel) a);
    } else {
      k.cancel();
      @SuppressWarnings("unchecked")
      NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
      invokeChannelUnregistered(task, k, null);
    }
  }
  for (AbstractNioChannel ch: channels) {
    ch.unsafe().close(ch.unsafe().voidPromise());
  }
}

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

try {
  if (!running) {
    Iterator<SelectionKey> it = sel.keys().iterator();
    while (it.hasNext()) {
      SelectionKey sk = it.next();
      sk.cancel();
      Object o = sk.attachment();
      if (o instanceof SockEvent  &&   ((SockEvent)o).ch instanceof ServerSocketChannel) {
    SelectionKey sk = it.next();
    it.remove();
    Object o = sk.attachment();
    sk.interestOps(0);
    assert(o instanceof SockEvent);

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

public void shutdown(final long period, final TimeUnit timeUnit) {
  channelDispatcher.stop();
  for (SelectionKey selectionKey : socketChannelSelector.keys()) {
    final AbstractChannelReader reader = (AbstractChannelReader) selectionKey.attachment();
    selectionKey.cancel();
    if (reader != null) {
  for (SelectionKey selectionKey : serverSocketSelector.keys()) {
    selectionKey.cancel();
    IOUtils.closeQuietly(selectionKey.channel());

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

Iterator<SelectionKey> i = selector.keys().iterator();
  ClientRequestExecutor clientRequestExecutor = (ClientRequestExecutor) selectionKey.attachment();

代码示例来源:origin: org.apache.hadoop/hadoop-common

RpcCall call = (RpcCall)key.attachment();
  if (call != null) {
   LOG.info(Thread.currentThread().getName() +
synchronized (writeSelector.keys()) {
 calls = new ArrayList<RpcCall>(writeSelector.keys().size());
 iter = writeSelector.keys().iterator();
 while (iter.hasNext()) {
  SelectionKey key = iter.next();
  RpcCall call = (RpcCall)key.attachment();
  if (call != null && key.channel() == call.connection.channel) { 
   calls.add(call);

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

private void closeAll() {
  selectAgain();
  Set<SelectionKey> keys = selector.keys();
  Collection<AbstractNioChannel> channels = new ArrayList<AbstractNioChannel>(keys.size());
  for (SelectionKey k: keys) {
    Object a = k.attachment();
    if (a instanceof AbstractNioChannel) {
      channels.add((AbstractNioChannel) a);
    } else {
      k.cancel();
      @SuppressWarnings("unchecked")
      NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
      invokeChannelUnregistered(task, k, null);
    }
  }
  for (AbstractNioChannel ch: channels) {
    ch.unsafe().close(ch.unsafe().voidPromise());
  }
}

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

i.remove();
    final SelectorSelection s = (SelectorSelection) key.attachment();
    try {
      if (key.isValid()) {
e.printStackTrace();
Set<SelectionKey> keys = selector.keys();
for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
  SelectionKey key = i.next();
  SelectorSelection s = (SelectorSelection) key.attachment();
  s.onError(e);

代码示例来源:origin: org.mongodb/mongo-java-driver

RegisteredSocket socket = (RegisteredSocket) key.attachment();
    processRead(socket);
    processWrite(socket);
for (SelectionKey key : selector.keys()) {
  RegisteredSocket socket = (RegisteredSocket) key.attachment();
  socket.close();

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

for (SelectionKey key: oldSelector.keys()) {
  Object a = key.attachment();
  try {
    if (!key.isValid() || key.channel().keyFor(newSelectorTuple.unwrappedSelector) != null) {

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

for (SelectionKey key : selector.keys()) {
  NIOServerCnxn cnxn = (NIOServerCnxn) key.attachment();
  if (cnxn.isSelectable()) {
    cnxn.close();

代码示例来源:origin: ltsopensource/light-task-scheduler

for (; ; ) {
  try {
    for (SelectionKey key : oldSelector.keys()) {
      Object a = key.attachment();
      try {
        if (key.channel().keyFor(newSelector) != null) {

代码示例来源:origin: ltsopensource/light-task-scheduler

for (; ; ) {
  try {
    for (SelectionKey key : oldSelector.keys()) {
      Object a = key.attachment();
      try {
        if (key.channel().keyFor(newSelector) != null) {

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

for (SelectionKey key: oldSelector.keys()) {
  Object a = key.attachment();
  try {
    if (!key.isValid() || key.channel().keyFor(newSelectorTuple.unwrappedSelector) != null) {

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

for (;;) {
  try {
    for (SelectionKey key: oldSelector.keys()) {
      try {
        if (key.channel().keyFor(newSelector) != null) {
        key.channel().register(newSelector, interestOps, key.attachment());
        nChannels ++;
      } catch (Exception e) {

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

Iterator it = (new ArrayList(this.selector.keys())).iterator();
while (it.hasNext()) {
 SelectionKey sk = (SelectionKey) it.next();
 ServerConnection sc = (ServerConnection) sk.attachment();
 if (sc == null) {
  continue;

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

if ((oldState & SHUTDOWN) != 0) {
  synchronized (lock) {
    keyCount = selector.keys().size();
    state = keyCount | SHUTDOWN;
    if (keyCount == 0 && workQueue.isEmpty()) {
    final Set<SelectionKey> keySet = selector.keys();
    synchronized (keySet) {
      keys = keySet.toArray(keys);
    if (key == null) break; //end of list
    keys[i] = null;
    final NioHandle attachment = (NioHandle) key.attachment();
    if (attachment != null) {
      safeClose(key.channel());
    if (ops != 0) {
      selectorLog.tracef("Selected key %s for %s", key, key.channel());
      final NioHandle handle = (NioHandle) key.attachment();
      if (handle == null) {
        cancelKey(key, false);

相关文章