要想容错,该怎么办?

父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. Android M Permission处理

    Android M(6.0)相比之前的版本,做了比较多的优化与改进.其中变化最大的一点是权限管理方面的改变,即不再是应用安装时授权,而改为运行时授权机制:一直以来,系统安全都是用户非常关心的问题,而之 ...

  2. Android之文字点击链接

    定义textView1和textView2并获得组件. String html = "<font color='red'></font>"; html += ...

  3. IIS6批量转移网站

    IIS6.0有个导出配置的功能,但你却找不到界面上的直接导入配置功能,需要用到操作系统自带的iiscnfg.vbs脚本. 1.导出当前的IIS网站配置 打开Internet信息服务(IIS)---&g ...

  4. 文件流操作(FileStream,StreamReader,StreamWriter)

    大文件拷贝: /// <summary> /// 大文件拷贝 /// </summary> /// <param name="sSource"> ...

  5. HTML+CSS学习笔记(5)- 与浏览者交互,表单标签

    HTML+CSS学习笔记(5)- 与浏览者交互,表单标签 1.使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务 ...

  6. Could not load the "defaultimg" image referenced from a nib in the bundle with identifier "com.abc"

    出现这个错误提示,是因为在xib中使用了.jpg格式的图片,在图片名称后面加上.jpg即可;

  7. spring事务到底用于service层还是dao层

    Spring事务为业务逻辑进行事务管理,保证业务逻辑上数据的原子性. 事务得根据项目性质来细分:事务可以设置到三个层面(dao层.service层和web层). 第一:web层事务,这一般是针对那些安 ...

  8. 膜拜 2014-2 (献给L之三)

    文/安然 深深的夜静静的想你细细的品味满满的甜蜜爱,心灵的对话让我流泪战栗谢谢你给我这么弥足珍贵的体会不能一生相随又有什么关系你一直都在我的爱就永不停息此生足以——献给心底的爱

  9. Windows Phone 为指定容器内的元素设置样式

    在Windows Phone中设置元素样式有多种 拿TextBlock来说 1.我们可以直接在控件上设置: <TextBlock Text="自身样式设置" Width=&q ...

  10. ASP.Net MVC中JSON处理。

    实体数据Model [Serializable] public class UserModel { //public UserModel(string name, string classname, ...