Reactive - 03 - Mono

翻译自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/Mono

Mono


Description


A Mono<T> is a Reactive Streams Publisher, also augmented with a lot of operators
that can be used to generate, transform, orchestrate Mono sequences.

Mono是一种反应流发布者,还增加了许多运算符,可用于生成、转换和编排Mono序列。

It is a specialization of Flux that can emit at most 1 <T> element:
a Mono is either valued (complete with element), empty (complete without element)
or failed (error).

它是Flux的特例,最多可以发射1个元素:Mono要么有值(带元素完成),要么为空(无元素完成),要么为失败(错误)。

A Mono<Void> can be used in cases where only the completion signal is interesting
(the Reactive Streams equivalent of a Runnable task completing).

在只对完成信号感兴趣的情况下(相当于Runnable任务完成的反应流),可以使用Mono<Void>。

Like for Flux, the operators can be used to define an asynchronous pipeline
which will be materialized anew for each Subscription.

与Flux一样,可以使用运算符定义异步管道,该管道将针对每个订阅重新具体化。

Note that some API that change the sequence's cardinality will return a Flux
(and vice-versa, APIs that reduce the cardinality to 1 in a Flux return a Mono).

请注意,一些更改序列基数的API将返回Flux(反之亦然,在Flux中将基数减少到1的API将返回Mono)。
03_Mono.png

Mono in action:

Mono.firstWithValue(
    Mono.just(1).map(integer -> "foo" + integer),
Mono.delay(Duration.ofMillis(100)).thenReturn("bar")
    )
    .subscribe(System.out::println);

Practice


As for the Flux let's return a empty Mono using the static factory.

就像Flux,让我们使用静态工厂返回一个空Mono。
    // Return an empty Mono
    Mono<String> emptyMono() {
        return Mono.empty();
    }

Now, we will try to create a Mono which never emits anything.
Unlike empty(), it won't even emit an onComplete event.

现在,我们将尝试创建一个从不发射任何东西的Mono。与empty()不同,它甚至不会发出onComplete事件。
    // Return a Mono that never emits any signal
    Mono<String> monoWithNoSignal() {
        return Mono.never();
    }

Like Flux, you can create a Mono from an available (unique) value.

与Flux一样,您可以从可用(唯一)值创建Mono。
    // Return a Mono that contains a "foo" value
    Mono<String> fooMono() {
        return Mono.just("foo");
    }

And exactly as we did for the flux, we can propagate exceptions.

正如我们对Flux所做的那样,我们可以传播异常。
    // Create a Mono that emits an IllegalStateException
    Mono<String> errorMono() {
        return Mono.error(new IllegalStateException());
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容