Vert.X mini-talk

3,966 views

Published on

My hastily assembled vert.x talk - AustinJUG 2/26/2013

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
3,966
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
54
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Vert.X mini-talk

  1. 1. orb@nostacktrace.com Vert.x A polyglot asynchronous application platformNorman Richards for the JVM
  2. 2. orb@nostacktrace.com Do you need Async? Conan, What is best in life? To crush their servers,Norman Richards s! them smoking before you, and to hear the lamentations of their admins! http://www.mikeperham.com/ Conan, the C10K Barbarian
  3. 3. orb@nostacktrace.com Ok, so of course that means ...Norman Richards http://bit.ly/ACrwel
  4. 4. orb@nostacktrace.com Some Async options Node.js (JavaScript) Twisted (Python) Tornado (Python) EventMachine (Ruby) Vert.x (Java) Vert.x (Javascript) Vert.x (Python)Norman Richards Vert.x (Ruby) Vert.x (Groovy)
  5. 5. orb@nostacktrace.com Key advantages of vert.x Runs on the JVM (Java 7+) Choose the best language Hybrid evented/threaded modelNorman Richards
  6. 6. orb@nostacktrace.com Evented IO One thread handles all requests Everything is an event handler Never block the thread Do work quickly + register callbacksNorman Richards Any kind of service - not just HTTP
  7. 7. orb@nostacktrace.com simple Example import org.vertx.java.core.Handler; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.platform.Verticle; public class ServerExample extends Verticle {   public void start() {     vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {       public void handle(HttpServerRequest req) {         req.response.headers().put("Content-Type", "text/html; charset=UTF-8");         req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");       }Norman Richards     }).listen(8080);   } }
  8. 8. orb@nostacktrace.com slightly bigger Example server.requestHandler(new Handler<HttpServerRequest>() { public void handle(HttpServerRequest request) {   final Buffer body = new Buffer(0);   request.dataHandler(new Handler<Buffer>() { public void handle(Buffer buffer) { body.appendBuffer(buffer); } }); request.endHandler(new SimpleHandler() { public void handle() { // The entire body has now been receivedNorman Richards log.info("The total body received was " + body.length() + " bytes"); } });   } }).listen(8080, "localhost");
  9. 9. orb@nostacktrace.com the verticle the unit of management/deployment Event Loop classloader isolated Event Handler Event Handler one thread / one event loop Event Handler Event Handler only one handler running at a timeNorman Richards VERTICLE no multi-threaded worries
  10. 10. orb@nostacktrace.com One VERTICLE PER CPU Shared Connection Load Balancer Event Loop Event Loop Event Loop Event Loop Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event HandlerNorman Richards Event Handler Event Handler Event Handler Event Handler VERTICLE VERTICLE VERTICLE VERTICLE
  11. 11. orb@nostacktrace.com One VERTICLE PER CPU PER HOST Event Loop Event Loop Event Loop Event Loop Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event HandlerNorman Richards VERTICLE VERTICLE VERTICLE VERTICLE host1 host2
  12. 12. orb@nostacktrace.com SHARED STATE Event Loop Event Loop Event Handler Event Handler per-instance “session” Event Handler Event Handler Event Handler Event Handler map or set interface Event Handler Event Handler only immutable valuesNorman Richards VERTICLE VERTICLE Shared State
  13. 13. orb@nostacktrace.com SHARED MAPs ConcurrentMap<String, Integer> map = vertx.sharedData().getMap("app.interesting-data");   map.put("some-key", 123); map.putIfAbsent("some-key", 123); map.replace("some-key", 123, 124); map.get("some-key"); map.remove("some-key"); Norman Richards
  14. 14. orb@nostacktrace.com SHARED SETS Set<String> set = vertx.sharedData().getSet("app.active-users");   set.add("alfred"); set.contains("alfred"); set.contains("remove");Norman Richards
  15. 15. orb@nostacktrace.com distributed event bus Event Loop Event Loop Event Loop Event Loop Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler VERTICLE VERTICLE VERTICLE VERTICLENorman Richards Shared State Shared State Event Bus
  16. 16. orb@nostacktrace.com The event bus ad-hoc addressing pubsub or p2p messaging not persistent JSON-based messagingNorman Richards
  17. 17. orb@nostacktrace.com The event bus Handler<Message<String>> myHandler = new Handler<Message<String>>() { public void handle(Message<String> message) { System.out.println("I received a message " + message.body);   // do work message.reply("This is a reply"); } };   eb.registerHandler("test.address", myHandler);   eb.send("test.address", "This is a message", new Handler<Message<String>>() {Norman Richards public void handle(Message<String> message) { System.out.println("I received a reply " + message.body); } });
  18. 18. orb@nostacktrace.com ... in groovy def myHandler = { message -> println "I received a message ${message.body}"  // do work message.reply "This is a reply" }   eb.registerHandler("test.address", myHandler)  Norman Richards eb.send("test.address", "This is a message") { message -> println "I received a reply ${message.body}" }
  19. 19. orb@nostacktrace.com ... in ruby Vertx::EventBus.registerHandler(test.address) do |message| puts("I received a message #{message.body}")   # do work   message.reply(This is a reply) end   Vertx::EventBus.send(test.address, This is a message) do |message| puts("I received a reply #{message.body}")Norman Richards end
  20. 20. orb@nostacktrace.com ... in python def handler(message): print "I received a message %s" % message.body   # do work message.reply(This is a reply)   EventBus.registerHandler(test.address, handler)   def reply_handler(message):Norman Richards print "I received a reply %s" % message.body EventBus.send(test.address, This is a message, reply_handler)
  21. 21. orb@nostacktrace.com ... in javscript var myHandler = function(message, replier) { log.info(I received a message + message);    // Now reply to it   replier(This is a reply); }   eb.registerHandler(test.address, myHandler);   eb.send(test.address, This is a message, function(reply) {Norman Richards log.info(I received a reply + reply); });
  22. 22. orb@nostacktrace.com ... in clojure !!! (defhandle my-handle [message] (println "I received a mesage" (:body message)) ;; do work (reply message "This is a reply"))   (register-handler event-bus "test.address" my-handle)   (send event-bus "test.address" "This is a message"Norman Richards (handle [response] (println "I received a reply" (:body response))))
  23. 23. orb@nostacktrace.com WHAT about things that block? blocking IO CPU-intensive operations legacy Java librariesNorman Richards
  24. 24. orb@nostacktrace.com WORKER VERTICLES Event Loop Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler Event Handler WORKER WORKER WORKER VERTICLE Worker Thread PoolNorman Richards
  25. 25. orb@nostacktrace.com more vert.x things timers streams / pumps / buffers module system SockJS / websockets eventbus bridgeNorman Richards outgoing network clients
  26. 26. Norman Richards orb@nostacktrace.com http://vertx.io
  27. 27. orb@nostacktrace.com Austin Clojure meetup Next Meeting: Monday, March 4. 7pm @ Capital FactoryNorman Richards http://www.meetup.com/Austin-Clojure-Meetup/
  28. 28. Norman Richards orb@nostacktrace.com http://lambdajam.com/
  29. 29. Norman Richards orb@nostacktrace.com thank you

×