How to create an actor with his wrapper?

I am trying to write an actor called ActorManagerthat wraps another actor called RealActor. The idea is that it ActorManagercan handle all messages coming in and out RealActor, which allows you to add additional logic, such as filtering or buffering. It is assumed that the outside world should communicate with RealActoronly through its manager, as in the real world.

The first draft will look like this:

class ActorManager(realActor: ActorRef) extends Actor {
  def receive = {
    case _ => { /* pre-process messages */ }
  }
}

class RealActor(actorManager: ActorRef) extends Actor {
  def receive = {
    case _ => { /* actual business logic */ }
  }
}

However, this raises the question of how to build both participants at once, or, more specifically, how to determine the appropriate one Propstaking into account the circular dependence of the two participants. I am not sure if a generic template islazy val applicable in the definition Props.

, Register .

+4
2

-. , RealActor , , ActorRef / ActorManager.

, RealActor ActorManager. ActorRef => Props, :

// Parent
class ActorManager(getRealActorProps: ActorRef => Props) extends Actor {
  val realActor = context.actorOf(getRealActorProps(self))
  def receive = {
    case _ => { /* pre-process messages */ }
  }
}

// Child
class RealActor(actorManager: ActorRef) extends Actor {
  def receive = {
    case _ => { /* actual business logic */ }
  }
}

object RealActor {
  def propsActorManager(getRealActorProps: ActorRef => Props) = 
    Props(new ActorManager(getRealActorProps))
  def propsRealActor(actorManager: ActorRef) = 
    Props(new RealActor(actorManager))

  def props() = 
    Props(new ActorManager(actorManager => propsRealActor(actorManager)))
}

, , RealActor props, ActorManager -> RealActor.

. , -.

+5

- :

import akka.actor._

class Manager extends Actor {

  def realActor: ActorRef = context.child("real")
        .getOrElse(context.actorOf(RealActor.props, "real"))

  override def receive: Receive = {
    case msg β‡’ realActor forward msg
  }
}    

object RealActor {
  def props: Props = Props(new RealActor)
}

class RealActor extends Actor {
  override def receive: Receive = {
    case _ β‡’
  }
}

parent-child. context.parent .

+1

Source: https://habr.com/ru/post/1673135/


All Articles