Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SpringOne Platform 2016 報告会 (A Lite Rx API for the JVM)

kainoque
September 03, 2016

SpringOne Platform 2016 報告会 (A Lite Rx API for the JVM)

SpringOne Platform 2016 報告会での資料です。
Reactive Streams/Reactor のセッション "A Lite Rx API for the JVM" の紹介です。

kainoque

September 03, 2016
Tweet

More Decks by kainoque

Other Decks in Technology

Transcript

  1. A Lite Rx API for the JVM 井口 貝 @

    SmartNews, Inc. SpringOne Platform 2016 報告会
  2. 自己紹介 名前 井口 貝 (いのくち かい) @kainoque 所属   サーバサイドエンジニア @

    SmartNews, Inc. ref: SmartNews, Inc. | Team http://about.smartnews.com/ja/team/
  3. タイトル Reactor Core 3.0: A Lite Rx API for the

    JVM 内容 ・Reactive Streams の概要とその実装の一つである Reactor の紹介 ・API ハンズオン 発表者 ・Mr. Stéphane Maldini (Project Reactor lead) ・Mr. Sébastien Deleuze (Spring and Project Reactor committer) セッション概要
  4. “Reactive” 広義には、「non-blocking で event-driven なシステム」を形容する言葉 Reactive Manifesto にてその必要要件が定義されている ・Responsive (即応性)

    ・Resilent (耐障害性) ・Elastic (弾力性) ・Message-Driven (メッセージ駆動) ref: The Reactive Manifesto http://www.reactivemanifesto.org/
  5. Reactive Streams ”Reactive” なシステムを実現するための非同期ストリーム処理の標準仕様 ・non-blocking な backpressure をもつ ・Reactive Streams

    Commons が策定 ・j.u.c.Flow として Java 9 にも組み込まれる予定 ・JEP 266: More Concurrency Updates ref: JEP 266: More Concurrency Updates http://openjdk.java.net/jeps/266 ref: reactive-streams-jvm https://github.com/reactive-streams/reactive-streams-jvm
  6. ・Publisher と Subscriber の間のコミュニケーション ・Subscriber が受け取るデータの個数を、自身でコントロール可能 (backpressure) ・Publisher は Subscriber

    の要求数より多いデータを送らない ・Publisher は Subscriber から要求されるまでデータを送らない Reactive Streams
  7. 4 interfaces / 7 methods の定義 (+ TCK) ・Publisher ・Subscriber

    ・Subscription ・Processor Reactive Streams Subscriber Publisher Subscription Subscriber Publisher Subscription subscribe(this) generate onSubscribe(s) s request(1) request something onNext(data) onComplete()
  8. Reactive Streams Publisher public interface Publisher<T> { void subscribe(Subscriber<T> s);

    } Subscriber Publisher Subscription Subscriber Publisher Subscription subscribe(this) generate onSubscribe(s) s request(1) request something onNext(data) onComplete()
  9. Reactive Streams Subscriber public interface Subscriber<T> { void onSubscribe(Subscription s);

    void onNext(T t); void onError(Throwable t); void onComplete(); } Subscriber Publisher Subscription Subscriber Publisher Subscription subscribe(this) generate onSubscribe(s) s request(1) request something onNext(data) onComplete()
  10. public interface Subscription { void request(long n); void cancel(); }

    Reactive Streams Subscription Subscriber Publisher Subscription Subscriber Publisher Subscription subscribe(this) generate onSubscribe(s) s request(1) request something onNext(data) onComplete()
  11. public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {} Reactive Streams

    Processor Component A Component C Component B Subscriber of A / Publisher of C Processor<T1, T2> T1 T2
  12. Reactive Streams Subscriber Publisher Subscription Subscriber Publisher Subscription subscribe(this) generate

    onSubscribe(s) s request(1) request something onNext(data) onComplete()
  13. ・Reactive Streams 自体は直接開発者に触られることを必ずしも想定していない ・通常は Reactive Streams を実装した Reactive API Library

    を使用する ・実装 Library ・RxJava 2.0 (JVM) ・Akka-Stream (JVM) ・Reactor 3.0 (JVM) ・RxJS (JavaScript) などなど Reactive Streams
  14. Reactor Reactive Streams を実装した JVM 向け API ライブラリの一つ ・buffer, merge,

    concatenate, filter, map などの API を提供する ・RxJava の project lead である David Karnok や  Spring Framework の commiter が中心となって contribute している
  15. Reactor 特徴 ・リソースの効率性に強くフォーカスしている ・contributor の多くが共通する Spring 5 とも相性良く設計されている ・Java 8

    の各インターフェイスをベースにしている ・RxJava などに顕著な専用の Action1 などのクラスは用いていない ・デバッグ・ロギング・テストを容易に行える  ・4th Generation Reactive Library ref: Operator-fusion (Part 1) http://akarnokd.blogspot.jp/2016/03/operator-fusion-part-1.html
  16. Reactor Mono ・1 個 (または 0 個) の値のみを emit する

    Publisher 実装 ・reactor.core.publisher.Mono
  17. Scheduler ・publish / subscribe を行うスレッドのスレッドプールの factory ・Schedulers.single(), parallel(), elastic() 等で生成

    ・publish / subscribe は、デフォルトでは呼び出し側スレッドで行われる Reactor
  18. No value Single value Multiple Values Blocking void T Future<T>

    Iterable<T> Collection<T> Stream<T> Non-Blocking CompletableFuture< Void> CompletableFuture< T> CompletableFuture< List<T>> Reactive Streams Publisher<Void> Publisher<T> Publisher<T> RxJava Completable Single<T> Observable<T> Reactor Mono<Void> Mono<T> Flux<T> Reactor
  19. RxJava1Adapter RxJava 1 系 の Completable / Single / Observable

    と Reactor の Mono / Flux の相互変換アダプタ Reactor from RxJava to RxJava No value completableToMono publisherToCompletable Single value singleToMono publisherToSingle Multiple Values observableToFlux publisherToObservable
  20. Reactor and Spring 5 Spring 5 における Reactive サポートの導入に際して、 Reactor

    Core がその基礎となっている ・例: ・Flux/Mono を受け取り、返却可能な Controller の導入 ・Flux/Mono を返却可能な HTTP Client 実装の導入 ref: Reactive Programming with Spring 5.0 M1 https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
  21. Spring Initializr で手軽に試すことができる Dependencies から Reactive Web を選択 (Spring Boot

    のバージョンは 1.4.1 (SNAPSHOT)) Reactor and Spring 5 ref: SPRING INITIALIZR bootstrap your application now https://start.spring.io/
  22. Reactor の各 API を理解するためのハンズオン Part 1 から 9 まで、各 API

    を利用した JUnit テストが書かれている それをグリーンにしながら進めていく Reactor API Hands-on ref: Lite Rx API Hands-on https://github.com/reactor/lite-rx-api-hands-on Part 1 Flux の作成 Part 2 Mono の作成 Part 3 値の変換 Part 4 Flux のマージ Part 5 リクエスト Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  23. Part 1 Flux の作成 Part 2 Mono の作成 Part 3

    値の変換 Part 4 Flux のマージ Part 5 リクエスト Reactor API Hands-on Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  24. Part 1 Flux の作成 Part 2 Mono の作成 Part 3

    値の変換 Part 4 Flux のマージ Part 5 リクエスト Reactor API Hands-on Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  25. Part 1 Flux の作成 Part 2 Mono の作成 Part 3

    値の変換 Part 4 Flux のマージ Part 5 リクエスト Reactor API Hands-on Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  26. Part 1 Flux の作成 Part 2 Mono の作成 Part 3

    値の変換 Part 4 Flux のマージ Part 5 リクエスト Reactor API Hands-on Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  27. Part 1 Flux の作成 Part 2 Mono の作成 Part 3

    値の変換 Part 4 Flux のマージ Part 5 リクエスト Reactor API Hands-on Part 6 その他の操作 Part 7 Reactive -> Blocking 処理の変換 Part 8 RxJava との相互運用 Part 9 Blocking -> Reactive 処理の変換
  28. おわりに ・”Reactive” なシステムを実現するための標準仕様として Reactive Streams が存在する ・Reactive なシステムを実現するライブラリとして Reactor という選択肢がある

    ・Non-blocking + backpressure を備えリソース効率性が高い Reactive API Library ・Java 8 / Spring 5 と統合されている ・Mono / Flux により操作を行う ・依然として銀の弾丸ではない