Search code examples
akkaactor

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?


Solution

  • 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 _
      }
    }