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

WebFluxでコルーチン #m3kt

WebFluxでコルーチン #m3kt

どこでもKotlin #4 ( https://m3-engineer.connpass.com/event/70561/ )で発表したスライドです。

Taro Nagasawa

November 27, 2017
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  2. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  3. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  4. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  5. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  6. 従来のスタイル @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    String { val itemA = demoService.getItemA() val itemB = demoService.getItemB() val itemC = demoService.getItemC(itemA, itemB) return itemC.answer } }
  7. WebFlux @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  8. WebFlux @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  9. @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle(): Mono<String>

    = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } WebFlux // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  10. @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle(): Mono<String>

    = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } WebFlux // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  11. @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle(): Mono<String>

    = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } WebFlux // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  12. @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle(): Mono<String>

    = Mono.zip( demoService.getMonoA(), demoService.getMonoB() ) .flatMap { demoService.getMonoC(it.t1, it.t2) } .map { it.answer } } WebFlux // dependencies compile('org.springframework.boot:spring-boot-starter-webflux')
  13. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'
  14. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'
  15. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'
  16. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'
  17. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'
  18. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  19. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  20. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  21. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  22. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  23. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  24. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  25. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  26. Channelで遊ぶ @RestController class DemoController { private val ch = Channel<String>()

    @GetMapping("read") fun read() = publish { while(isActive) send(ch.receive()) } @GetMapping("write/{msg}") fun write(@PathVariable msg: String) = mono { ch.send(msg) msg } }
  27. ハンドラがサスペンド @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") suspend fun

    handle():String { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } return c.await().answer } } // dependencies compile 'org.springframework.kotlin:spring-webflux-kotlin-coroutine:0.3.1'
  28. ハンドラがサスペンド @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") suspend fun

    handle():String { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } return c.await().answer } } // dependencies compile 'org.springframework.kotlin:spring-webflux-kotlin-coroutine:0.3.1'
  29. Reactorでコルーチン @RestController class DemoController(val demoService: DemoService) { @GetMapping("demo") fun handle():

    Mono<String> = mono { val a = async { demoService.getItemA() } val b = async { demoService.getItemB() } val c = async { demoService.getItemC(a.await(), b.await()) } c.await().answer } } // dependencies compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor:0.19.3'