[英]How to manually throw HTTP 404 Not Found exception in spray/akka?
[英]How to throw an exception in akka stream?
我想拋出一個異常如下:
Source.empty
.map {
throw new RuntimeException("Failed")
}
.runWith(Sink.foreach(println))
.onComplete {
case Success(_) =>
println()
case Failure(e) =>
println(s"Thrown ${e.getMessage}")
}
但是該異常不會出現在onComplete
方法中。 它打印
Exception in thread "main" java.lang.RuntimeException: Failed
at com.sweetsoft.App$.main(App.scala:30)
at com.sweetsoft.App.main(App.scala)
如何引發異常,這將停止流並在末尾出現?
Akka內置了錯誤處理: Akka監督策略
val testSupervisionDecider: Supervision.Decider = {
case ex: java.lang.RuntimeException =>
println(s"some run time exception ${ex.getMessage}")
Supervision.Stop
case ex: Exception =>
println("Exception occurred and stopping stream",
ex)
Supervision.Stop
}
您可以將監督決策者用作
val list = List.range(1, 100)
Source(list).map { item =>
if ((item % 2) == 0) {
throw new RuntimeException(s"$item")
} else {
item
}
}.withAttributes(ActorAttributes.supervisionStrategy(testSupervisionDecider))
.runWith(Sink.foreach(println)).onComplete {
case Success(_) =>
println()
case Failure(e) =>
println(s"Thrown ${e.getMessage}")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.