publisher
1 reactor3
1.1 Terms Involved
- Publisher(发布者)、subscriber(订阅者)、Subscription(订阅 n.)、subscribe(订阅 v.)。
- event/signal(事件/信号)。
- sequence/stream(序列/流)。
- element/item(序列中的元素)。
- emit/produce/generate(发出/产生/生成,后两个多翻译为“生成”)、consume(消费)。
- Processor(未做翻译,保留英文)。
- operator(操作符,声明式的可组装的响应式方法,其组装成的链译作“操作链”)。
1.2 Interface
- Publisher(发布者)
- Subscriber(订阅者)
- Subscription(订阅关系)
- Processor(Publisher和Subscriber的合体)
1.3 Code Analyze
Remember:
Nothing HAPPens Until You Subscribe
1.3.1 Code
Code:
Mono.just(“Weison”).subscribe(System.out::println);
Analyze:
Mono.just("Weison").subscribe(System.out::println);
+-------+ +------+ +-------+ +-----------------+ +------------------------+ 4.1
a b c d | Subscription +<----------|
+ + +-----+ + +------------------------+ |
| | | | | T value | +<--+
+-----------------------+ | | Subscriber actual | | |
| | | | +------------------------+ 4.2 | |
| | | | +-----+ void request(long var1)+<----------+ |
| |1 | | | | void cancel(); | |
| | | | | +------------------------+ |
| | +------v-------------------+ | |
| | | Publisher | | | |
| | +--------------------------+ | |4
+------>+ final T value | | |5 |
| +----------------v---------+ | 6 |
+--> subscribe(consumer c ) <-------------------------------------------------+ |
| ...... + | | | |
| ...... |2 | 3.1| +-------------------------------------+ |
| ...... v | 3 +------------->+ Subscriber | | |
| subscribe(Subsriber s) +-----+ 3.2| +-------------------------------------+ |
+--------------------------+ +------------->+ void onSubscribe(Subscription+var1)+---+
| | | |
+---->+ void onNext(T var1) +--------+ |
| |
| void onERROR(throwable var1) |
| |
| void onComplete() |
| |
+-------------------------------------+
1.开始订阅
2.传入lambda表达式并,调用重写subscriber(Subsriber s))方法
3.1根据lambda表达式等参数生成Subsriber
3.2调用生产Subsriber的onSubscribe方法
4.1生成Subscription,并将Publisher.value和Subscriber塞入Subscription.value和Subscription.actual
4.2调用Subscription的request方法
5.Subscription的request方法调用Subsriber的onNext方法,并传入value
6.Subsriber的onNext方法调用Subsriber的Consumer属性(lambda表达式)并进行消费
参考文献:
[ 1 ] Reactor 3 Reference Guide 英文版
[ 2 ] Reactor 3 Reference Guide 中文版
[ 3 ] Reactive-Streams Github reactive-streams-jvm
[ 4 ] Reactor Github reactor
[ 5 ] Projectreactor Docs reactor
相关阅读
一、Reactor模式是什么 反应器设计模式(Reactor pattern)是一种为处理并发服务请求,并将请求提交到一个或者多个服务处理程序的
原文:http://blog.51cto.com/liukang/2090191 Project Reactor与Spring是兄弟项目,侧重于Server端的响应式编程,主要artifact是react