Java中的Mono类:什么是什么,什么时候使用? [英] Mono class in Java: what is, and when to use?
问题描述
我有以下代码:
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class GreetingHandler
public Mono<ServerResponse> hello(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello Spring!"));
}
}
除类Mono的功能及其功能之外,我了解此代码.我做了很多搜索,但并没有直截了当: Mono类是什么和何时使用它?
I understand this code except what the class Mono does and what are its features. I did a lot of search but it didn't goes straight to the point: what is the class Mono and when to use it?
推荐答案
Mono< T>
是专门的 Publisher< T>
,最多发出一项,并且然后(可选)以 onComplete
信号或 onError
信号终止.它仅提供可用于 Flux
的运算符的子集,以及一些运算符(特别是那些将 Mono
与另一个 Publisher
组合在一起的运算符)切换到 Flux
.例如, Mono#concatWith(Publisher)
返回 Flux
,而 Mono#then(Mono)
返回另一个 Mono
.请注意,可以使用 Mono
表示仅具有完成概念(类似于Runnable)的无值异步过程.要创建一个,您可以使用一个空的 Mono< Void>
.
A Mono<T>
is a specialized Publisher<T>
that emits at most one item and then (optionally) terminates with an onComplete
signal or an onError
signal.
It offers only a subset of the operators that are available for a Flux
, and some operators (notably those that combine the Mono
with another Publisher
) switch to a Flux
. For example, Mono#concatWith(Publisher)
returns a Flux
while Mono#then(Mono)
returns another Mono
.
Note that you can use a Mono
to represent no-value asynchronous processes that only have the concept of completion (similar to a Runnable). To create one, you can use an empty Mono<Void>
.
Mono和Flux都是反应性物流.它们的表达方式有所不同.Mono是0到1个元素的流,而Flux是0到N个元素的流.
Mono and Flux are both reactive streams. They differ in what they express. A Mono is a stream of 0 to 1 element, whereas a Flux is a stream of 0 to N elements.
这两个流的语义上的差异非常有用,例如,向Http服务器发出的请求期望收到0或1个响应,在这种情况下使用Flux是不合适的.相反,计算一个区间上的数学函数的结果,则期望该区间中每个数字有一个结果.在这种情况下,使用Flux是合适的.
This difference in the semantics of these two streams is very useful, as for example making a request to an Http server expects to receive 0 or 1 response, it would be inappropriate to use a Flux in this case. On the opposite, computing the result of a mathematical function on an interval expects one result per number in the interval. In this other case, using a Flux is appropriate.
如何使用它:
Mono.just("Hello World !").subscribe(
successValue -> System.out.println(successValue),
error -> System.error.println(error.getMessage()),
() -> System.out.println("Mono consumed.")
);
// This will display in the console :
// Hello World !
// Mono consumed.
// In case of error, it would have displayed :
// **the error message**
// Mono consumed.
Flux.range(1, 5).subscribe(
successValue -> System.out.println(successValue),
error -> System.error.println(error.getMessage()),
() -> System.out.println("Flux consumed.")
);
// This will display in the console :
// 1
// 2
// 3
// 4
// 5
// Flux consumed.
// Now imagine that when manipulating the values in the Flux, an exception
// is thrown for the value 4.
// The result in the console would be :
// An error as occured
// 1
// 2
// 3
//
// As you can notice, the "Flux consumed." doesn't display because the Flux
// hasn't been fully consumed. This is because the stream stop handling future values
// if an error occurs. Also, the error is handled before the successful values.
来源:反应器Java#1-如何创建Mono和Flux?, Mono,异步0-1结果
这可能会有所帮助: Mono doc
这篇关于Java中的Mono类:什么是什么,什么时候使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!