嗨,我想知道是否有人可以帮助我理解第三方创建的GraphStage的异常处理。
我正在使用GraphStage引发异常并停止流。我想做的是记录有关错误的所有信息(失败之前传递给GraphStage的所有信息)并继续进行处理。 我尝试了 recover 和监督策略,但是它们不允许流继续进行。
这是一个GraphStage示例,演示了我的问题。我明确抛出异常,这可能不是最佳实践。
import akka.stream.{Attributes, FlowShape, Inlet, Outlet}
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
class TestStage extends GraphStage[FlowShape[Int, Int]] {
private val in = Inlet[Int]("Test.in")
private val out = Outlet[Int]("Test.out")
override val shape = FlowShape.of(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) with InHandler with OutHandler {
setHandlers(in, out, this)
override def onPush(): Unit = {
val num = grab(in)
if (num == 5) {
throw new Exception(s"Number is 5")
}
push(out, num)
}
override def onPull(): Unit = pull(in)
}
}
Source(1 to 10)
.via(Flow.fromGraph(new TestStage))
.withAttributes(ActorAttributes.supervisionStrategy(Supervision.resumingDecider))
.recover {
case e: Exception => e.getMessage
}
.runForeach(println)
这个不使用GraphStage的示例会继续处理。所以看起来 从GraphStage抛出的异常需要区别对待吗?
Source(1 to 10)
.map {
case 5 => throw new Exception("5 is bad")
case n => n
}
.withAttributes(ActorAttributes.supervisionStrategy(Supervision.resumingDecider))
.runForeach(println)
感谢您的帮助
答案 0 :(得分:0)