Android DevConference - Live code with Kotlin: construindo o primeiro app usando Kotlin
ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class...
Transcript of ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class...
![Page 1: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/1.jpg)
1 / 64
ndertow
![Page 2: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/2.jpg)
2 / 64
A Long Time Ago
![Page 3: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/3.jpg)
3 / 64
A Long Time Ago
![Page 4: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/4.jpg)
4 / 64
A Long Time Ago
![Page 5: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/5.jpg)
5 / 64
Undertow
![Page 6: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/6.jpg)
6 / 64
Features
● HTTP/HTTPS● HTTP/2● WebSockets
![Page 7: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/7.jpg)
7 / 64
Hello, World!
Undertow.builder() .addHttpListener(8080, "localhost") { it: HttpServerExchange it.responseSender.send("Hello") } .build() .start()
![Page 8: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/8.jpg)
8 / 64
Hello, World!
Undertow.builder() .addHttpListener(8080, "localhost") { it: HttpServerExchange it.responseSender.send("Hello") } .build() .start()
![Page 9: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/9.jpg)
9 / 64
Hello, World!
Undertow.builder() .addHttpListener(8080, "localhost") { it: HttpServerExchange it.responseSender.send("Hello") } .build() .start()
![Page 10: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/10.jpg)
10 / 64
HttpHandler
public Builder addHttpListener( int port, String host, HttpHandler rootHandler) { listeners.add(new ListenerConfig(ListenerType.HTTP, port, host, null, null, rootHandler)); return this;}
![Page 11: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/11.jpg)
11 / 64
HttpHandler
public interface HttpHandler {
/** * Handle the request. * * @param exchange the HTTP request/response exchange * */ void handleRequest(HttpServerExchange exchange) throws Exception;}
![Page 12: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/12.jpg)
12 / 64
HttpHandler
typealias HttpHandler = (exchange: HttpServerExchange) *> Unit
![Page 13: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/13.jpg)
13 / 64
Architecture
![Page 14: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/14.jpg)
14 / 64
Architecture
class BkugRootHandler : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { when (exchange.hostName) { "foo.com" *> { when (exchange.requestPath) { "/path1" *> exchange.responseSender.send("foo.com/path1") } } "bar.com" *> { when (exchange.requestPath) { "/path2" *> exchange.responseSender.send("bar.com/path2") } } } }}
![Page 15: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/15.jpg)
15 / 64
Architecture
class BkugRootHandler : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { when (exchange.hostName) { "foo.com" *> { when (exchange.requestPath) { "/path1" *> exchange.responseSender.send("foo.com/path1") } } "bar.com" *> { when (exchange.requestPath) { "/path2" *> exchange.responseSender.send("bar.com/path2") } } } }}
![Page 16: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/16.jpg)
16 / 64
Architecture
class BkugRootHandler : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { when (exchange.hostName) { "foo.com" *> { when (exchange.requestPath) { "/path1" *> exchange.responseSender.send("foo.com/path1") } } "bar.com" *> { when (exchange.requestPath) { "/path2" *> exchange.responseSender.send("bar.com/path2") } } } }}
![Page 17: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/17.jpg)
17 / 64
Architecture
fun main() { Undertow.builder() .addHttpListener(80, "0.0.0.0", BkugRootHandler()) .build() .start()}
![Page 18: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/18.jpg)
18 / 64
![Page 19: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/19.jpg)
19 / 64
Architecture - Composition
val fooHandler = RoutingHandler().get("/path1") { it.responseSender.send("foo.com/path1")}
Undertow.builder() .addHttpListener(80, "0.0.0.0", fooHandler) .build() .start()
![Page 20: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/20.jpg)
20 / 64
Architecture - Composition
val fooHandler = RoutingHandler().get("/path1") { it.responseSender.send("foo.com/path1")}
Undertow.builder() .addHttpListener(80, "0.0.0.0", fooHandler) .build() .start()
![Page 21: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/21.jpg)
21 / 64
Architecture - Composition
val fooHandler = RoutingHandler().get("/path1") { it.responseSender.send("foo.com/path1")}
Undertow.builder() .addHttpListener(80, "0.0.0.0", fooHandler) .build() .start()
![Page 22: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/22.jpg)
22 / 64
Architecture - Composition
val fooHandler = RoutingHandler() .get("/path1", pathGetHandler) .get("/path2", path2GetHandler) .post("/path2", path3PostHandler)
![Page 23: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/23.jpg)
23 / 64
Architecture - Compositionval fooHandler = RoutingHandler().get("/path1") { it.responseSender.send("foo.com/path1")}
val barHandler = RoutingHandler().post("/path2") { it.responseSender.send("bar.com/path2")}
val rootHandler = NameVirtualHostHandler() .addHost("foo.com", fooHandler) .addHost("bar.com", barHandler)
Undertow.builder() .addHttpListener(80, "0.0.0.0", rootHandler) .build() .start()
![Page 24: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/24.jpg)
24 / 64
Architecture - Compositionval fooHandler = RoutingHandler().get("/path1") { it.responseSender.send("foo.com/path1")}
val barHandler = RoutingHandler().post("/path2") { it.responseSender.send("bar.com/path2")}
val rootHandler = NameVirtualHostHandler() .addHost("foo.com", fooHandler) .addHost("bar.com", barHandler)
Undertow.builder() .addHttpListener(80, "0.0.0.0", rootHandler) .build() .start()
![Page 25: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/25.jpg)
25 / 64
Architecture - Composition
val rootHandler = NameVirtualHostHandler() .addHost("foo.com", fooHandler) .addHost("bar.com", barHandler)
class NameVirtualHostHandler : HttpHandler { val hostToHandler = mapOf<String, HttpHandler>() override fun handleRequest(exchange: HttpServerExchange) { hostToHandler[exchange.hostName] *.handleRequest(exchange) }}
![Page 26: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/26.jpg)
26 / 64
Takeaway:Applications are assembled from multiple
handler (HttpHandler) classes
![Page 27: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/27.jpg)
27 / 64
Built-in Handlers
● Path, Path Template, Virtual Host, Predicate● Resource● Websocket● Redirect● Trace● Header● Graceful Shutdown● Request Limiting Handler
![Page 28: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/28.jpg)
28 / 64
Kotlin + Undertow
● Handler Builder● Request Params● Request Headers● Response Body● Attachments
![Page 29: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/29.jpg)
29 / 64
Kotlin + Undertow: Handler Builder
class HelloHandler : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { exchange.responseSender.send("Hello!") }}
![Page 30: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/30.jpg)
30 / 64
Kotlin + Undertow: Handler Builder
inline fun handler( crossinline body: HttpServerExchange.() *> Unit): HttpHandler { return HttpHandler { exchange -> body(exchange) }}
val helloHandler = handler { responseSender.send("Hello!")}
![Page 31: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/31.jpg)
31 / 64
Kotlin + Undertow: Request Params
val db = handler { val query = queryParameters["query"]*.peekFirst()}
![Page 32: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/32.jpg)
32 / 64
Kotlin + Undertow: Request Params
val db = handler { val filter by queryParam<String?>() val page by queryParam<Int?>()}
![Page 33: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/33.jpg)
33 / 64
Kotlin + Undertow: Request Headers
val db = handler { val auth = requestHeaders.get("Authorization")*.peekFirst() */ vs val auth by header<String?>("Authorization")}
![Page 34: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/34.jpg)
34 / 64
Kotlin + Undertow: Response Body
class ShareCountHandler( private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler { override suspend fun handleRequest(exchange: HttpServerExchange) { val url by exchange.queryParam<String>() val count = shareCountAggregateService.getCount(url)
exchange.sendObj(count) }}
![Page 35: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/35.jpg)
35 / 64
Kotlin + Undertow: Response Body
class ShareCountHandler( private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler { override suspend fun handleRequest(exchange: HttpServerExchange) { val url by exchange.queryParam<String>() val count = shareCountAggregateService.getCount(url)
exchange.sendObj(count) }}
![Page 36: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/36.jpg)
36 / 64
Kotlin + Undertow: Attachments
val KEY: AttachmentKey<Any> = AttachmentKey.create(Any*:class.java)
exchange.putAttachment(KEY, "HELLO")exchange.getAttachment(KEY)exchange.removeAttachment(KEY)
![Page 37: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/37.jpg)
37 / 64
Kotlin + Undertow: Attachments
fun <T> HttpServerExchange.pull(key: AttachmentKey<T>): T? { val attachment: T? = getAttachment(key) attachment*.let { removeAttachment(key) } return attachment}
![Page 38: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/38.jpg)
38 / 64
Coroutines Support
![Page 39: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/39.jpg)
39 / 64
Request
class HandlerWithBlockingOperation : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { */ **. do long op here, */ some math or remote request possibly }}
![Page 40: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/40.jpg)
40 / 64
Request
class HandlerWithBlockingOperation : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { if (exchange.isInIoThread) { exchange.dispatch(this) }
*/ **. do long op here, */ some math or remote request possibly }}
![Page 41: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/41.jpg)
41 / 64
Request
class HandlerWithBlockingOperation : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { if (exchange.isInIoThread) { exchange.dispatch(this) }
*/ **. do long op here, */ some math or remote request possibly }}
![Page 42: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/42.jpg)
42 / 64
Request
val blocking = BlockingHandler(HandlerWithBlockingOperation())
![Page 43: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/43.jpg)
43 / 64
Request
val blocking = BlockingHandler(HandlerWithBlockingOperation())
![Page 44: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/44.jpg)
44 / 64
Threads
![Page 45: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/45.jpg)
45 / 64
CoroutinesHandler
interface CoroutinesHandler { suspend fun handleRequest(exchange: HttpServerExchange)}
![Page 46: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/46.jpg)
46 / 64
CoroutinesHandlerAdapterCoroutinesHandler → HttpHandler
/** * Bridge between thread and coroutines worlds. */class CoroutinesHandlerAdapter( private val handler: CoroutinesHandler) : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { exchange.dispatch(SameThreadExecutor.INSTANCE, Runnable { GlobalScope.launch(Dispatchers.Default) { handler.handleRequest(exchange) } }) }}
![Page 47: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/47.jpg)
47 / 64
CoroutinesHandlerAdapter/** * Bridge between thread and coroutines worlds. */class CoroutinesHandlerAdapter( private val handler: CoroutinesHandler) : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { exchange.dispatch(SameThreadExecutor.INSTANCE, Runnable { GlobalScope.launch(Dispatchers.Default) { handler.handleRequest(exchange) } }) }}
![Page 48: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/48.jpg)
48 / 64
CoroutinesHandlerAdapter/** * Bridge between thread and coroutines worlds. */class CoroutinesHandlerAdapter( private val handler: CoroutinesHandler) : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { exchange.dispatch(SameThreadExecutor.INSTANCE, Runnable { GlobalScope.launch(Dispatchers.Default) { handler.handleRequest(exchange) } }) }}
![Page 49: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/49.jpg)
49 / 64
CoroutinesHandlerAdapterval rootHandler = CoroutinesHandlerAdapter(SampleCoroutines())
Undertow.builder() .addHttpListener(80, "0.0.0.0", rootHandler) .build() .start()
![Page 50: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/50.jpg)
50 / 64
CoroutinesHandler
class ShareCountHandler( private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler { override suspend fun handleRequest(exchange: HttpServerExchange) { val url by exchange.queryParam<String>() val count = shareCountAggregateService.getCount(url)
exchange.sendObj(count) }}
![Page 51: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/51.jpg)
51 / 64
But
– Different Threads Pool
– Blocking handlers/wrapper not usable (*almost)
![Page 52: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/52.jpg)
52 / 64
Request Scope with Coroutines
class RandomContext : CoroutineContext.Element { override val key = RandomKey
val random: Random by lazy { Random() }}
object RandomKey : CoroutineContext.Key<RandomContext>
suspend fun random(): Random { return coroutineContext[RandomKey]*.random *: throw RuntimeException("Random not found.")}
![Page 53: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/53.jpg)
53 / 64
Request Scope with Coroutines
class WithRequestScope : CoroutinesHandler { override suspend fun handleRequest(exchange: HttpServerExchange) { withContext(coroutineContext + RandomContext()) { } }}
![Page 54: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/54.jpg)
54 / 64
Request Scope with Coroutines
class WithRequestScope : CoroutinesHandler { override suspend fun handleRequest(exchange: HttpServerExchange) { withContext(coroutineContext + RandomContext()) { random().nextInt() } }}
![Page 55: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/55.jpg)
55 / 64
Undertow VS ...
● Ktor● Spring● Spark-java● vert.x, jetty, tomcat...
![Page 56: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/56.jpg)
56 / 64
Lightweight
● 2.2M undertow-core-2.0.17.Final.jar
● 508K xnio-api-3.3.8.Final.jar
● 116K xnio-nio-3.3.8.Final.jar
● 68K jboss-logging-3.3.2.Final.jar
---------------------------------------------------
● 2.9M Total
![Page 57: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/57.jpg)
57 / 64
Lightweight
● -Xmx3m – Start● -Xmx5m – ab -c 500● Startup time < 500ms
![Page 58: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/58.jpg)
58 / 64
Lightweight
![Page 59: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/59.jpg)
59 / 64
When
● Proxy (https://github.com/IRus/kotlin-dev-proxy/)● Micro HTTP/WebSocket services● Swagger/OpenApi (https://github.com/networknt/light-4j)● File Server
![Page 60: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/60.jpg)
60 / 64
Also
● Undertow/JS ● Servlet 4.0
![Page 61: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/61.jpg)
61 / 64
Photo Credits
● http://vectorply.com/reinforcement-fibers/● https://www.flickr.com/photos/blachswan/284456674
52●
![Page 62: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/62.jpg)
62 / 64
XNIO
![Page 63: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/63.jpg)
63 / 64
XNIO
![Page 64: ndertow - Kotlin Belarus · 2019. 1. 30. · Kotlin + Undertow: Response Body class ShareCountHandler(private val shareCountAggregateService: ShareCountAggregateService) : CoroutinesHandler](https://reader035.fdocuments.in/reader035/viewer/2022071406/60fb822d61c7a065c1292331/html5/thumbnails/64.jpg)
64 / 64
XNIO