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
810
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
250
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
900
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.5k
Kotlin Contracts #m3kt
ntaro
4
3.7k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
420
Other Decks in Programming
See All in Programming
Mergeable Libraryで 高速なアプリ起動を実現しよう!
giginet
PRO
1
1.9k
Rubyのobject_id
qnighy
6
1.3k
ウォンテッドリーにおけるモバイルアプリ開発 / iOSDC Japan 2024 Sponsor Session
kubode
0
230
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.8k
LangChainの現在とv0.3にむけて
os1ma
3
700
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
210
GoのIteratorに詳しくなってしまう
inatonix
1
180
dotfiles について話したい #湘なんか
stefafafan
2
280
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
220
Prompt Cachingは本当に効果的なのか検証してみた.pdf
ttnyt8701
0
430
Regular Expressions, REXML, Automata Learning
makenowjust
0
180
暴走のウホーレン 〜想いってのはvimrcにしないと伝わらないんだぜ〜 / iosdc_japan_2024
uhooi
1
240
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
167
14k
Designing with Data
zakiwarfel
98
5k
Visualization
eitanlees
142
15k
Statistics for Hackers
jakevdp
793
220k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
22
3.9k
Building Your Own Lightsaber
phodgson
101
6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Building a Scalable Design System with Sketch
lauravandoore
458
32k
What’s in a name? Adding method to the madness
productmarketing
PRO
21
3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.4k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
109
6.9k
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やストリームみたいなやつを簡単に扱うこと ができるよ! • コルーチンは素直に非同期処理を書き下せていいね!