翻譯自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/ReactiveToBlocking
Reactive to Blocking
Sometimes you can only migrate part of your code to be reactive, and you need
to reuse reactive sequences in more imperative code.
有時,您只能將部分代碼遷移為反應式代碼,并且需要在更重要的代碼中重用反應式序列。
Thus if you need to block until the value from a Mono
is available, use
Mono#block()
method. It will throw an Exception
if the onError
event
is triggered.
因此喻鳄,如果需要阻塞圈澈,直到Mono的值可用顶考,請使用Mono#block()方法展箱。如果觸發(fā)onError事件羊赵,它將引發(fā)異常舷蒲。
Note that you should avoid this by favoring having reactive code end-to-end,
as much as possible. You MUST avoid this at all cost in the middle of other
reactive code, as this has the potential to lock your whole reactive pipeline.
注意耸袜,您應該盡可能多地使用反應式代碼來避免這種情況。您必須在其他反應性代碼中不惜一切代價避免這種情況牲平,
因為這有可能鎖定您的整個反應性管道句灌。
// Return the user contained in that Mono
User monoToValue(Mono<User> mono) {
return mono.block();
}
Similarly, you can block for the first or last value in a Flux
with
blockFirst()
/blockLast()
. You can also transform a Flux
to an Iterable
with toIterable
. Same restrictions as above still apply.
類似地,可以使用blockFirst()/blockLast()阻塞Flux中的第一個或最后一個值欠拾。
您還可以使用 toIterable 將Flux轉換為Iterable胰锌。上述限制仍然適用。
// Return the users contained in that Flux
Iterable<User> fluxToValues(Flux<User> flux) {
return flux.toIterable();
}