gpt4 book ai didi

project-reactor - 带短路选项的 Flux.concatDelayError

转载 作者:行者123 更新时间:2023-12-04 10:53:49 25 4
gpt4 key购买 nike

我正在使用 Flux.concatDelayError因为我想订阅多个 Monos一个一个,也想知道有没有失败。

但是,现在我也想短路如果我的 Monos 之一以特定类型的错误结束。
这很容易吗?

最佳答案

使用 onErrorResume运算符,您可以配置一个条件回退到 Mono.empty()对于每个单声道:

package com.example;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static java.util.function.Predicate.not;

public class ReactorExample
{
public static void main(String[] args)
{
Mono<String> mono = Mono.just("first").doOnNext(a -> System.out.println(a + " was called."));

Mono<String> mono2 = Mono.<String>error(new RuntimeException("Not terminating error."))
.onErrorResume(not(ShortCircuitingException.class::isInstance), e -> Mono.empty());

Mono<String> mono3 = Mono.just("third").doOnNext(a -> System.out.println(a + " was called."));

Mono<String> mono4 = Mono.<String>error(new ShortCircuitingException())
.onErrorResume(not(ShortCircuitingException.class::isInstance), e -> Mono.empty());

Mono<String> mono5 = Mono.just("fifth").doOnNext(a -> System.out.println(a + " was called."));

Flux.concat(mono, mono2, mono3, mono4, mono5)
.collectList()
.block();
}

private static class ShortCircuitingException extends RuntimeException
{
}
}

输出:
first was called.
third was called.
Exception in thread "main" com.example.ReactorExample$ShortCircuitingException

关于project-reactor - 带短路选项的 Flux.concatDelayError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com