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

RxJava + Vert.x + jOOλ で Microservice的な何かを作ってみた

RxJava + Vert.x + jOOλ で Microservice的な何かを作ってみた

Hideyuki Takeuchi

April 28, 2016
Tweet

More Decks by Hideyuki Takeuchi

Other Decks in Programming

Transcript

  1. νϟʔτ༻JSON͕ฦΔ·ͰͷྲྀΕ 1. Media API ͔ΒνϟʔτͷૉΛऔಘ͢Δ 2. νϟʔτͷૉͰࢦఆ͞ΕͨσʔλΛ
 Companies API ΍

    Stats APIʹ
 ෳ਺ճϦΫΤετΛ౤͛ͯऔಘ͢Δ 3. ݁ՌΛ·ͱΊͯJSONʹͯ͠ϒϥ΢βʹฦ͢
  2. Vert.x + Vert.x-Web • ϊϯϒϩοΩϯάI/OͳߴϨϕϧϑϨʔϜϫʔΫ • γϯάϧεϨουͰେྔͷίωΫγϣϯΛ͞͹͚Δ • શ͕ͯίʔϧόοΫͰ݁Ռ͕ฦΔඇಉظAPI •

    Node.js + Express తͳଘࡏ (ͱ͍͏͔·Μ·ͦΕ) • ϚϧνεϨουͰ΋ಈ͘
 Verticleͱ͍͏֓೦ɺEventBusͱ͍͏֓೦
  3. RxJavaΛ࢖Θͳ͔ͬͨ৔߹(Πϝʔδ) client.getConnection(result1 -> { SQLConnection connection = result1.result(); connection.setAutoCommit(false, result2

    -> { connection.queryWithParams("SELECT ...", params3, result3 -> { connection.updateWithParams("UPDATE ...", params4, result4 -> { connection.commit(result5 -> { connection.close(result6 -> { successCallback(); }); }); }); }); }); }); ΤϥʔॲཧΛೖΕΔͱ΋ͬͱͻͲ͍͜ͱʹɻɻɻ
  4. RxJavaΛ࢖ͬͨ৔߹(Πϝʔδ) client.getConnectionObservable().flatMap(connection -> { return connection.setAutoCommitObservable(false) .flatMap(result -> connection.queryWithParamsObservable("SELECT ...",

    params1)) .flatMap(result -> connection.updateWithParamsObservable("UPDATE ...", params2))) .flatMap(result -> connection.commitObservable()) .flatMap(result -> connection.closeObservable()); }); Τϥʔॲཧ͸subscribeͰҰճॻ͚ͩ͘ʂ
  5. ࣮ࡍͷྑ͋ͬͨ͘ίʔυ(งғؾ) observableA // Media APIからチャートの素を取得 .flatMap(responseA -> { .. responseAからobservableB,

    C, D (企業B, C, D)を生成 .. return Observable.zip( observableB.map(Optional::of).onErrorReturn(e -> Optional.empty()), observableC.map(Optional::of).onErrorReturn(e -> Optional.empty()), observableD.map(Optional::of).onErrorReturn(e -> Optional.empty()), (optionalResponseB, optionalResponseC, optionalResponseD) -> { .. responseAとoptionalResponseB, C, DからresponseZを生成 .. return responseZ; // チャートのモデル }); });