JUDCon Brazil 2013 - Vert.x an introduction

28
Vert.x, an Introduction Clebert Suconic Samuel Tauil

Transcript of JUDCon Brazil 2013 - Vert.x an introduction

Page 1: JUDCon Brazil 2013 - Vert.x an introduction

Vert.x, an Introduction

Clebert SuconicSamuel Tauil

Page 2: JUDCon Brazil 2013 - Vert.x an introduction

Project Info• 100% open source

• Docs and web-site licensed under Creative Commons

• Code licensed under ASL 2.0

• Top 10 Java projects on github

https://github.com/languages/Java/most_watched

Page 3: JUDCon Brazil 2013 - Vert.x an introduction

What is Vert.x?

• General purpose application platform

• VMware sponsored OS project

• Runs on the JVM

• Provides primarily asynchronous APIs

• Popular for modern web applications

• Solves similar problems as Node.js, Akka, Play…

Page 4: JUDCon Brazil 2013 - Vert.x an introduction

vert.x

Framework to write

polyglot,

highly concurrent apps

Page 5: JUDCon Brazil 2013 - Vert.x an introduction

vert.x

Framework to write

polyglot,

highly concurrent apps

Page 6: JUDCon Brazil 2013 - Vert.x an introduction

Javascript

load('vertx.js')

vertx.createHttpServer().requestHandler(function(req) { req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");}).listen(8080, 'localhost');

Page 7: JUDCon Brazil 2013 - Vert.x an introduction

Javascript

load('vertx.js')

vertx.createHttpServer().requestHandler(function(req) { req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");}).listen(8080, 'localhost');

$ vertx run example.js –instances 32

Page 8: JUDCon Brazil 2013 - Vert.x an introduction

Groovy

vertx.createHttpServer().requestHandler { req ->  req.response.end "<html><body><h1>Hello from vert.x!</h1></body></html>"}.listen(8080, "localhost")

$ vertx run example.groovy

Page 9: JUDCon Brazil 2013 - Vert.x an introduction

Python

server = vertx.create_http_server()

@server.request_handlerdef handle(req):    req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>")    server.listen(8080)

$ vertx run example.py

Page 10: JUDCon Brazil 2013 - Vert.x an introduction

Ruby

require "vertx"include Vertx

HttpServer.new.request_handler do |req|  req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>")end.listen(8080)

$ vertx run example.rb

Page 11: JUDCon Brazil 2013 - Vert.x an introduction

Javaimport 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);  }}

$ vertx run ServerExample.java

Page 12: JUDCon Brazil 2013 - Vert.x an introduction

vert.x

Framework to write

polyglot,

highly concurrent apps

Page 13: JUDCon Brazil 2013 - Vert.x an introduction

Core Concepts

• Verticle

• vert.x instance

• Event bus

Page 14: JUDCon Brazil 2013 - Vert.x an introduction

Verticles runs inside a vert.x instance.

A single vert.x instance runs inside its own

JVM instance.

Page 15: JUDCon Brazil 2013 - Vert.x an introduction

Verticles

• Are the execution units of vert.x

• Mult-language by modules

• Application is composed by many verticles

• Verticles communicate by message passing

• Verticles provide isolation

Page 16: JUDCon Brazil 2013 - Vert.x an introduction

Event Bus

• The nervous system of Vert.x

• Verticles communicate using the event bus.

• Ultra simple API.

• Point to point. Publish/Subscribe. Request/Response.

• Pass simple strings, numbers or other primitive types.

• JSON messages are preferred for structured data.

Page 17: JUDCon Brazil 2013 - Vert.x an introduction

Event Bus

• Allows verticles to talk each other

• Works cross language

• Works across the cluster

• Spans from server to client side

Page 18: JUDCon Brazil 2013 - Vert.x an introduction

Event Bus

• Register Handlers

• Unregister Handlers

• Addresses

• Messages (transient)

Page 19: JUDCon Brazil 2013 - Vert.x an introduction

load('vertx.js')

var eb = vertx.eventBus;

var address = 'example.address'

var handler = function(message) { stdout.println('Received message ' + message)}

eb.registerHandler(address, handler);

function vertxStop() { eb.unregisterHandler(address, handler);}

Handler

Page 20: JUDCon Brazil 2013 - Vert.x an introduction

load('vertx.js')

var eb = vertx.eventBus;

var address = 'example.address’

vertx.setPeriodic(2000, sendMessage)

var count = 0

function sendMessage() {  var msg = "some-message-" + count++;  eb.send(address, msg);  stdout.println("sent message " + msg)}

Sender

Page 21: JUDCon Brazil 2013 - Vert.x an introduction

Publish Subscribe

  eb.publish(‘example.address’, ‘hello world’); 

Page 22: JUDCon Brazil 2013 - Vert.x an introduction

Point to point

  eb.send(‘example.address’, ‘hello world’); 

Page 23: JUDCon Brazil 2013 - Vert.x an introduction

Scaling

• Scale by creating more Verticle instances

• Use message passing to communicate.

• Sounds like the Actor Model? It's similar

• For TCP and HTTP servers Vert.x will automatically load-balance

Page 24: JUDCon Brazil 2013 - Vert.x an introduction

SockJS

• Handles the communication between the browser and the server.

• Provides a websocket-like API in client-side JS

• Works when websockets not available

• More info at http://sockjs.org

Page 25: JUDCon Brazil 2013 - Vert.x an introduction

Internals

Netty for network IO

JRuby for the Ruby engine

Groovy

Mozilla Rhino for JS

Jython for Python support

Hazelcast for clustering

Page 26: JUDCon Brazil 2013 - Vert.x an introduction

Future

• Scala and Clojure support

• Management, including GUI console

• Developer experience - IDE integration, testing

• Direct-style API using continuations?

Page 27: JUDCon Brazil 2013 - Vert.x an introduction

http://vertx.io/