Spring Reactor:Mono.zip 在空 Mono 上失败 [英] Spring Reactor: Mono.zip fails on empty Mono

查看:260
本文介绍了Spring Reactor:Mono.zip 在空 Mono 上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Reactor 3.1.0.M3 并且有一个用例,我需要合并来自多个来源的 Mono.我发现如果 Mono 之一是空 Mono,则 zip 会失败而不会出错.

I am using Spring Reactor 3.1.0.M3 and have a use case where I need to merge Mono's from multiple sources. I found that if one of the Monos is an empty Mono, zip fails without an error.

示例:

Mono<String> m1 = Mono.just("A");
Mono<String> m2 = Mono.just("B");
Mono<String> m3 = Mono.empty();

Mono<String> combined = Mono.zip(strings -> {
    StringBuffer sb = new StringBuffer();
    for (Object string : strings) {
        sb.append((String) string);
    }
    return sb.toString();
}, m1, m2, m3);
System.out.println("Combined " + combined.block());

添加m3时,在响应为null时跳过组合子.当我删除 m3 时,一切都按预期工作,并返回AB".有没有办法通过检测空的 Mono 来处理这个问题?另外,有没有办法让组合器方法知道对象的类型而不必强制转换?

When m3 is added, the combinator is skipped in the response is null. When I remove m3, it all works as expected and "AB" is returned. Is there a way I could handle this by detecting the empty Mono? Also, is there a way to have the combinator method know the type of the object instead of having to cast?

推荐答案

zip 运算符的行为不是这样的.这实际上是违反直觉的:您的代码需要一个包含 3 个元素的元组,而您只得到两个?!?

The zip operator doesn't behave like this. It would be in fact counter-intuitive: your code is expecting a Tuple of 3 elements and you're only getting two?!?

在这种情况下,您处于控制之中,如果没有提供,则只有您可以决定什么是好的默认值(请记住,响应流规范禁止 null 值).

In this case, you're in control and only you can decide what's a good default value if none is provided (remember, null values are forbidden by the reactive streams specification).

Mono<String> m1 = Mono.just("A");
Mono<String> m2 = Mono.just("B");
Mono<String> m3 = Mono.empty().defaultIfEmpty("");

Mono<String> combined = Mono.when(m1, m2, m3).map(t -> {
    StringBuffer sb = new StringBuffer();
    sb.append(t.getT1());
    sb.append(t.getT2());
    sb.append(t.getT3());
    return sb.toString();
});

编辑

您似乎对 Publisher 类型的性质感到困惑,请参阅:

Edit

You seem to be confused by the nature of a Publisher type, see:

如果 Monos 之一是空 Mono,则 zip 失败且不会出错

if one of the Monos is an empty Mono, zip fails without an error

因此,如果我尝试压缩 Mono 并且由于某种原因一个是空的,则 zip 将失败,我似乎无法输入任何代码来防止这种情况发生

So if I was to try and zip Mono's and for some reason one is empty, the zip would fail and I cannot seem to put in any code to safeguard against that

一个空的 Mono 不是失败的情况:它只是没有发出任何值并且成功完成.您可以通过更改代码示例来验证:

An empty Mono isn't a failure case: it's just that no value is emitted and it is completed successfully. You can verify that by changing the code sample:

    combined.subscribe(
            s -> System.out.println("element: " + s), // doesn't execute
            s -> System.out.println("error: " + s), // doesn't execute
            () -> { System.out.println("complete!"); // prints
    });

因此,根据您的要求,您可以:

So depending on your requirements, you can:

  • 在这 3 个 Mono 实例上应用 defaultIfEmpty 运算符,如果有方便的默认值可以依赖
  • 在组合的 Mono 上应用 defaultIfEmpty 运算符,使用默认值,或者甚至使用 combined.switchIfEmpty(Mono.error(...))
  • apply a defaultIfEmpty operator on those 3 Mono instances, if there are convenient default values you can rely on
  • apply a defaultIfEmpty operator on the combined Mono, with a default value or even transform that into an error message with combined.switchIfEmpty(Mono.error(...))

这篇关于Spring Reactor:Mono.zip 在空 Mono 上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆