【发布时间】:2014-03-08 03:52:12
【问题描述】:
我正在尝试编写侦听 tcp 连接的服务器。 我正在使用 nonblockig java.nio。
当演员收到“STARTLISTEN”消息时我的 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....
}
}
}
【问题讨论】:
-
很不清楚,你应该分享你的代码的相关sn-ps。