Java 响应式编程系列---Reactor Operator API 总结


尽管 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 链时,仅用于描述异步处理的抽象过程,默认情况下数据不会开始处理。只有通过订阅,将 PublisherSubscriber 绑定在一起时,才能触发整个链中的数据流处理。这是由其内部实现方式决定的: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 most n 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 avoid request(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 流

mono_create

中间操作

Each operator adds behavior to a Publisher and wraps the previous step’s Publisher into a new instance. The whole chain is thus linked, such that data originates from the first Publisher and moves down the chain, transformed by each link. Eventually, a Subscriber finishes the process. Remember that nothing happens until a Subscriber subscribes to a Publisher.

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.

转换操作

mono_map

空值处理

mono_ifempty

执行操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1X4WeSt3-1612354225238)(https://qidawu.github.io/img/java/reactive-stream/reactor/mono/mono_do.png)]

异常处理

mono_error

终结操作

阻塞返回结果

mono_block

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 流

普通创建

flux_create

定时创建

flux_create_interval

合并创建

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4bv04g3Q-1612354225244)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_create_combine.png)]

编程式创建

flux_create_2

其它

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sU4wlzHz-1612354225246)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_create_switchOnNext.png)]

中间操作

转换操作

Flux 转 Mono:

flux_to_mono

map、flatMap:

flux_map

转成并行流:

flux_parallel

空值处理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VSJmxytz-1612354225248)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_ifempty.png)]

排序

flux_sort

去重

flux_distinct

分组

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6ZwMSDpl-1612354225251)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_window.png)]

合并

flux_concat

flux_merge

压缩

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6hTWhGBK-1612354225254)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_zip.png)]

获取元素

例如用于获取指定个数。

flux_take

延迟处理

flux_delay

缓冲

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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

异常处理

flux_error

执行操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b0SBmAMG-1612354225261)(https://qidawu.github.io/img/java/reactive-stream/reactor/flux/flux_do.png)]

终结操作

阻塞返回结果

flux_block

参考

Reactor 框架,实现 Reactive Streams 规范,并扩展大量特性

“which operator do I need?” appendix

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dylan、

耕码不易,白嫖可耻

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值