Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
WebFluxでコルーチン #m3kt
Search
Taro Nagasawa
November 27, 2017
Programming
2
840
WebFluxでコルーチン #m3kt
どこでもKotlin #4 (
https://m3-engineer.connpass.com/event/70561/
)で発表したスライドです。
Taro Nagasawa
November 27, 2017
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
370
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
920
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
3.8k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
430
Other Decks in Programming
See All in Programming
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
as(型アサーション)を書く前にできること
marokanatani
10
2.8k
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Jakarta EE meets AI
ivargrimstad
0
220
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1.1k
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Macとオーディオ再生 2024/11/02
yusukeito
0
380
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Pragmatic Product Professional
lauravandoore
31
6.3k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Raft: Consensus for Rubyists
vanstee
136
6.6k
The Cult of Friendly URLs
andyhume
78
6k
What's new in Ruby 2.0
geeforr
343
31k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
A designer walks into a library…
pauljervisheath
204
24k
Transcript
WebFluxでコルーチン 2017-11-22 どこでもKotlin #4 長澤太郎 @ngsw_taro
自己紹介 • 長澤 太郎(たろーって呼んでね) • @ngsw_taro • エムスリー株式会社 • ディズニーが大好き!
従来のスタイル @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 } }
従来のスタイル @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 } }
従来のスタイル @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 } }
従来のスタイル @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 } }
従来のスタイル @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 } }
従来のスタイル @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 } }
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')
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')
@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')
@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')
@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')
@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')
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'
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'
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'
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'
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'
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 } }
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 } }
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 } }
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 } }
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 } }
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 } }
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 } }
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 } }
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 } }
None
None
ハンドラがサスペンド @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'
ハンドラがサスペンド @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'
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'
まとめ • WebFluxはノンブロッキングでいいね! • ReactorはFutureやストリームみたいなやつを簡単に扱うこと ができるよ! • コルーチンは素直に非同期処理を書き下せていいね!