- Hands-On Reactive Programming in Spring 5
- Oleh Dokuka Igor Lozynskyi
- 332字
- 2021-07-23 16:36:32
Mono
Now, let's look at how the Mono type is different from the Flux type:
In contrast with Flux, the Mono type defines a stream that can produce at most one element and can be described by the following formula:
onNext x 0..1 [onError | onComplete]
The distinction between Flux and Mono allows us to not only introduce additional meaning to the method signatures, but also enables more efficient internal implementation of Mono due to skipping redundant buffers and costly synchronizations.
Mono<T> may be useful in cases when an application API returns one element at most. Consequently, it can easily replace CompletableFuture<T>, giving a pretty similar semantic. Of course, these two types have some small semantic differences—CompletableFuture, unlike Mono, cannot complete normally without emitting a value. Also, CompletableFuture starts processing immediately, while Mono does nothing until a subscriber appears. The benefit of the Mono type lies in providing plenty of reactive operators and the ability to be flawlessly incorporated into a bigger reactive workflow.
Also, Mono can be used when it is required to notify a client about a finished action. In such cases, we may return the Mono<Void> type and signal onComplete() when processing is done or onError() in the event of failure. In such a scenario, we don't return any data but signal a notification, which in turn may be used as a trigger for further computation.
Mono and Flux are not detached types and can easily be "transformed" into each other. For example, Flux<T>.collectList() returns Mono<List<T>> and Mono<T>.flux() returns Flux<T>. In addition, the library is smart enough to optimize some transformations that do not change the semantic. For example, let's consider the following transformation (Mono -> Flux -> Mono):
Mono.from(Flux.from(mono))
When calling the preceding code, it returns the original mono instance, as this is conceptually a no-ops conversion.
- Cisco OSPF命令與配置手冊
- 連接未來:從古登堡到谷歌的網(wǎng)絡(luò)革命
- 無人機(jī)通信
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- 網(wǎng)絡(luò)環(huán)境中基于用戶視角的信息質(zhì)量評價研究
- Scala Design Patterns.
- Learning Windows 8 Game Development
- 物聯(lián)網(wǎng)工程概論
- 從實(shí)踐中學(xué)習(xí)手機(jī)抓包與數(shù)據(jù)分析
- 移動物聯(lián)網(wǎng):商業(yè)模式+案例分析+應(yīng)用實(shí)戰(zhàn)
- 園區(qū)網(wǎng)絡(luò)架構(gòu)與技術(shù)
- 云計算技術(shù)與標(biāo)準(zhǔn)化
- Hands-On Docker for Microservices with Python
- 移動互聯(lián)網(wǎng)環(huán)境下的核心網(wǎng)剖析及演進(jìn)