【问题标题】:How to catch/detect exceptions in multi-threaded map/reduce using Reactor framework 2.x?如何使用 Reactor 框架 2.x 捕获/检测多线程映射/归约中的异常?
【发布时间】:2015-01-16 02:24:05
【问题描述】:

我正在使用this answer 的代码,它运行顺利。 但是,如果抛出异常,调用者代码不会捕获它。

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

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.

【问题讨论】:

    标签: java multithreading concurrency mapreduce reactor


    【解决方案1】:

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

    然后您可以在onErrorXXX 方法中处理它们(例如onErrorResume

    查看更多:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2015-08-31
      • 2011-09-26
      相关资源
      最近更新 更多