Michał Jurczuk
Michał Jurczuk

Reputation: 3828

How set Akka supervisor for actors created by system.actorOf

How can I set two different supervisor policies for two actors created using system context:

val exporter = system.actorOf(Props[DataExporter], name = "dataExporter")
val communicator = system.actorOf(Props[DeviceCommunicator], name = "deviceCommunicator")

And can I check the actor type during handling exception?

Upvotes: 2

Views: 1507

Answers (2)

Instead of spinning up top level actors and make Akka act on them differently you should create deeper hierarchies of Actors.

You should create top level actors (system.actorOf) which define different strategies, and they in turn should create your "worker" actors. So you would have 2 top level actors - defining the strategies as you explained. And they should start child actors using context.actorOf. This way you define hierarchies of "what to do when my children fail".

In other words keep the top-level actors as "supervisors", define supervision in there, and have them create children which do the actual work.

See also, docs: Fault Tolerance

Upvotes: 4

Dennis Traub
Dennis Traub

Reputation: 51684

I don't think there's a way to define multiple supervising strategies inside of one actor. But you could create another layer with two actors that each implement a specific strategy and let them forward the messages to the respective worker actors.

val exporter = system.actorOf(Props[DataExporterSupervisor], ...)
val communicator = system.actorOf(Props[DeviceCommunicatorSupervisor], ...)


class DataExporterSupervisor extends Actor {

  val exporter = system.actorOf(Props[DataExporter], ...)

  override val supervisorStrategy = ???

  def receive = {
    case _ => exporter forward _
  }
}


class DeviceCommunicatorSupervisor extends Actor {

  val communicator = system.actorOf(Props[DeviceCommunicator], ...)

  override val supervisorStrategy = ???

  def receive = {
    case _ => communicator forward _
  }
}

Upvotes: 2

Related Questions