Reactor3 Core 发布 订阅

1 Reactor3

1.1 Terms Involved

  1. Publisher(发布者)、Subscriber(订阅者)、Subscription(订阅 n.)、subscribe(订阅 v.)。
  2. event/signal(事件/信号)。
  3. sequence/stream(序列/流)。
  4. element/item(序列中的元素)。
  5. emit/produce/generate(发出/产生/生成,后两个多翻译为“生成”)、consume(消费)。
  6. Processor(未做翻译,保留英文)。
  7. operator(操作符,声明式的可组装的响应式方法,其组装成的链译作“操作链”)。

1.2 Interface

  1. Publisher(发布者)
  2. Subscriber(订阅者)
  3. Subscription(订阅关系)
  4. 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)