execute 2 Mono sequentialy 不支持阻塞

问题描述 投票:0回答:1

我有两个波纹管功能

Mono<Void> messageEvent(MessageEvent messageEvent);
Mono<Victim> getPersonById(String personId);

我想当函数

messageEvent
执行成功时我执行
getPersonById 
函数,并且
getPersonById 
函数的返回保存在数据库中

我试过如下

    spi.messageEvent(message).doOnSuccess(it -> {
        spi.getPersonById(evt.getVictimId()).doOnSuccess(victim -> {
            repository.save(victim);
        }).block();
    }).block();

但是我有这个错误

2023-03-09 21:56:20.191 错误 21080 --- [nister-group]-0] c.s.e.v.s.q.i:on() 中的异常 cause = 'NULL' 和 exception = 'block()/blockFirst()/blockLast() 是 阻塞,这在线程 reactor-http-nio-2 中不受支持' java.lang.IllegalStateException: block()/blockFirst()/blockLast() 是 阻塞,在线程 reactor-http-nio-2 中不支持 reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83

)

java spring spring-webflux reactive-programming project-reactor
1个回答
0
投票

您需要来自

Mono
的操作员:

/**
 * Let this {@link Mono} complete then play another Mono.
 * <p>
 * In other words ignore element from this {@link Mono} and transform its completion signal into the
 * emission and completion signal of a provided {@code Mono<V>}. Error signal is
 * replayed in the resulting {@code Mono<V>}.
 *
 * <p>
 * <img class="marble" src="doc-files/marbles/thenWithMonoForMono.svg" alt="">
 *
 * <p><strong>Discard Support:</strong> This operator discards the element from the source.
 *
 * @param other a {@link Mono} to emit from after termination
 * @param <V> the element type of the supplied Mono
 *
 * @return a new {@link Mono} that emits from the supplied {@link Mono}
 */
public final <V> Mono<V> then(Mono<V> other) {

所以,在你的代码中它可能是这样的:

 return spi.messageEvent(message)
     .then(spi.getPersonById(evt.getVictimId()))
     .map(repository::save);

是的:不要在 Reactor Http 线程中使用

block()

© www.soinside.com 2019 - 2024. All rights reserved.