加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 09:58:29 所属栏目:安全 来源:网络整理
导读:我为演员研究了Akka的“Graceful Stop”并创建了一个小测试应用来测试它. 应用程序显示http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Graceful_Stop中提到的“Graceful Stop” 并不总是保证您可以重复使用正常停止的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.")
  }
}

和演员……

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.

解决方法

推荐有很糟糕的格式,所以我会在这里复制它

我只是按照链接,在这里发现红色警报

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.

因此,只有终止接待才能成为主管演员并开始同名

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读