Springboot中的Schedulers.elastic时,Webflux不会传播异常

时间:2018-12-13 08:15:16

标签: spring spring-boot exception-handling spring-webflux

使用webflux尝试在Springboot上处理异常时遇到问题。 我实现了一个ResponseEntityExceptionHandler类,该类工作正常,但是,如果从WebFlux线程抛出异常,则永远不会调用该异常(尽管如果使用Schedulers.immediate(),在这种情况下也会调用该异常处理程序)。 复制我发现的问题的最简单方法是执行:

Mono.fromCallable(() -> {
        throw new IllegalArgumentException();
    })
    .doOnError(throwable -> {
        throw new IllegalArgumentException();
    })
    .subscribeOn(Schedulers.elastic())
    .subscribe(result -> {}, throwable -> {throw new IllegalArgumentException();});

正如我所说,如果我不是使用Schedulers.elastic()而是使用Schedulers.immediate(),那么我可以在ResponseEntityExceptionHandler类上处理该异常。如何传播异常,以便始终可以在处理程序类上处理它?<​​/ p>

预先感谢

1 个答案:

答案 0 :(得分:0)

我终于让Spring(通过ExceptionHandler)处理异常,因此,在我的控制器中,我将响应设置为:

final DeferredResult<ResponseDTO> result = new DeferredResult<>();
Mono.fromCallable(...).subscribe(result::setResult, result::setErrorResult);