gpt4 book ai didi

scala - Akka Actor - 发件人指向死信

转载 作者:行者123 更新时间:2023-12-02 08:14:18 24 4
gpt4 key购买 nike

考虑以下示例:

case class Payload(message: String, async: Boolean)

class EchoActor extends Actor {
override def receive: Receive = {
case Payload(message, async) =>
if (async) Future {
println(s"from: $sender")
sender ! message
} else {
println(s"from: $sender")
sender ! message
}
}
}

def main(args: Array[String]): Unit = {
val system = ActorSystem("demo")
val echo = system.actorOf(Props[EchoActor])
implicit val timeout = Timeout(2 seconds)

(echo ? Payload("Hello", async = false)).mapTo[String].foreach(println(_))
(echo ? Payload("Async Hello", async = true)).mapTo[String].foreach(println(_))

StdIn.readLine()
system.terminate()
}

控制台输出:

from: Actor[akka://demo/temp/$a]
Hello
from: Actor[akka://demo/deadLetters]
[INFO] [04/13/2017 19:56:58.516] [demo-akka.actor.default-dispatcher-4] [akka://demo/deadLetters] Message [java.lang.String] from Actor[akka://demo/user/$a#2112869650] to Actor[akka://demo/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

即当从另一个线程访问时,发送者指向 deadLetters

这背后的原因是什么?是bug吗?

虽然,我们可以保存对实际发件人的引用以使其工作:

if (async) {
val currentSender = sender()
Future {
println(s"from: $currentSender")
currentSender ! message
}
}

但是...没有更好的方法吗?

最佳答案

这不是错误,而是记录在案的行为 -

http://doc.akka.io/docs/akka/2.5.0/scala/actors.html#Send_messages

使用 Future 意味着调用一个匿名函数,该函数不是 Actor 类的实例,因此您的 sender() 引用映射到deadLetters 邮箱

关于scala - Akka Actor - 发件人指向死信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43396596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com