在actor之间共享可变对象

时间:2017-04-05 20:37:45

标签: java akka state mutable

我有以下情况

Actor A收到请求并将工作委托给两个子代理ACtor bActor c。这两个角色都改变了对象的状态,但是父角色并不依赖于任何这些角色的结果

这是反模式吗?如何在不传递此对象的情况下分配负载?

1 个答案:

答案 0 :(得分:0)

这是scala中的示例,但逻辑是相同的

class ParentActorA extends Actor {
  val childActorB: ActorRef = _
  val childActorC: ActorRef = _

  def receive: Receive = {
     case r: Request =>
       childActorB ! Delegate(r)
       childActorC ! Delegate(r)
     case n: ChildNotification => //change local state based on this notification from child actor       
  }
}

class ChildActorA extends Actor {
   def receive: Receive = {
      case Delegate(r) => //do some work and notify parent with ChildNotification object
   }
}