文章目录
尽管 Reactive Streams 规范并未指定任何运算符(Operators),但 Reactor 的核心价值之一就是提供了丰富的运算符。从简单的转换、过滤到复杂的编排和错误处理,涉及方方面面。
推荐通过参考文档而不是 JavaDoc 来学习 Mono/Flux API 和 Operator 操作符。参考:“which operator do I need?” appendix
注意点
使用 Reactor API 时,有以下几个注意点:
一、每个 Operator API 都会返回新实例
在 Reactor 中,操作符(Operators)好比流水线中的工作站。 每个操作符都会向
Publisher
添加行为,并将上一步的Publisher
包装成新实例。因而,整个链就被串起来,数据从第一个Publisher
开始,沿着链向下移动,通过每个链接进行转换。最终,Subscriber
结束该流程。 注意,直到Subscriber
订阅Publisher
为止,什么都不会发生。理解操作符会创建新实例这一行为,有助于避免一些常见错误,详见:I Used an Operator on my Flux but it Doesn’t Seem to Apply. What Gives?
二、Nothing Happens Until You subscribe()
Reactor 中,当您编写
Publisher
链时,仅用于描述异步处理的抽象过程,默认情况下数据不会开始处理。只有通过订阅,将Publisher
与Subscriber
绑定在一起时,才能触发整个链中的数据流处理。这是由其内部实现方式决定的:Subscriber
发出请求信号并向上传播,直到源Publisher
。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P8VcnBbf-1612354225230)(https://qidawu.github.io/img/java/reactive-stream/reactive-stream/org.reactivestream_api.png)]
三、使用背压(Backpressure)进行流量控制
Propagating signals upstream is also used to implement backpressure, which we described in the assembly line analogy as a feedback signal sent up the line when a workstation processes more slowly than an upstream workstation.
The real mechanism defined by the Reactive Streams specification is pretty close to the analogy: A subscriber can work in unbounded mode and let the source push all the data at its fastest achievable rate or it can use the
request
mechanism to signal the source that it is ready to process at mostn
elements.Intermediate operators can also change the request in-transit. Imagine a
buffer
operator that groups elements in batches of ten. If the subscriber requests one buffer, it is acceptable for the source to produce ten elements. Some operators also implement prefetching strategies, which avoidrequest(1)
round-trips and is beneficial if producing the elements before they are requested is not too costly.This transforms the push model into a push-pull hybrid, where the downstream can pull n elements from upstream if they are readily available. But if the elements are not ready, they get pushed by the upstream whenever they are produced.
Mono
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
for [0|1] elements
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ap3o30h9-1612354225232)(https://qidawu.github.io/img/java/reactive-stream/reactor/mono/mono.svg)]
创建 Mono 流
中间操作
Each operator adds behavior to a
Publisher
and wraps the previous step’sPublisher
into a new instance. The whole chain is thus linked, such that data originates from the firstPublisher
and moves down the chain, transformed by each link. Eventually, aSubscriber
finishes the process. Remember that nothing happens until aSubscriber
subscribes to aPublisher
.While the Reactive Streams specification does not specify operators at all, one of the best added values of reactive libraries, such as Reactor, is the rich vocabulary of operators that they provide. These cover a lot of ground, from simple transformation and filtering to complex orchestration and error handling.
转换操作
空值处理
执行操作
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1X4WeSt3-1612354225238)(https://qidawu.github.io/img/java/reactive-stream/reactor/mono/mono_do.png)]
异常处理
终结操作
阻塞返回结果
Flux
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
for [N] elements
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pkZl4Y3c-1612354225241)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux.svg)]
创建 Flux 流
普通创建
定时创建
合并创建
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4bv04g3Q-1612354225244)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_create_combine.png)]
编程式创建
其它
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sU4wlzHz-1612354225246)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_create_switchOnNext.png)]
中间操作
转换操作
Flux 转 Mono:
map、flatMap:
转成并行流:
空值处理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VSJmxytz-1612354225248)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_ifempty.png)]
排序
去重
分组
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6ZwMSDpl-1612354225251)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_window.png)]
合并
压缩
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6hTWhGBK-1612354225254)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_zip.png)]
获取元素
例如用于获取指定个数。
延迟处理
缓冲
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZkpHicwK-1612354225258)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_buffer.png)]
订阅
订阅后可以使用 Disposable
API 停止 FLux 流。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IufZWFFD-1612354225259)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_subscribe.png)]
计数
count
异常处理
执行操作
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b0SBmAMG-1612354225261)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_do.png)]
终结操作
阻塞返回结果
参考
Reactor 框架,实现 Reactive Streams 规范,并扩展大量特性