inter reactive kotlin applications
play

Inter-Reactive Kotlin Applications Julien Viet @julienviet Julien - PowerPoint PPT Presentation

Inter-Reactive Kotlin Applications Julien Viet @julienviet Julien Viet Open source developer for 15+ years Current @vertx_project lead Principal software engineer at Marseille Java User Group Leader https://www.julienviet.com/


  1. Inter-Reactive Kotlin Applications Julien Viet @julienviet

  2. Julien Viet Open source developer for 15+ years Current @vertx_project lead Principal software engineer at Marseille Java User Group Leader � https://www.julienviet.com/ � http://github.com/vietj � @julienviet

  3. Outline ✓ Reactive applications ✓ Going event driven ✓ Going interactive with coroutines ✓ Streaming with channels ✓ Coroutines vs RxJava

  4. Application

  5. Software Availability Metrics Messages Requests

  6. Reactive Manifesto, Actor, Messages “Responding to stimuli” Data flow Resilience, Elasticity, Scalability, Events, Observable Asynchronous, non-blocking Spreadsheets Reactive Reactive Reactive systems streams programming Data flow Back-pressure Non-blocking Akka, Vert.x Akka Streams, Rx v2, Reactor, Reactive Spring, Reactor, Vert.x RxJava, Vert.x

  7. Eclipse Vert.x Open source project started in 2012 Eclipse / Apache licensing A toolkit for building reactive applications for the JVM 7K ⋆ on � Built on top of � https://vertx.io � @ vertx_project

  8. Going event driven

  9. while (isRunning) { val line = bufferedReader.readLine() when (line) { "ECHO" �-? bufferedWriter.write(line) �/0 ��../ �/0 Other cases ( ��../ ) �/0 ��../ else �-? bufferedWriter.write( "Unknown command" ) } }

  10. x 1000 = �

  11. “When you have a line of text, call C2” Something else with C1 C2 no blocking call either

  12. Events Event Loop Thread

  13.  2 event-loops per CPU core by default

  14. Movie rating application router { get( "/movie/:id" ) { ctx �-? getMovie(ctx) } post( "/rate/:id" ) { ctx �-? rateMovie(ctx) } get( "/rating/:id" ) { ctx �-? getRating(ctx) } }

  15. Movie rating application router { get( "/movie/:id" ) { ctx �-? getMovie(ctx) } post( "/rate/:id" ) { ctx �-? rateMovie(ctx) } get( "/rating/:id" ) { ctx �-? getRating(ctx) } }

  16. fun getMovie(ctx: RoutingContext) { val id = ctx.pathParam( "id" ) val params = json { array(id) } client.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , params) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { ctx.response().end(json { obj( "id" to id, "title" to result.rows[0][ "TITLE" ]).encode() } ) } else { ctx.response().setStatusCode(404).end() } } else { ctx.fail( it .cause()) } } }

  17. fun getMovie(ctx: RoutingContext) { val id = ctx.pathParam( "id" ) val params = json { array(id) } client.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , params) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { ctx.response().end(json { obj( "id" to id, "title" to result.rows[0][ "TITLE" ]).encode() } ) } else { ctx.response().setStatusCode(404).end() } } else { ctx.fail( it .cause()) } } }

  18. fun getMovie(ctx: RoutingContext) { val id = ctx.pathParam( "id" ) val params = json { array(id) } client.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , params) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { ctx.response().end(json { obj( "id" to id, "title" to result.rows[0][ "TITLE" ]).encode() } ) } else { ctx.response().setStatusCode(404).end() } } else { ctx.fail( it .cause()) } } }

  19. fun getMovie(ctx: RoutingContext) { val id = ctx.pathParam( "id" ) val params = json { array(id) } client.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , params) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { ctx.response().end(json { obj( "id" to id, "title" to result.rows[0][ "TITLE" ]).encode() } ) } else { ctx.response().setStatusCode(404).end() } } else { ctx.fail( it .cause()) } } }

  20. Movie rating application router { get( "/movie/:id" ) { ctx �-? getMovie(ctx) } post( "/rate/:id" ) { ctx �-? rateMovie(ctx) } get( "/rating/:id" ) { ctx �-? getRating(ctx) } }

  21. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

  22. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

  23. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

  24. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

  25. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

  26. val movie = ctx.pathParam( "id" ) val rating = Integer.parseInt(ctx.queryParam( "getRating" )[0]) client.getConnection { if ( it .succeeded()) { val connection = it .result() val queryParams = json { array(movie) } connection.queryWithParams( "SELECT TITLE FROM MOVIE WHERE ID=?" , queryParams) { if ( it .succeeded()) { val result = it .result() if (result.rows. size �=> 1) { val updateParams = json { array(rating, movie) } connection.updateWithParams( "INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?" , updateParams) { if ( it .succeeded()) { ctx.response().setStatusCode(201).end() } else { connection.close() ctx.fail( it .cause()) } } } else { connection.close() ctx.response().setStatusCode(404).end() } } else { connection.close() ctx.fail( it .cause()) } } } else { ctx.fail( it .cause()) } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend