gpt4 book ai didi

scala - 不能总是在优雅停止后重用 Actor 的名字

转载 作者:行者123 更新时间:2023-12-04 14:25:48 27 4
gpt4 key购买 nike

我为 Actor 研究了 Akka 的“Graceful Stop”,并创建了一个小型测试应用程序来测试它。

应用显示http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Graceful_Stop中提到的“Graceful Stop”
总是 保证您可以重用优雅停止的 Actor 的名称。

时不时会出现以下异常:

Exception in thread "main" akka.actor.InvalidActorNameException: actor name [DummyActor] is not unique!

这是为什么?如何修复应用程序,以便 InvalidActorNameExceptions 不会时不时出现?

这是代码:

主课...
import akka.actor._
import akka.pattern.gracefulStop
import DummyActor.Stop
import DummyActor

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

object AkkaTest {
def main(args: Array[String]) {

val actorsystem = ActorSystem("testSystem")

1 to 5 foreach { _ =>

// Create an actor with name: DummyActor
val dummyActor = actorsystem.actorOf(Props[DummyActor], "DummyActor")

// Gracefully stop the DummyActor
val stopped: Future[Boolean] = gracefulStop(dummyActor, 5 seconds, Stop)
Await.result(stopped, 6 seconds)
}

val terminated: Future[Terminated] = actorsystem.terminate()
Await.result(terminated, 10 seconds)

System.out.println("Finished successfully. Try again, eventually it will fail. You can also increase the amount of loops.")
}
}

还有 Actor ...
import akka.actor.Actor
import DummyActor.Stop

object DummyActor {
case object Stop
}

class DummyActor extends Actor {

override def receive: Receive = {
case Stop => context stop self
}

}

我有 Scala 2.11.7 和 Java 8 和 Akka 2.4.0。

最佳答案

Commend 的格式很糟糕,所以我会复制到这里

我刚刚点击了链接,在这里发现了红色警报

Warning

Keep in mind that an actor stopping and its name being deregistered are separate events which happen asynchronously from each other. Therefore it may be that you will find the name still in use after gracefulStop() returned. In order to guarantee proper deregistration, only reuse names from within a supervisor you control and only in response to a Terminated message, i.e. not for top-level actors.



因此,仅在 Terminated 上制作主管 Actor 并开始同名。接待

关于scala - 不能总是在优雅停止后重用 Actor 的名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33939361/

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