How to catch/detect exceptions in multi-threaded map/reduce using Reactor framework 2.x?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 04:25:09

问题


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.

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.

回答1:


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

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

See more:

https://projectreactor.io/docs/core/release/reference/#error.handling



来源:https://stackoverflow.com/questions/27976372/how-to-catch-detect-exceptions-in-multi-threaded-map-reduce-using-reactor-framew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!