翻譯自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/OthersOperations
Other Operations
Description
In this section, we'll have a look at a few more useful operators that don't
fall into the broad categories we explored earlier. Reactor 3 contains a lot
of operators, so don't hesitate to have a look at the
Flux
and Mono
javadocs as well as the reference guide
to learn about more of them.
在本節(jié)中投慈,我們將了解一些更有用的運(yùn)算符尉辑,這些運(yùn)算符不屬于我們前面探討的大類(lèi)別铡俐。
Reactor 3包含很多運(yùn)算符赠幕,所以請(qǐng)毫不猶豫地查看Flux和Mono javadocs以及參考指南蚀之,以了解更多袱院。
Practice
In the first exercise we'll receive 3 Flux<String>
. Their elements could
arrive with latency, yet each time the three sequences have all emitted
an element, we want to combine these 3 elements and create a new User
.
This concatenate-and-transform operation is called zip
:
在第一個(gè)練習(xí)中嘁字,我們將收到3個(gè)Flux<String>蒿往。它們的元素可能會(huì)延遲到達(dá),但每次這三個(gè)序列
都發(fā)出一個(gè)元素棕诵,我們希望將這三個(gè)元素結(jié)合起來(lái)裁良,創(chuàng)建一個(gè)新用戶。這種連接和轉(zhuǎn)換操作稱為zip:
// Create a Flux of user from Flux of username, firstname and lastname.
Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
return Flux.zip(usernameFlux, firstnameFlux, lastnameFlux).map(tuples->new User(tuples.getT1(), tuples.getT2(), tuples.getT3()));
}
If you have 3 possible Mono sources and you only want to keep the one that
emits its value the fastest, you can use the firstWithValue
static method:
如果您有3個(gè)可能的Mono源校套,并且只想保持發(fā)射其值最快的一個(gè)价脾,那么可以使用firstWithValue靜態(tài)方法:
// Return the mono which returns its value faster
Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
return Mono.firstWithValue(mono1, mono2);
}
Flux
also has the firstWithValue
static method. Only the first element
emitted by each Flux
is considered to select the fastest Flux (which is
then mirrored in the output):
Flux也有firstWithValue靜態(tài)方法。只考慮每個(gè)通量發(fā)射的第一個(gè)元素來(lái)選擇最快的通量(然后在輸出中鏡像):
// Return the flux which returns the first value faster
Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
return Flux.firstWithValue(flux1, flux2);
}
Sometimes you're not interested in elements of a Flux<T>
. If you want to
still keep a Flux<T>
type, you can use ignoreElements()
. But if you really
just want the completion, represented as a Mono<Void>
, you can use then()
instead:
有時(shí)你對(duì)Flux<T>的元素不感興趣笛匙。如果仍要保持Flux<T>類(lèi)型侨把,可以使用ignoreElements()。
但是妹孙,如果您真的只想完成秋柄,用Mono<Void>表示,您可以使用then():
// Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
Mono<Void> fluxCompletion(Flux<User> flux) {
return flux.then();
}
Reactive Streams does not allow null values in onNext
. There's an operator
that allow to just emit one value, unless it is null in which case it will
revert to an empty Mono
. Can you find it?
反應(yīng)流不允許onNext中出現(xiàn)空值蠢正。有一個(gè)運(yùn)算符只允許發(fā)出一個(gè)值骇笔,除非它為null,在這種情況下嚣崭,
它將恢復(fù)為空的Mono笨触。你能找到它嗎?
// Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
Mono<User> nullAwareUserToMono(User user) {
return Mono.justOrEmpty(user);
}
Similarly, if you want to prevent the empty Mono
case by falling back to a
different one, you can find an operator that does this switch:
類(lèi)似地雹舀,如果您想通過(guò)返回另一個(gè)Mono來(lái)防止出現(xiàn)空Mono情況芦劣,您可以找到一個(gè)執(zhí)行此switch的運(yùn)算符:
// Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty
Mono<User> emptyToSkyler(Mono<User> mono) {
//這兩個(gè)api都可以實(shí)現(xiàn)
return mono.defaultIfEmpty(User.SKYLER);
//return mono.switchIfEmpty(Mono.just(User.SKYLER));
}
Sometimes you want to capture all values emitted by Flux
into separate List
.
In this case you can use collectList
operator that would return Mono
containing that List
.
有時(shí),您希望將Flux發(fā)出的所有值捕獲到單獨(dú)的List中说榆。在這種情況下虚吟,
您可以使用collectList運(yùn)算符返回包含該List的Mono。
// Convert the input Flux<User> to a Mono<List<User>> containing list of collected flux values
Mono<List<User>> fluxCollection(Flux<User> flux) {
return flux.collectList();
}
There are more operators belonging to the collect family. You can check them
out in Flux
documentation.
有更多的運(yùn)算符屬于collect系列签财。您可以在Flux文檔中查看它們稍味。