1. Overview
1.概述
In this tutorial, we’ll learn the difference between Flux and Mono of the Reactor Core library.
在本教程中,我们将学习Flux和Mono的Reactor Core库之间的区别。
2. What Is Mono?
2.什么是Mono?
Mono is a special type of Publisher. A Mono object represents a single or empty value. This means it can emit only one value at most for the onNext() request and then terminates with the onComplete() signal. In case of failure, it only emits a single onError() signal.
Mono是Publisher的一个特殊类型。一个Mono对象代表一个单一或空的值。这意味着它最多只能为onNext()请求发出一个值,然后以onComplete()信号终止。在失败的情况下,它只发出一个onError()信号。
Let’s see an example of Mono with a completion signal:
让我们看看一个带有完成信号的Mono的例子。
@Test
public void givenMonoPublisher_whenSubscribeThenReturnSingleValue() {
Mono<String> helloMono = Mono.just("Hello");
StepVerifier.create(helloMono)
.expectNext("Hello")
.expectComplete()
.verify();
}
We can see here that when helloMono is subscribed, it emits only one value and then sends the signal of completion.
我们在这里可以看到,当helloMono被订阅时,它只发出一个值,然后发送完成信号。
3. What Is Flux?
3.什么是Flux?
Flux is a standard Publisher that represents 0 to N asynchronous sequence values. This means that it can emit 0 to many values, possibly infinite values for onNext() requests, and then terminates with either a completion or an error signal.
Flux是一个标准的Publisher,表示0到N个异步序列值。这意味着它可以发出0到许多值,对于onNext()请求来说可能是无限的值,然后以完成或错误信号终止。
Let’s see an example of Flux with a completion signal:
让我们看看一个带有完成信号的Flux的例子。
@Test
public void givenFluxPublisher_whenSubscribedThenReturnMultipleValues() {
Flux<String> stringFlux = Flux.just("Hello", "Baeldung");
StepVerifier.create(stringFlux)
.expectNext("Hello")
.expectNext("Baeldung")
.expectComplete()
.verify();
}
Now, let’s see an example of Flux with an error signal:
现在,让我们看一个带有错误信号的Flux的例子。
@Test
public void givenFluxPublisher_whenSubscribeThenReturnMultipleValuesWithError() {
Flux<String> stringFlux = Flux.just("Hello", "Baeldung", "Error")
.map(str -> {
if (str.equals("Error"))
throw new RuntimeException("Throwing Error");
return str;
});
StepVerifier.create(stringFlux)
.expectNext("Hello")
.expectNext("Baeldung")
.expectError()
.verify();
}
We can see here that after getting two values from the Flux, we get an error.
我们在这里可以看到,在从Flux获得两个值后,我们得到了一个错误。
4. Mono Vs. Flux
4.Mono与Flux的对比
Mono and Flux are both implementations of the Publisher interface. In simple terms, we can say that when we’re doing something like a computation or making a request to a database or an external service, and expecting a maximum of one result, then we should use Mono.
Mono和Flux都是Publisher接口的实现。简单地说,我们可以说,当我们在做计算或向数据库或外部服务发出请求,并且期望最多有一个结果时,我们应该使用Mono。
When we’re expecting multiple results from our computation, database, or external service call, then we should use Flux.
当我们期望从我们的计算、数据库或外部服务调用中获得多个结果时,那么我们应该使用Flux。
Mono is more relatable to the Optional class in Java since it contains 0 or 1 value, and Flux is more relatable to List since it can have N number of values.
Mono与Java中的Optional类更有关系,因为它包含0或1个值,而Flux与List更有关系,因为它可以有N多个值。
5. Conclusion
5.总结
In this article, we’ve learned the difference between Mono and Flux.
在这篇文章中,我们已经了解了Mono和Flux之间的区别。
As always, the complete source code for examples is available over on GitHub.
一如既往,实例的完整源代码可在GitHub上获得超过。