实际上,Supervision.Restart和Supervision.Resume有什么区别? [英] What's the difference between Supervision.Restart and Supervision.Resume really?

查看:180
本文介绍了实际上,Supervision.Restart和Supervision.Resume有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Supervision.Restart Supervision.Resume 之间有什么区别?

what's the difference between Supervision.Restart and Supervision.Resume really ?

这里是情况。我有3个元素来自 Source(List(1、2、3))。在 runForeach 中,如果元素为 2 ,则会引发异常。对于 Supervision.Restart ,我希望仅处理 1 。但是奇怪的是,我看到 3 陷入沉没。为什么呢我正在使用Akka 2.4.11

Here is the situation. I have 3 elements coming from the Source(List(1, 2, 3)). In runForeach I throw exception if element is 2. For Supervision.Restart I expected only 1 to be processed. But oddly I see 3 reaching sink. Why ? I'm using Akka 2.4.11

import akka.actor.{ActorRef, ActorSystem}
import akka.stream.{ActorMaterializer, ActorMaterializerSettings, OverflowStrategy, Supervision}
import akka.stream.scaladsl.Source

val decider: Supervision.Decider = {
  case _: NotImplementedError =>
    Supervision.Restart
  case _                      =>
    Supervision.Stop
}

implicit val system = ActorSystem("root")
implicit val executor = system.dispatcher
implicit val materializer = ActorMaterializer(ActorMaterializerSettings(system).withSupervisionStrategy(decider))(system)

Source(List(1, 2, 3))
  .map { v =>
    if (v == 2)
      throw new NotImplementedError
    v
  }
  .runForeach(println)

https://scalafiddle.io/sf/ HSj01pO / 3

推荐答案

在您的示例中,您的是(也称为 FlowShape ):

In your example your Flow is (also called FlowShape):

.map { v =>
  if (v == 2)
    throw new NotImplementedError
  v
}

主要区别是:


  • Supervision。重新启动:将创建一个演员的新实例,该实例将具有您的班级的初始状态。先前的状态将丢失。在您的情况下,这将是您的 Flow 的状态。

  • Supervision.Restart: a new instance of your actor will be created which will have the initial state of your class. The previous state will be lost. In your case it will be the state of your Flow.

监督.Resume :同一实例以以前的状态重用。在您的情况下,这将是您的 Flow 的状态。

Supervision.Resume: the same instance is reused with the previous state. In your case it will be the state of your Flow.

对于您来说,您的是无状态的,因此 Supervision.Restart 和<$ c $之间没有区别c> Supervision.Resume 。

In your case, your Flow is stateless so there is no difference between Supervision.Restart and Supervision.Resume.

由于您有情况_:NotImplementedError =>监督。重新启动,然后 1 获取地图并到达下沉 2 抛出一个 NotImplementedError ,它重新启动您的 Flow ,将其丢弃消息,但不会阻止您的信息流。然后 3 得到地图,到达水槽。因此,您应该在输出 1 3 中看到。

Since you have case _: NotImplementedError => Supervision.Restart, then 1 gets mapped and reaches the Sink. 2 throws a NotImplementedError which restarts your Flow, drops that message but does not stop your stream. And then 3 gets mapped and reaches the Sink. Therefore you should see in the output 1 and 3.

这篇关于实际上,Supervision.Restart和Supervision.Resume有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆