在上一篇讨论中我们谈到了监管:在Akka中就是一种直属父子监管树结构,父级Actor负责处理直属子级Actor产生的异常。当时我们把BackoffSupervisor作为父子监管方式的其中一种。实际上BackoffSupervisor与定义了supervisorStrategy的Actor有所不同。我们应该把BackoffSupervisor看作是一个一体化的Actor。当然,它的实现方式还是由一对父子Actor组成。监管策略(SupervisorStrategy)是在BackoffSupervisor的内部实现的。从外表上BackoffSupervisor就像是一个Actor,运算逻辑是在子级Actor中定义的,所谓的父级Actor除监管之外没有任何其它功能,我们甚至没有地方定义父级Actor的功能,它的唯一功能是转发收到的信息给子级,是嵌入BackoffSupervisor里的。所以我们虽然发送消息给BackoffSupervisor,但实际上是在与它的子级交流。我们看看下面这个例子:

package backoffSupervisorDemo
import akka.actor._
import akka.pattern._
import backoffSupervisorDemo.InnerChild.TestMessage import scala.concurrent.duration._ object InnerChild {
case class TestMessage(msg: String)
class ChildException extends Exception def props = Props[InnerChild]
}
class InnerChild extends Actor with ActorLogging {
import InnerChild._
override def receive: Receive = {
case TestMessage(msg) => //模拟子级功能
log.info(s"Child received message: ${msg}")
}
}
object Supervisor {
def props: Props = { //在这里定义了监管策略和child Actor构建
def decider: PartialFunction[Throwable, SupervisorStrategy.Directive] = {
case _: InnerChild.ChildException => SupervisorStrategy.Restart
} val options = Backoff.onFailure(InnerChild.props, "innerChild", second, seconds, 0.0)
.withManualReset
.withSupervisorStrategy(
OneForOneStrategy(maxNrOfRetries = , withinTimeRange = seconds)(
decider.orElse(SupervisorStrategy.defaultDecider)
)
)
BackoffSupervisor.props(options)
}
}
//注意:下面是Supervisor的父级,不是InnerChild的父级
object ParentalActor {
case class SendToSupervisor(msg: InnerChild.TestMessage)
case class SendToInnerChild(msg: InnerChild.TestMessage)
case class SendToChildSelection(msg: InnerChild.TestMessage)
def props = Props[ParentalActor]
}
class ParentalActor extends Actor with ActorLogging {
import ParentalActor._
//在这里构建子级Actor supervisor
val supervisor = context.actorOf(Supervisor.props,"supervisor")
supervisor ! BackoffSupervisor.getCurrentChild //要求supervisor返回当前子级Actor
var innerChild: Option[ActorRef] = None //返回的当前子级ActorRef
val selectedChild = context.actorSelection("/user/parent/supervisor/innerChild")
override def receive: Receive = {
case BackoffSupervisor.CurrentChild(ref) => //收到子级Actor信息
innerChild = ref
case SendToSupervisor(msg) => supervisor ! msg
case SendToChildSelection(msg) => selectedChild ! msg
case SendToInnerChild(msg) => innerChild foreach(child => child ! msg)
} }
object BackoffSupervisorDemo extends App {
import ParentalActor._
val testSystem = ActorSystem("testSystem")
val parent = testSystem.actorOf(ParentalActor.props,"parent") Thread.sleep() //wait for BackoffSupervisor.CurrentChild(ref) received parent ! SendToSupervisor(TestMessage("Hello message 1 to supervisor"))
parent ! SendToInnerChild(TestMessage("Hello message 2 to innerChild"))
parent ! SendToChildSelection(TestMessage("Hello message 3 to selectedChild")) scala.io.StdIn.readLine() testSystem.terminate() }

在上面的例子里我们分别向supervisor,innerChild,selectedChild发送消息。但所有消息都是由InnerChild响应的,如下:

[INFO] [// ::48.167] [testSystem-akka.actor.default-dispatcher-] [akka://testSystem/user/parent/supervisor/innerChild] Child received message: Hello message 1 to supervisor
[INFO] [// ::48.177] [testSystem-akka.actor.default-dispatcher-] [akka://testSystem/user/parent/supervisor/innerChild] Child received message: Hello message 2 to innerChild
[INFO] [// ::48.179] [testSystem-akka.actor.default-dispatcher-] [akka://testSystem/user/parent/supervisor/innerChild] Child received message: Hello message 3 to selectedChild

上面我们向supervisor发送了一个BackoffSupervisor.GetCurrentChild消息用来获取子级Actor。BackoffSupervisor是这样处理下面几个特殊消息的:

private[akka] trait HandleBackoff { this: Actor ⇒
def childProps: Props
def childName: String
def reset: BackoffReset var child: Option[ActorRef] = None
var restartCount = import BackoffSupervisor._
import context.dispatcher override def preStart(): Unit = startChild() def startChild(): Unit = {
if (child.isEmpty) {
child = Some(context.watch(context.actorOf(childProps, childName)))
}
} def handleBackoff: Receive = {
case StartChild ⇒
startChild()
reset match {
case AutoReset(resetBackoff) ⇒
val _ = context.system.scheduler.scheduleOnce(resetBackoff, self, ResetRestartCount(restartCount))
case _ ⇒ // ignore
} case Reset ⇒
reset match {
case ManualReset ⇒ restartCount =
case msg ⇒ unhandled(msg)
} case ResetRestartCount(current) ⇒
if (current == restartCount) {
restartCount =
} case GetRestartCount ⇒
sender() ! RestartCount(restartCount) case GetCurrentChild ⇒
sender() ! CurrentChild(child) case msg if child.contains(sender()) ⇒
// use the BackoffSupervisor as sender
context.parent ! msg case msg ⇒ child match {
case Some(c) ⇒ c.forward(msg)
case None ⇒ context.system.deadLetters.forward(msg)
}
}
}

在handleBackoff函数里可以找到这些消息的处理方式。

在构建上面例子里的Supervisor的Props时定义了监管策略(SupervisorStrategy)对InnerChild产生的异常ChildException进行Restart处理。我们调整一下InnerChild代码来随机产生一些异常:

object InnerChild {
case class TestMessage(msg: String)
class ChildException(val errmsg: TestMessage) extends Exception
object CException { //for pattern match of class with parameter
def apply(msg: TestMessage) = new ChildException(msg)
def unapply(cex: ChildException) = Some(cex.errmsg)
}
def props = Props[InnerChild]
}
class InnerChild extends Actor with ActorLogging {
import InnerChild._
context.parent ! BackoffSupervisor.Reset //reset backoff counts
override def receive: Receive = {
case TestMessage(msg) => //模拟子级功能
if (Random.nextBoolean()) //任意产生异常
throw new ChildException(TestMessage(msg))
else
log.info(s"Child received message: ${msg}")
}
}

我们用Random.nextBoolean来任意产生一些异常。注意:我们同时把ChildException改成了一个带参数的class,因为我们可能需要在重启之前获取造成异常的消息,如下:

    def decider: PartialFunction[Throwable, SupervisorStrategy.Directive] = {
case InnerChild.CException(tmsg) =>
println(s"Message causing exception: ${tmsg.msg}") //we can extract message here
SupervisorStrategy.Restart
}

所有信息发给supervisor就行了:

class ParentalActor extends Actor with ActorLogging {
import ParentalActor._
//在这里构建子级Actor supervisor
val supervisor = context.actorOf(Supervisor.props,"supervisor")
override def receive: Receive = {
case msg@ _ => supervisor ! msg
} }
object BackoffSupervisorDemo extends App {
import ParentalActor._
import InnerChild._
val testSystem = ActorSystem("testSystem")
val parent = testSystem.actorOf(ParentalActor.props,"parent") parent ! TestMessage("Hello message 1 to supervisor")
parent ! TestMessage("Hello message 2 to supervisor")
parent ! TestMessage("Hello message 3 to supervisor")
parent ! TestMessage("Hello message 4 to supervisor")
parent ! TestMessage("Hello message 5 to supervisor")
parent ! TestMessage("Hello message 6 to supervisor") scala.io.StdIn.readLine() testSystem.terminate() }

运行后发现在出现异常后所有消息都变成了DeadLetter:

[INFO] [// ::11.689] [testSystem-akka.actor.default-dispatcher-] [akka://testSystem/user/parent/supervisor/innerChild] Message [backoffSupervisorDemo.InnerChild$TestMessage] from Actor[akka://testSystem/user/parent#2140150413] to Actor[akka://testSystem/user/parent/supervisor/innerChild#-1047097634] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
....

这也证明了BackoffSupervisor具有不同的Restart处理方式,好像是直接终止InnerChild而非正常的挂起,销毁了ActorRef和邮箱,所以在完成启动之前发给InnerChild的消息都被导入DeadLetter队列了。也就是说不但错过造成异常的消息,而是跳过了下面启动时间段内所有的消息。

下面我们来解决失踪消息的问题:首先是如何重新发送造成异常的消息,我们可以在监管策略中重启前发送:

    def decider: PartialFunction[Throwable, SupervisorStrategy.Directive] = {
case InnerChild.CException(tmsg) =>
println(s"Message causing exception: ${tmsg.msg}") //we can extract message here
BackoffSupervisorDemo.sendToParent(tmsg) //resend message
SupervisorStrategy.Restart
}

在BackoffSupervisorDemo里先声明sendToParent函数:

  def sendToParent(msg: TestMessage) = parent ! msg

然后再想办法把DeadLetter捞出来。我们可以用Akka的eventStream来订阅DeadLetter类型消息:

object DeadLetterMonitor {
def props(parentRef: ActorRef) = Props(new DeadLetterMonitor(parentRef))
}
class DeadLetterMonitor(receiver: ActorRef) extends Actor with ActorLogging {
import InnerChild._
import context.dispatcher
override def receive: Receive = {
case DeadLetter(msg,sender,_) =>
//wait till InnerChild finishes restart then resend
context.system.scheduler.scheduleOnce( second,receiver,msg.asInstanceOf[TestMessage])
}
}
object BackoffSupervisorDemo extends App {
import ParentalActor._
import InnerChild._ def sendToParent(msg: TestMessage) = parent ! msg val testSystem = ActorSystem("testSystem")
val parent = testSystem.actorOf(ParentalActor.props,"parent") val deadLetterMonitor = testSystem.actorOf(DeadLetterMonitor.props(parent),"dlmonitor")
testSystem.eventStream.subscribe(deadLetterMonitor,classOf[DeadLetter]) //listen to DeadLetter parent ! TestMessage("Hello message 1 to supervisor")
parent ! TestMessage("Hello message 2 to supervisor")
parent ! TestMessage("Hello message 3 to supervisor")
parent ! TestMessage("Hello message 4 to supervisor")
parent ! TestMessage("Hello message 5 to supervisor")
parent ! TestMessage("Hello message 6 to supervisor") scala.io.StdIn.readLine() testSystem.terminate() }

试运算后显示InnerChild成功处理了所有6条消息。

下面是本次讨论的完整示范代码:

package backoffSupervisorDemo
import akka.actor._
import akka.pattern._
import scala.util.Random import scala.concurrent.duration._ object InnerChild {
case class TestMessage(msg: String)
class ChildException(val errmsg: TestMessage) extends Exception
object CException { //for pattern match of class with parameter
def apply(msg: TestMessage) = new ChildException(msg)
def unapply(cex: ChildException) = Some(cex.errmsg)
}
def props = Props[InnerChild]
}
class InnerChild extends Actor with ActorLogging {
import InnerChild._
context.parent ! BackoffSupervisor.Reset //reset backoff counts
override def receive: Receive = {
case TestMessage(msg) => //模拟子级功能
if (Random.nextBoolean()) //任意产生异常
throw new ChildException(TestMessage(msg))
else
log.info(s"Child received message: ${msg}")
}
}
object Supervisor {
def props: Props = { //在这里定义了监管策略和child Actor构建
def decider: PartialFunction[Throwable, SupervisorStrategy.Directive] = {
case InnerChild.CException(tmsg) =>
println(s"Message causing exception: ${tmsg.msg}") //we can extract message here
BackoffSupervisorDemo.sendToParent(tmsg) //resend message
SupervisorStrategy.Restart
} val options = Backoff.onFailure(InnerChild.props, "innerChild", second, seconds, 0.0)
.withManualReset
.withSupervisorStrategy(
OneForOneStrategy(maxNrOfRetries = , withinTimeRange = seconds)(
decider.orElse(SupervisorStrategy.defaultDecider)
)
)
BackoffSupervisor.props(options)
}
}
//注意:下面是Supervisor的父级,不是InnerChild的父级
object ParentalActor {
case class SendToSupervisor(msg: InnerChild.TestMessage)
case class SendToInnerChild(msg: InnerChild.TestMessage)
case class SendToChildSelection(msg: InnerChild.TestMessage)
def props = Props[ParentalActor]
}
class ParentalActor extends Actor with ActorLogging {
import ParentalActor._
//在这里构建子级Actor supervisor
val supervisor = context.actorOf(Supervisor.props,"supervisor")
override def receive: Receive = {
case msg@ _ => supervisor ! msg
} }
object DeadLetterMonitor {
def props(parentRef: ActorRef) = Props(new DeadLetterMonitor(parentRef))
}
class DeadLetterMonitor(receiver: ActorRef) extends Actor with ActorLogging {
import InnerChild._
import context.dispatcher
override def receive: Receive = {
case DeadLetter(msg,sender,_) =>
//wait till InnerChild finishes restart then resend
context.system.scheduler.scheduleOnce( second,receiver,msg.asInstanceOf[TestMessage])
}
}
object BackoffSupervisorDemo extends App {
import ParentalActor._
import InnerChild._ def sendToParent(msg: TestMessage) = parent ! msg val testSystem = ActorSystem("testSystem")
val parent = testSystem.actorOf(ParentalActor.props,"parent") val deadLetterMonitor = testSystem.actorOf(DeadLetterMonitor.props(parent),"dlmonitor")
testSystem.eventStream.subscribe(deadLetterMonitor,classOf[DeadLetter]) //listen to DeadLetter parent ! TestMessage("Hello message 1 to supervisor")
parent ! TestMessage("Hello message 2 to supervisor")
parent ! TestMessage("Hello message 3 to supervisor")
parent ! TestMessage("Hello message 4 to supervisor")
parent ! TestMessage("Hello message 5 to supervisor")
parent ! TestMessage("Hello message 6 to supervisor") scala.io.StdIn.readLine() testSystem.terminate() }

Akka(3): Actor监管 - 细述BackoffSupervisor的更多相关文章

  1. 二 Akka学习 - actor介绍

    一个actorSystem 是一个重量级的结构.它会分配N个线程.所以对于每一个应用来说只用创建一个ActorSystem. Actor是种可怜的“生物”,它们不能独自存活.Akka中的每一个Acto ...

  2. 细述 Java垃圾回收机制→Types of Java Garbage Collectors

    细述 Java垃圾回收机制→Types of Java Garbage Collectors 转自:https://segmentfault.com/a/1190000006214497 本文非原创, ...

  3. [翻译]AKKA笔记 -ACTOR SUPERVISION - 8

    失败更像是分布式系统的一个特性.因此Akka用一个容忍失败的模型,在你的业务逻辑与失败处理逻辑(supervision逻辑)中间你能有一个清晰的边界.只需要一点点工作,这很赞.这就是我们要讨论的主题. ...

  4. [翻译]AKKA笔记 - ACTOR MESSAGING - REQUEST AND RESPONSE -3

    上次我们看Actor消息机制,我们看到开火-忘记型消息发出(意思是我们只要发个消息给Actor但是不期望有响应). 技术上来讲, 我们发消息给Actors就是要它的副作用. 这就是这么设计的.除了不响 ...

  5. 【Akka】Actor模型探索

    Akka是什么 Akka就是为了改变编写高容错性和强可扩展性的并发程序而生的.通过使用Actor模型我们提升了抽象级别,为构建正确的可扩展并发应用提供了一个更好的平台.在容错性方面我们採取了" ...

  6. [翻译]AKKA笔记 - ACTOR生命周期 - 基本 -5

    原文地址:http://rerun.me/2014/10/21/akka-notes-actor-lifecycle-basic/ (请注意这了讨论的生命周期并不包括 preRestart 或者pos ...

  7. 翻译:AKKA笔记 - Actor消息 -1(二)

    消息 我们只是让QuoteRequest到ActorRef去但是我们根本没见过消息类! 它是这样的: (一个最佳实践是把你的消息类包装在一个完整的对象里以利于更好的组织) TeacherProtoco ...

  8. 翻译:AKKA笔记 - Actor消息 -1(一)

    从第一篇Akka笔记的介绍中,我们是从很高的高度去观察Akka工具箱中的Actors.在这篇笔记的第二篇,我们会看一下Actors中的消息部分.而且延续上一次的例子,我们还会使用同样的学生与老师的例子 ...

  9. Akka的Actor模型及使用实例

    本文的绝大部分内容转载自rerun.me这一blog,老外写的东西就是好啊. ACTORS介绍 Anyone who has done multithreading in the past won't ...

随机推荐

  1. 事件驱动的Python实现

    EventManager事件管理类实现,大概就百来行代码左右.如果有不了事件驱动的工作原理的可以看前一篇<事件驱动的简明讲解> # encoding: UTF-8 # 系统模块 from ...

  2. shell初步了解

    shell的类型 查看用户所用的shell程序,在/etc/passwd 文件中的第七个字段(好像就是最后一个,主要是bash shell) 还有一个默认shell是/bin/sh,它作为默认的系统s ...

  3. node服务成长之路

    我们的系统也从第一代平台开始到现在第四代平台更换中,对这四代平台做一个简单的介绍: 第一代平台,主要是集中式,以快速上线为目的:第二代平台主要是分布式改造,缓解各服务压力:第三代平台主要做服务端SOA ...

  4. 基于opencv的车牌识别系统

    前言 学习了很长一段时间了,需要沉淀下,而最好的办法就是做一个东西来应用学习的东西,同时也是一个学习的过程. 概述     OpenCV的全称是:Open Source Computer Vision ...

  5. aos.js超赞页面滚动元素动画jQuery动画库

    插件描述:aos.js 是一款效果超赞的页面滚动元素动画jQuery动画库插件.该动画库可以在页面滚动时提供28种不同的元素动画效果,以及多种easing效果.在页面往回滚动时,元素会恢复到原来的状态 ...

  6. html、css、js实现简易计算器

    学习HTML,CSS,JS一个月后,想着能自己是否能写出一个简单的东西,故编写了简易的计算器,之前也写过一个坦克大战,坦克大战的有些基本功能没有实现, 故也没有记录下来,想来,对这行初来咋到的,还是需 ...

  7. Ruby中有意思的块

    块:是在调用方法时,能与参数一起传递的多个处理的集合 简单点说,跟在方法执行后面的do |变量| end就是一个块,这个块会被传入方法中去执行! 这个非常厉害,非常有意思! 在ruby中,如果需要便利 ...

  8. 【lucene系列学习四】log4j日志文件实现多线程的测试

    参考资料:http://nudtgk2000.iteye.com/blog/1716379 首先,在http://www.apache.org/dyn/closer.cgi/logging/log4j ...

  9. 关于jstl.jar引用问题及解决方法

    在前文SSM说到因为从MyEclipse换成了Eclipse.有些架包自动缺失. 造成:"org.apache.jasper.JasperException: This absolute u ...

  10. iOS 详解NSObject协议

      协议就是一组接口的集合,遵守一个协议之后就拥有的该协议中所有方法的声明.NSObject这个类遵守了NSObject协议,并且实现了NSObject协议里的所有方法,所以NSObject类及其子类 ...