因此,当使用提问模式(actor ? msg
)发送消息时,它会在幕后创建一个“one-off actor”。Akka - 使用提问模式的actorSelection
我的问题是 - 是否可以使用actorSelection
发送此临时演员消息?
例如,下面的代码工作得很好:
object Test extends App {
case class WrappedMsg(msg: String, replyTo: ActorRef)
class Source(target: ActorRef) extends Actor {
def receive = { case _ => } // doesn't matter
implicit val execution = context.dispatcher
implicit val timeout = Timeout(5.seconds)
val middleware = context.actorOf(Props(new Middleware(target)))
(middleware ? "Something").mapTo[String].onComplete {
case Success(msg) => println("Success: " + msg)
case Failure(err) => println("Failure: " + err)
}
}
class Middleware(target: ActorRef) extends Actor {
def receive = {
case msg: String =>
val wrappedMsg = WrappedMsg(replyTo = sender(), msg = msg)
target ! wrappedMsg
}
}
class Target extends Actor {
def receive = {
case wrappedMsg: WrappedMsg => wrappedMsg.replyTo ! "Received"
}
}
val system = ActorSystem()
val target = system.actorOf(Props(new Target))
val source = system.actorOf(Props(new Source(target)))
}
但是,如果我做以下修改,以便使用演员网址,而不是ActorRef,它没有:
case class WrappedMsg(msg: String, replyTo: String)
...
val wrappedMsg = WrappedMsg(replyTo = sender().path.toSerializationFormat, msg = msg)
...
case wrappedMsg: WrappedMsg => context.actorSelection(wrappedMsg.replyTo) ! "Received"
谢谢
“它失败”是什么意思? –
“收到”消息未达到源(通过临时参与者)。 在第一种情况下,打印“成功”,在第二种情况下打印“失败”。 – AlonL
'sender()。path.toSerializationFormat'返回'akka:// default/temp/$ a',顺便说一句 – AlonL