[英]Unregister and register again in java NIO
Is it possible to unregister and then register again later on with the same selector ? 是否可以注销,然后稍后使用相同的选择器再次注册? I have to limit the amount of connection I accept and do key.cancel() to stop receiving OP_ACCEPT events. 我必须限制我接受的连接数量并执行key.cancel()才能停止接收OP_ACCEPT事件。
server.register(selector, SelectionKey.OP_ACCEPT);
...
key.cancel()
...
If a connection is closed, I cant run register(..) again, getting a java.nio.channels.CancelledKeyException 如果关闭连接,则无法再次运行register(..),并获得java.nio.channels.CancelledKeyException
Javadoc for register(..) : throw CancelledKeyException if this channel is currently registered with the given selector but the corresponding key has already been cancelled 用于register(..)的Javadoc:如果此通道当前已在给定选择器中注册,但对应的键已被取消,则抛出CancelledKeyException
What is the way to achieve what I am trying to do ? 实现我正在尝试的方法是什么?
只需清除要在密钥的interestOps
清除的位即可:例如key.interestOps(0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.