【发布时间】:2014-09-29 14:59:52
【问题描述】:
使用Akka-IO TCP,在actor中建立连接的过程如下:
class MyActor(remote: InetSocketAddress) extends Actor {
IO(Tcp) ! Connect(remote) //this is the first step, remote is the address to connect to
def receive = {
case CommandFailed(_: Connect) => context stop self // failed to connect
case Connected(remote, local) =>
val connection = sender()
connection ! Register(self)
// do cool things...
}
}
您向IO(Tcp) 发送Connect 消息并期望收到CommandFailed 或Connected 消息。
现在,我的目标是创建一个包装 TCP 连接的参与者,但我希望我的参与者仅在建立连接后才开始接受消息 - 否则,在等待 Connected 消息时,它将开始接受查询,但没有人可以寄给他们。
我尝试了什么:
class MyActor(address: InetSocketAddress) extends Actor {
def receive = {
case Initialize =>
IO(Tcp) ! Connect(address)
context.become(waitForConnection(sender()))
case other => sender ! Status.Failure(new Exception(s"Connection to $address not established yet."))
}
private def waitForConnection(initializer: ActorRef): Receive = {
case Connected(_, _) =>
val connection = sender()
connection ! Register(self)
initializer ! Status.Success(Unit)
// do cool things
case CommandFailed(_: Connect) =>
initializer ! Status.Failure(new Exception("Failed to connect to " + host))
context stop self
}
}
我的第一个receive 期待一个虚构的Initialize 消息,它将触发整个连接过程,一旦完成,Initialize 的发送者会收到一条成功消息并知道它可以知道开始发送查询。
我对此不太满意,它迫使我用它来创建我的演员
val actor = system.actorOf(MyActor.props(remote))
Await.ready(actor ? Initialize, timeout)
而且它不会很“重启”友好。
在 Tcp 层回复 Connected 之前,有什么办法可以保证我的 actor 不会开始从邮箱接收消息吗?
【问题讨论】: