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

Vert.x 3 - Voxxed Days Belgrade

Vert.x 3 - Voxxed Days Belgrade

It's the latest and more improved presentation of vert.x 3

Bartek Zdanowski

October 02, 2015
Tweet

More Decks by Bartek Zdanowski

Other Decks in Programming

Transcript

  1. vert.x 5 Invented by Tim Fox as Node.x 24 June

    2015 - released v3 still it’s very hot :D
  2. the problem number of mobile users raises 2,1 bln in

    2012 -> 7 bln in 2018 Internet of things - 30 bln in 2020! IPv4 is exhausted IPv6 is already ready for all of them 9
  3. the problem traditional synchronous approach ▪ one thread handles one

    task ▪ thread is waiting for job to finish ▪ whole queue of tasks waits 10
  4. the problem traditional synchronous approach ▪ synchronous code ▪ scalability

    problem ▪ lots of threads ▪ concurrency that no-one understands ;) 11
  5. the problem 12 Tomcat: 200 threads = 200 connections rest

    of incoming connections must wait*… *okay, now you can do nonblocking in Tomcat https://tomcat.apache.org/tomcat-7.0-doc/aio.html
  6. the problem big thread pools are evil synchronous code is

    even more evil concurrency is very hard synchronous == blocking 15
  7. the solution 18 1 task = series of loosely coupled

    events event that informs that new job/data is waiting
  8. the solution 19 program should release thread instead of waiting

    for operation (I/O) to be finished use non-blocking IO (NIO)
  9. 26 public class HelloVerticle extends AbstractVerticle { @Override public void

    start() { LoggerFactory.getLogger(getClass()).info( "Hello vert.x"); } }
  10. 27 public static final String CONSUMER_ADDRESS = "consumer.address"; public void

    start() { vertx.eventBus().consumer(CONSUMER_ADDRESS, new Handler<Message<String>>() { public void handle(Message<String> message) { logger.info("Got message:" + message.body()); } }); } //Java < 8
  11. 28 public static final String CONSUMER_ADDRESS = "consumer.address"; public void

    start() { vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> { logger.info("Got message: " + message.body()); }); } //Java 8 Lambdas FTW!
  12. threadsafe only when one thread! 31 class MyService { public

    synchronized Result doSomething(Data data) { //do some critical stuff } }
  13. 34 basic deployment unit actor-like model (akka similarity) always run

    in the same thread classloader isolated can have many instances verticle
  14. verticle threadsafe code ▪ verticle instance - always the same

    thread ▪ separated classloaders for each verticle instance 35
  15. 46 public void start() { getLogger().info("Broadcaster started"); vertx.setPeriodic(PERIOD_MS, timerID ->

    { getLogger().info("Broadcasting message " + counter); vertx.eventBus().publish(Consumer.CONSUMER_ADDRESS, "Message " + counter); counter++; }); } Broadcasting a message
  16. 47 public void start() { getLogger().info("Broadcaster started"); vertx.setPeriodic(PERIOD_MS, timerID ->

    { getLogger().info("Broadcasting message " + counter); vertx.eventBus().send(Consumer.CONSUMER_ADDRESS, "Message " + counter); counter++; }); } Sending a message p2p
  17. 48 public void start() { getLogger().info("Consumer started! " + hashCode());

    vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> { getLogger().info("Received message: " + message.body()); }); } Consuming a message
  18. public void start() { getLogger().info("Consumer started! " + hashCode()); vertx.eventBus().consumer(CONSUMER_ADDRESS,

    message -> { getLogger().info("Received message: " + message.body()); message.reply("I got your message"); }); } 49 Consuming a message + replying
  19. 51 lightweight vert.x core extensions ▪ web ▪ data access

    (mongoDB, redis, JDBC) ▪ security (basic auth, jdbc auth, jwt, shiro) ▪ reactive (based on RxJava) ▪ others vert.x ecosystem
  20. 53 web server ▪ vert.x core - very raw and

    basic HTTP server ▪ web server ext - neat path and content routing
  21. 54 vertx.createHttpServer().requestHandler( req -> { LOG.info(String.format("Got request [%s]", req.path())); switch

    (req.path()) { case "/" : req.response().end("Ok. Here's root"); break; case "/other": req.response().end("Other things..."); break; default: req.response().setStatusCode(404).end("Unknown resource!"); } }).listen(port) vert.x core - very raw and basic HTTP server
  22. 55 Router router = new RouterImpl(vertx); router.get("/users/:uid").handler( ctx -> {

    String id = ctx.request().getParam("uid"); JsonObject user = new JsonObject().put("id", id).put("name", "bartek"); ctx.response().end(user.encode()); }); HttpServer server = vertx.createHttpServer(); server.requestHandler(router::accept).listen(8080); web server ext - neat path and content routing
  23. verticles: ▪ are actor-like ▪ can be addressed ▪ can

    subscribe to many addresses ▪ can do processing jobs ▪ can send back content ▪ can have multiple instances of same class 60 vert.x vs akka
  24. vertically ▪ verticle can have multiple instances ▪ each instance

    can have own processor core horizontally ▪ clustering - lot’s of nodes ▪ eventbus reaches all nodes 62 how vert.x scales
  25. 65 puts "Ruby for the win!" eb = $vertx.event_bus() #

    Send a message every second $vertx.set_periodic(1000) { |v| puts "RUBY: sending Ruby message" eb.publish("producer.address", "Ruby shines!") } Ruby shines
  26. 66 var eb = vertx.eventBus(); eb.consumer("producer.address", function (message) { console.log("JAVASCRIPT:

    Received a message: " + message.body()); }); console.log("Receiver ready!"); For JavaScript dare devils!
  27. ▪ effective use of servers’ resources ▪ parallel computing ▪

    great as a backend for lots of (mobile) clients ▪ nice integration platform ▪ could be awesome in microservices 68 usecases
  28. I’m not the owner of the pictures used. Just found

    them on: http://www.rantchic.com/wp-content/uploads/2013/12/Apollo-13.jpg http://www.gamesaktuell.de/screenshots/1280x1024/2009/04/terminator_2_blu_ray03.jpg http://tripoutlook.com/wp-content/uploads/2013/03/Car-parking.jpg http://upload.wikimedia.org/wikipedia/commons/5/52/Parallel_Parking_cars.jpg http://www.foreverbarcelona.com/wp-content/uploads/2014/02/Taxi-Tips.png http://www.125p.eu/wp-content/uploads/2009/03/fiat-125p-zmiennicy-5.jpg http://www.novosti.rs/upload/images/2011/07/2107/bg-taksi.jpg http://i.livescience.com/images/i/000/024/292/iFF/neurons-120208.jpg?1328727600 73 All the pics used