Top Banner
VERT.X Columbus Code Camp 2013 Yiguang Hu
30

Introduction to Vert.x

May 08, 2015

Download

Technology

Yiguang Hu

Introduction to Vert.x.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Vert.x

VERT.X

Columbus Code Camp 2013Yiguang Hu

Page 2: Introduction to Vert.x

What is it?

• PolyGlot• Simple• Scalable• Asynchronous• Concurrent

Page 3: Introduction to Vert.x

Simple

A groovy server example:

vertx.createHttpServer().requestHandler { req -> req.response.end "<html><body><h1>Hello

from vert.x!</h1></body></html>"}.listen(8080, "localhost")

Page 4: Introduction to Vert.x

Demo create a simple server that dispays a static html page

vertx run http/Servertxt.groovy

Page 5: Introduction to Vert.x

PolyGlot

• Java• groovy• Python/jython• Ruby• JavaScript(CoffeeScript, AngularJS)• (Scala, clojure to come….)• Mix them in one app

Page 6: Introduction to Vert.x

PolyGlot

Vert.x Core(Java)

Language Specific Layer

Natural API

Page 7: Introduction to Vert.x

Scalable

• Linear horizontal scale• Message passing • Automatic load-balancing• Efficiently utilizes server cores

Page 8: Introduction to Vert.x

Vert.x Architecture

Page 9: Introduction to Vert.x

Event Bus

Page 10: Introduction to Vert.x

Scaling

• Instances: -instance 10• Clustering: -cluster

Page 11: Introduction to Vert.x

AsynchronousJava Version web server –Anonymous inner class

public class ServerExample extends Verticle {

public void start() { vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { System.out.println("Got request: " + req.uri());

req.response().headers().set("Content-Type", "text/html; charset=UTF-8"); req.response().end("<html><body><h1>Hello from vert.x!</h1></body></html>"); } }).listen(8080); }}

Page 12: Introduction to Vert.x

Asynchronous

Groovy Version web server-closure

package http

vertx.createHttpServer().requestHandler { req -> req.response.end "<html><body><h1>Hello

from vert.x!</h1></body></html>"}.listen(8080, "localhost")

Page 13: Introduction to Vert.x

Asynchronous

JavaScript Version web server-function

var vertx = require('vertx')

vertx.createHttpServer().requestHandler(function(req) {

req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");

}).listen(8080);

Page 14: Introduction to Vert.x

AsynchronousJava Version web server –Anonymous inner class

public class ServerExample extends Verticle {

public void start() { vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { System.out.println("Got request: " + req.uri());

req.response().headers().set("Content-Type", "text/html; charset=UTF-8"); req.response().end("<html><body><h1>Hello from vert.x!</h1></body></html>"); } }).listen(8080); }}

Page 15: Introduction to Vert.x

WebSockets(Server)

vertx.createHttpServer().websocketHandler { ws ->

ws.dataHandler { data -> ws.writeTextFrame(data.toString()) }

}.requestHandler { req -> if (req.uri == "/") req.response.sendFile

"websockets/ws.html"}.listen(8080)

Page 16: Introduction to Vert.x

WebSockets (Client)<script> var socket; if (window.WebSocket) { socket = new WebSocket("ws://localhost:8080/myapp"); socket.onmessage = function(event) { alert("Received data from websocket: " + event.data); } socket.onopen = function(event) { alert("Web Socket opened!"); }; socket.onclose = function(event) { alert("Web Socket closed."); }; } else { alert("Your browser does not support Websockets. (Use Chrome)"); }

function send(message) { if (!window.WebSocket) { return; } if (socket.readyState == WebSocket.OPEN) { socket.send(message); } else { alert("The socket is not open."); } }</script>

Page 17: Introduction to Vert.x

SockJS

• Handles the communication between the browser and the server.

• Provides a websocket-like API in client-side JS• Works when websockets not available• JSON-Polling, XHR-Polling/Streaming, etc

Page 18: Introduction to Vert.x

SockJS<html><head> <title>SockJS Test</title> <script src="http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script></head><body>

<script> var sock = new SockJS('http://localhost:8080/testapp'); sock.onopen = function() { console.log('open'); }; sock.onmessage = function(e) { console.log('message', e.data); alert('received message echoed from server: ' + e.data); }; sock.onclose = function() { console.log('close'); };

function send(message) {

if (sock.readyState === SockJS.OPEN) { console.log("sending message") sock.send(message); } else { console.log("The socket is not open."); } }</script><form onsubmit="return false;"> <input type="text" name="message" value="Hello, World!"/> <input type="button" value="Send SockJS data" onclick="send(this.form.message.value)"/></form></body></html>

Page 19: Introduction to Vert.x

SockJSdef server = vertx.createHttpServer()

// Serve the index pageserver.requestHandler { req -> if (req.uri == "/") req.response.sendFile 'sockjs/index.html'}

// The handler for the SockJS app - we just echo data backvertx.createSockJSServer(server).installApp(prefix: '/testapp') { sock -> sock.dataHandler { buff -> sock << buff }}

server.listen(8080)

Page 20: Introduction to Vert.x

How module communicate

• Shared data• Maps or Sets• May only store immutable data• Immutable-no concurrency issue

Page 21: Introduction to Vert.x

Publish/subscribe messaging

Page 22: Introduction to Vert.x

Publish/subscribe

var eb = require("vertx/event_bus");var console = require("vertx/console");var vertx = require("vertx")

vertx.setPeriodic(1000, function sendMessage() {

eb.publish('news-feed', 'some news!');})

Page 23: Introduction to Vert.x

subscriber

var eb = require("vertx/event_bus");var console = require("vertx/console");

eb.registerHandler("news-feed", function(message) {

console.log('Received news ' + message);});

Page 24: Introduction to Vert.x

P-to-P

Page 25: Introduction to Vert.x

Ping

var eb = require("vertx/event_bus");var console = require("vertx/console");var vertx = require("vertx")

vertx.setPeriodic(1000, function sendMessage() { eb.send('ping-address', 'ping!', function(reply) { console.log("Received reply: " + reply); });})

Page 26: Introduction to Vert.x

receiver

var eb = require("vertx/event_bus");var console = require("vertx/console");

eb.registerHandler("ping-address", function(message, replier) {

console.log('Received message ' + message); // Now reply to it replier('pong!');});

Page 27: Introduction to Vert.x

Event Bus

• Fit asynchronous Model• Decoupling-Only data, no method calls• Can be distributed• JSON

Page 28: Introduction to Vert.x

Demonstration

• Web app• Listen on a topic and send received data to

web through websocket• A server that searches random topics on

duckduckgo periodically and publish result to the topic

• Multiple browsers display the data and are update simultaneously

Page 30: Introduction to Vert.x