Akka supervisor managing supervisors

假如想象 提交于 2019-12-07 18:20:26

问题


I reckon there might be a broader question of application design using Akka hidden in here but I'll ask how does one set up a supervision tree where a "kernel" or "top" supervisor might supervise children that are other supervisors which supervisor workers?


回答1:


You might start with a declarative supervisor on the top level

 val kernel = Supervisor(
     SupervisorConfig(
         OneForOneStrategy(List(classOf[Exception]), 3, 1000),
         Supervise(
             actorOf[Module1],
             Permanent) ::
         Supervise(
             actorOf[Module2],
             Permanent) ::
         Supervise(
             actorOf[Module3],
             Permanent) ::
         Nil
     )
 )

where module 1 to 3 represents the next level of your application architecture. Each module itself is a supervisor for all of its child actors and implemented like for example

class Module1 extends Actor {
   self.faultHandler = OneForOneStrategy(List(classOf[Throwable]), 5, 5000)

   def receive = {
       case Register(actor) =>
       self.link(actor)
   }
}

You might replace the "Register(actor)" message with something more suitable for your application, and you might want to span a further programmatically created supervisor layer but basically that would be the approach to follow.

You'll find further details (as Viktor already commented) in the official akka documentation on Fault tolerance with Scala or Fault tolerance with Java.



来源:https://stackoverflow.com/questions/5861505/akka-supervisor-managing-supervisors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!