除非需要以非常精细的方式或不同的方式在单个参与者中处理异常,否则我将让内置的管理策略处理异常以及相应的参与者重新启动/终止等。它还具有更好的可读性,因为它具有故障处理逻辑的中心位置。
我们能在管理策略中指定一些其他错误处理逻辑吗?
除了继续还是重新启动?
还有其他故障处理操作,如
Stop
,
Escalate
在管理策略上。
停止
终止子演员并
逐步升级
向上升级到主管的家长。在返回
Directive
在每一个
case
,只要
decider
代码块符合
PartialFunction[Throwable, Directive]
. 例如:
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
case e: ArithmeticException =>
myLogActor ! s"ERROR: $e from $sender; Resuming."
Resume
case _: Exception =>
myLogActor ! s"ERROR: unknown exception from $sender; Escalating."
Escalate
}
作为旁注,如果你必须
try/catch
方法,考虑使用
Try
相反,如下所示:
Try(dbFunction(user, pass)) match {
case Success(res) => // do something with `res`
case Failure(e: SQLException) => // log `e`, etc
case Failure(e) => // other exceptions ...
}