如何从Akka流接收器中引发的异常中恢复?
简单示例:
Source<Integer, NotUsed> integerSource = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
integerSource.runWith(Sink.foreach(x -> {
if (x == 4) {
throw new Exception("Error Occurred");
}
System.out.println("Sink: " + x);
}), system);
输出:
Sink: 1
Sink: 2
Sink: 3
如何处理异常并从源代码移至下一个元素? (aka 5,6,7,8,9)
默认情况下,抛出异常时,supervision strategy将停止流。要更改监视策略以删除引起异常的消息并继续下一条消息,请使用“恢复”策略。例如: