Vert.X mini-talk

29
[email protected] Norman Richards Vert.x A polyglot asynchronous application platform for the JVM

description

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

Transcript of Vert.X mini-talk

Page 1: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Vert.xA polyglot

asynchronous application platform

for the JVM

Page 2: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Do you need Async?

Conan, the C10K Barbarianhttp://www.mikeperham.com/

To crush their servers, s! them smoking before you, and to hear the lamentations

of their admins!

Conan, What is best in life?

Page 3: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Ok, so of course that means ...

http://bit.ly/ACrwel

Page 4: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Some Async options

Node.js (JavaScript)Twisted (Python)Tornado (Python)EventMachine (Ruby)Vert.x (Java)Vert.x (Javascript)Vert.x (Python)Vert.x (Ruby)Vert.x (Groovy)

Page 5: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Key advantages of vert.x

Runs on the JVM (Java 7+)

Choose the best language

Hybrid evented/threaded model

Page 6: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Evented IO

One thread handles all requests

Everything is an event handler

Never block the thread

Do work quickly + register callbacks

Any kind of service - not just HTTP

Page 7: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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>");      }    }).listen(8080);  }}

simple Example

Page 8: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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 received log.info("The total body received was " + body.length() + " bytes"); } });  }}).listen(8080, "localhost");

slightly bigger Example

Page 9: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

the unit of management/deployment

classloader isolated

one thread / one event loop

only one handler running at a time

no multi-threaded worriesVERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

the verticle

Page 10: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

One VERTICLE PER CPU

Shared Connection Load Balancer

Page 11: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

One VERTICLE PER CPU PER HOST

host1 host2

Page 12: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

SHARED STATE

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

Shared State

per-instance “session”

map or set interface

only immutable values

Page 13: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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"); 

SHARED MAPs

Page 14: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Set<String> set = vertx.sharedData().getSet("app.active-users"); set.add("alfred");set.contains("alfred");set.contains("remove");

SHARED SETS

Page 15: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

distributed event bus

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

Shared State

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

VERTICLE

Event Handler

Event Loop

Event Handler

Event Handler

Event Handler

Shared State

Event Bus

Page 16: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

ad-hoc addressing

pubsub or p2p messaging

not persistent

JSON-based messaging

The event bus

Page 17: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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>>() { public void handle(Message<String> message) { System.out.println("I received a reply " + message.body); }});

The event bus

Page 18: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

... 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)

 eb.send("test.address", "This is a message") { message -> println "I received a reply ${message.body}"}

Page 19: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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}") end

... in ruby

Page 20: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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): print "I received a reply %s" % message.body EventBus.send('test.address', 'This is a message', reply_handler)

... in python

Page 21: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

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) { log.info('I received a reply ' + reply);});

... in javscript

Page 22: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

(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" (handle [response] (println "I received a reply" (:body response))))

... in clojure !!!

Page 23: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

WHAT about things that block?

blocking IO

CPU-intensive operations

legacy Java libraries

Page 24: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

WORKER VERTICLES

VERTICLE

Event Loop

Event Handler

Event Handler

Event Handler

WORKER

Event Handler

Event Handler

Event Handler

WORKER

Event Handler

Event Handler

Event Handler

WORKER

Event Handler

Event Handler

Event Handler

Worker Thread Pool

Page 25: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

more vert.x things

timersstreams / pumps / buffersmodule systemSockJS / websocketseventbus bridgeoutgoing network clients

Page 26: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

http://vertx.io

Page 27: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

Austin Clojure meetup

http://www.meetup.com/Austin-Clojure-Meetup/

Next Meeting:Monday, March 4. 7pm @ Capital Factory

Page 28: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

http://lambdajam.com/

Page 29: Vert.X mini-talk

orb@

nost

ackt

race

.com

Norm

an R

icha

rds

thank you