【问题标题】:Testing Akka Actors Fault Tolerance测试 Akka Actor 的容错性
【发布时间】:2013-07-04 07:49:58
【问题描述】:

我正在学习如何使用 Akka 演员,我在 Akka 文档 here 中找到了这个关于容错的示例。

class Parent extends Actor with ActorLogging {
    override val supervisorStrategy =
            OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
            case _: ArithmeticException ⇒ Resume
            case _: NullPointerException ⇒ Restart
            case _: IllegalArgumentException ⇒ Stop
            case _: Exception ⇒ Escalate
    }
    def receive = {
        case p: Props ⇒ sender ! context.actorOf(p)
    }
}

class Child extends Actor {
    var state = 0
    def receive = {
        case ex: Exception ⇒ throw ex
        case x: Int ⇒ state = x
        case "get" ⇒ sender ! state
    }
}

我正在尝试创建一个测试类来控制父类和子类。这个类应该有一个方法来做初始化部分和另一个方法来向子实例发送消息。

我需要帮助来组织课程。在 Java 中,我会将 ActorSystem、Parent 和 Child 声明为实例变量并在方法中使用它们。但我不确定如何在 Scala 中执行此操作。

谢谢。

【问题讨论】:

    标签: scala akka actor


    【解决方案1】:

    不是最好的方法。但我认为它应该可以工作。

    object Application {
       def main(args : Array[String]) {
         val system = ActorSystem("mySystem")
         val parent = system.actorOf(Props[Parent], "parent")
         (parent ? Props[Child]).mapTo[ActorRef].foreach { child =>
            child ! new IllegalArgumentException("Oh my")
         }
       }       
    }
    

    另外我推荐你lookup the akka scala documentation

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2019-02-12
      • 2012-05-20
      • 2014-08-14
      • 2020-10-04
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多