本文介绍了如何使用Reactor框架2.x在多线程映射/归约中捕获/检测异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用,它可以正常运行。
但是,如果引发异常,则调用者代码不会捕获该异常。

I was playing with the code of this answer and it works smoothly.However, if an exception is thrown, the caller code does not catch it.

如何在反应堆2.0流中捕获异常?
我想做的是:如果抛出异常,则流处理必须停止。我需要在调用者线程(首先创建蒸汽的线程)中抛出异常。

How is an Exception captured in reactor 2.0 streams? What I want to do is: if an Exception is thrown, stream processing must stop. I need to throw the Exception up in the caller thread (the one that created the steam in first place).

List<Map<String, Object>> data = readData(); 

Streams.from(data)
       .flatMap(m -> Streams.just(m)
                            .dispatchOn(Environment.cachedDispatcher()) 
                            .map(ignored -> {throw new RuntimeException("kaboom!");}))
       .buffer() 
       .consume(s -> System.out.println("s: " + s)); 
// the exception is not thrown and there is not opportunity to deal with it.


推荐答案

在Reactor中,您只需要包装异常并返回它们as Flux.error()

In Reactor you just need to wrap exceptions and return them as Flux.error()

然后您可以在 onErrorXXX 方法(例如 onErrorResume

Then you can handle them in onErrorXXX methods (eg. onErrorResume)

查看更多:

这篇关于如何使用Reactor框架2.x在多线程映射/归约中捕获/检测异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:40