相关疑难解决方法(0)

按需演员得到或否则创造

我可以创建演员,actorOf并与他们一起看actorFor.我现在想要一些演员id:String,如果它不存在,我希望它被创建.像这样的东西:

  def getRCActor(id: String):ActorRef = {
    Logger.info("getting actor %s".format(id))
    var a = system.actorFor(id)
    if(a.isTerminated){
      Logger.info("actor is terminated, creating new one")
      return system.actorOf(Props[RC], id:String)
    }else{
      return a
    }
   }
Run Code Online (Sandbox Code Playgroud)

但这并不isTerminated总是如此,我得到actor name 1 is not unique!了第二次调用的异常.我想我在这里使用了错误的模式.有人可以帮助如何实现这一目标吗?我需要

  • 按需创建演员
  • 通过id查找actor,如果不存在则创建它们
  • 能够摧毁,因为我不知道我是否会再次需要它

我应该使用Dispatcher或Router吗?

解决方案 如我所建议的,我使用一个具体的Supervisor来保存地图中可用的actor.可以要求他提供他的一个孩子.

class RCSupervisor extends Actor {

  implicit val timeout = Timeout(1 second)
  var as = Map.empty[String, ActorRef]

  def getRCActor(id: String) = as get id getOrElse {
    val c = context actorOf …
Run Code Online (Sandbox Code Playgroud)

scala akka

23
推荐指数
2
解决办法
6042
查看次数

我怎样才能检查Akka演员是否存在(akka 2.2)?

我有一个java对象,它不是一个使用actorSelection(Path)从actor系统中选择actor的actor.系统中可能存在所选的actor.

在Java Api中,ActorSelection不存在ask(),因此我无法向actor选择发送和识别消息并使用响应的发送者.

我试图通过演员选择向演员发送消息然后对deadletter做出反应来解决问题.但我没有任何动静.

如果演员是活着还是不存在,我如何检查ActorSelection?

ActorSystem system = ActorSystem.create("test");

//create test actor
system.actorOf(Props.create(TestActor.class), "testActor");

//add dead letter listener to the system
ActorRef eventBusActor = asys.actorOf(Props.create(EventBusActor.class), "eventbusactor");
system.eventStream().subscribe(eventBusActor, DeadLetter.class);


//This works. The test actor receives the message      
ActorSelection a1 = asys.actorSelection("/user/testActor");
a1.tell("hello", ActorRef.noSender());

//This does not work and does not send dead letters      
ActorSelection a2 = asys.actorSelection("/user/doesnotexist");
a2.tell("hello", ActorRef.noSender());

//Does not compile, because ask needs an ActorRef as first argument
ActorSelection a3 = asys.actorSelection("/user/test");
Future f = Patterns.ask(a3, new Identify(), 1000);
Run Code Online (Sandbox Code Playgroud)

java actor akka

21
推荐指数
3
解决办法
2万
查看次数

获取或创建儿童Akka演员并确保活力

我试图使用Akka actor的层次结构来处理每个用户状态.有一个拥有所有子节点的父actor,并以正确的方式处理get-or-create(参见a1,a2):

class UserActorRegistry extends Actor {
  override def Receive = {
    case msg@ DoPerUserWork(userId, _) =>
      val perUserActor = getOrCreateUserActor(userId)
      // perUserActor is live now, but will it receive "msg"?
      perUserActor.forward(msg)
  }

  def getOrCreateUserActor(userId: UserId): ActorRef = {
    val childName = userId.toActorName
    context.child(childName) match {
      case Some(child) => child
      case None => context.actorOf(Props(classOf[UserActor], userId), childName)
  }
}
Run Code Online (Sandbox Code Playgroud)

为了回收内存,在UserActors一段闲置之后过期(即计时器触发子actor调用context.stop(self)).

我的问题是我认为我在"getOrCreateUserActor"和接收转发消息的子actor之间存在竞争条件 - 如果子进程在该窗口中到期,则转发的消息将丢失.

有什么方法可以检测到这种边缘情况,还是重构UserActorRegistry以排除它?

scala race-condition actor akka

6
推荐指数
1
解决办法
3288
查看次数

标签 统计

akka ×3

actor ×2

scala ×2

java ×1

race-condition ×1