我正在尝试编写侦听tcp连接的服务器。 我正在使用nonblockig java.nio。当演员收到“STARTLISTEN”消息Akka和java.nio。非阻塞选择器
我selector.select()开始,这是确定的,但选择之前没有收到任何数据时,我发信息给演员就无法接收消息。
如何避免这种情况?例如,我想发送消息“停止”给我的演员停止选择? 只有通过timeout或selectNow()才能调用select?
我不打算使用akka.io.
这不是完整的代码,但它说明了这个问题:
class MyActor(val host: String, port: Int) extends Actor {
val serverChannel = ServerSocketChannel.open()
val channelSelector = Selector.open()
serverChannel.configureBlocking(false);
serverChannel.bind(new InetSocketAddress(InetAddress.getByName(host), port))
serverChannel.register(channelSelector, SelectionKey.OP_ACCEPT);
override def receive = {
case "STARTLISTEN" => {
val keys = channelSelector.select()
// processing read write etc....
}
case "STOP" => {
// I want to stop selection here....
}
}
}
非常不清楚,你应该共享您的代码的相关片段。 – vptheron
我添加的代码 – Antek