JUDCon Brazil 2013 - Vert.x an introduction

Post on 08-May-2015

1.543 views 0 download

Transcript of JUDCon Brazil 2013 - Vert.x an introduction

Vert.x, an Introduction

Clebert SuconicSamuel Tauil

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

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…

vert.x

Framework to write

polyglot,

highly concurrent apps

vert.x

Framework to write

polyglot,

highly concurrent apps

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

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

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

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

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

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

vert.x

Framework to write

polyglot,

highly concurrent apps

Core Concepts

• Verticle

• vert.x instance

• Event bus

Verticles runs inside a vert.x instance.

A single vert.x instance runs inside its own

JVM instance.

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

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.

Event Bus

• Allows verticles to talk each other

• Works cross language

• Works across the cluster

• Spans from server to client side

Event Bus

• Register Handlers

• Unregister Handlers

• Addresses

• Messages (transient)

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

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

Publish Subscribe

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

Point to point

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

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

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

Internals

Netty for network IO

JRuby for the Ruby engine

Groovy

Mozilla Rhino for JS

Jython for Python support

Hazelcast for clustering

Future

• Scala and Clojure support

• Management, including GUI console

• Developer experience - IDE integration, testing

• Direct-style API using continuations?

http://vertx.io/

Questions?

Thanks

csuconic@redhat.com

samuel@redhat.com