要想容错,该怎么办?

父actor首先要获知子actor的失败状态,然后确定该怎么办, “怎么办”这回事叫做“supervisorStrategy"。

  // Restart the storage child when StorageException is thrown.
  // After 3 restarts within 5 seconds it will be stopped.
  override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 3,
    withinTimeRange = 5 seconds) {
    case _: Storage.StorageException => Restart
  }
 
SupervisorStrategy是干啥的呢? An Akka Supervisor Strategy is the policy to apply for crashing children.
有哪些SupervisorStrategy可以用呢?
1. AllForOneStrategy  :   当一个child失败以后,把所有的fault handling "Directive"应用于所有children。
2. OneForOneStrategy :  当一个child失败以后,把fault handling "Directive"应用于失败的那个child。
 
”Directive"是干啥的?
首先,这个东西需要决定当child failure之后,应该怎么办? “应该怎么办”是Akka提供了几个实现,表现在Directive的子类:
1. Resume 继续让失败的那个actor处理消息
2. Restart 抛弃旧的actor,然后创建一个新的,然后恢复消息处理
3. Stop 停止出问题的子actor
4. Escalate 把失败提升到父Actor的父Actor,通过重新跑出失败的cause,使用父Actor由于子actor同样的异常失败。
 
由此看,所谓的failure,就是异常,准确地说就是Throwable。
 
 
应该如何把Directive传给strategy?
OneForOneStrategy和 AllForOneStrategy是一个柯里化函数,第二个参数块是一个叫Decider的东西,它是一个PartialFunction,把异常和Directive对应起来。
 type Decider = PartialFunction[Throwable, Directive]
在XXXStrategy的参数中,第一个参数块里的参数都可以省略,使得可以不限制重试次数,或者只限制总次数不限制频繁程度。
 
当没有指定supervisor strategy时,默认的行为是怎么样的呢?
• ActorInitializationException will stop the failing child actor
• ActorKilledException will stop the failing child actor
• Exception will restart the failing child actor
• Other types of Throwable will be escalated to parent actor
 
可以订制Strategy的行为:例子
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import scala.concurrent.duration._
override val supervisorStrategy =
     OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
          case _: ArithmeticException => Resume
          case t =>
          super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate)
}
 
Supervision of Top-Level Actors
 
所有Top-Level Actors都是User Guardian的子actor。所以它们的失败由guardian根据它配置的strategy来处理
 
 

Akka的fault tolerant的更多相关文章

  1. FTH: (7156): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***

    这两天在Qtcreator上编译程序的时候莫名其妙的出现了FTH: (7156): *** Fault tolerant heap shim applied to current process. T ...

  2. 解决Qt4.8.6+VS2010运行程序提示 FTH: (6512): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes

    这个问题偶尔碰到两次,现在又遇上了,解决办法如下: 打开注册表,设置HKLM\Software\Microsoft\FTH\Enabled 为0 打开CMD,运行Rundll32.exe fthsvc ...

  3. 分布式应用框架Akka快速入门

    转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...

  4. Akka Essentials - 2

    Actors Defining an actor class MyActor extends Actor { def receive = { } } In Scala, the receive blo ...

  5. Akka边学边写(1)-- Hello, World!

    Akka Akka是什么呢?直接引用Akka站点上面的描写叙述吧: Akka is a toolkit and runtime for building highly concurrent, dist ...

  6. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  7. 资源list:Github上关于大数据的开源项目、论文等合集

    Awesome Big Data A curated list of awesome big data frameworks, resources and other awesomeness. Ins ...

  8. Java开源框架推荐(全)

    Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...

  9. awesome-java

    Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancient ...

随机推荐

  1. <转载>批处理之FOR语句祥解

    批处理之FOR语句祥解 FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号) FOR 参数 %%变量名 IN ...

  2. VScript 函数调用的两种分类:Sub过程和Function过程

    来源:http://soft.zdnet.com.cn/software_zone/2007/0925/523318.shtml 在 VBScript 中,过程被分为两类:Sub 过程和 Functi ...

  3. swift闭包传值

    不知道原理,就知道这么用的,皮毛上的那一点. 寻思着把以前的项目改成swift的,结果了,,, 反向传值 一. //类似于OC中的typedef typealias sendValueClosure= ...

  4. dorado问题查询&快捷键重命名

    重命名还有一个快捷键  F2 有关dorado的问题可以进 www.bsdn.org提问,而且更好

  5. 你早该这么玩Excel 读书笔记

    1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...

  6. Cursor--游标

    游标--cursor['kɜːsə]   概念:                         在执行SQL语句时,Oracle服务器将分配一个内存区域,不仅存储这个语句,还存储语句的结果 — 称为 ...

  7. web网页的表单排版利器--960css

    表单排版样式 960css 前言 一般web网页的表单排版,大家都习惯用table排版,自己需要根据实际需要去定义TR和TD,很多时候对于TD的高宽度.是否合并行,合并列,都要去做一些处理,这些都是比 ...

  8. 玄机网C#论坛测试小游戏

    http://files.cnblogs.com/ro4ters/EasyGame.zip http://www.xuanjics.com/thread-39-1-1.html 具体活动地址

  9. android 软件开机自启动

    安卓的很多功能实现方式都是“Don't call me, I'll call you back!”,开机启动就是其中之一 步骤: 1.首先建立一个BroadcastReceiver, 2.在他的onR ...

  10. ACM俱乐部 字符串

    数制转换分数: 2 时间限制:1 秒 内存限制:32 兆 特殊判题: 否 提交:59 解决: 24 标签 进制转换 题目描述 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所 ...