vert.x Effortless asynchronous application development for the - - PowerPoint PPT Presentation

vert x
SMART_READER_LITE
LIVE PREVIEW

vert.x Effortless asynchronous application development for the - - PowerPoint PPT Presentation

vert.x Effortless asynchronous application development for the modern web and enterprise Stuart Williams 'Pid' Consulting Architect SpringSource Division of VMware* vert.x committer @pidster * Correct at time of writing What is vert.x?


slide-1
SLIDE 1

vert.x

Effortless asynchronous application development for the modern web and enterprise

slide-2
SLIDE 2

Stuart Williams 'Pid'

Consulting Architect

SpringSource Division of VMware* vert.x committer @pidster

* Correct at time of writing

slide-3
SLIDE 3

What is vert.x?

  • Polyglot
  • Simple
  • Scalable
  • Asynchronous
  • Concurrent
slide-4
SLIDE 4

Polyglot

Write application components in:

  • JavaScript + CoffeeScript + AngularJS
  • Ruby
  • Python
  • Groovy
  • Java

Mix and match several programming languages in a single app.

slide-5
SLIDE 5

Simple

  • ...without being simplistic
  • Create real applications in just a few lines of code
  • No sprawling XML config
slide-6
SLIDE 6

Scalable

  • Linear horizontal scale
  • Uses message passing
  • Automatic load-balancing
  • Efficiently utilise your server cores
slide-7
SLIDE 7

Asynchronous

  • NIO
  • Handlers
  • EventBus
  • FileSystem
  • WebSockets
  • SockJS
slide-8
SLIDE 8

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
  • Similar in some ways to socket.io
  • More info at http://sockjs.org
slide-9
SLIDE 9

Concurrency Model

  • A verticle instance is single-threaded.
  • Move away from 'Java-style' multi-threaded

concurrency

  • No more synchronized, volatile or locking
  • Wave goodbye to many race conditions
slide-10
SLIDE 10

Threading Model – Event Loops

  • vert.x implements the Multi-Reactor Pattern
  • An event loop is an OS thread
  • Handles events for many handlers
  • vert.x has multiple event loops – typically one per

core.

  • Don't block the event loop!
slide-11
SLIDE 11

Event Loop != Silver Bullet

  • Event loops not the best fit for all types of stuff
  • Long running calculations (Fibonacci anyone?)
  • Using blocking APIs – e.g. JDBC
  • Most traditional Java libs have blocking APIs
  • … how do we leverage them?
slide-12
SLIDE 12

Hybrid Threading Model

  • Don't force everything to run on an event loop
  • Worker verticles can block
  • Communicate with other verticles by message

passing.

  • Allows us to leverage the huge ecosystem of

blocking Java libs

slide-13
SLIDE 13

Real-time Web Applications

  • Modern definition of real-time
  • Event bus key technology for real-time web

applications

  • Focus on sending messages to server

components or other browser nodes

  • Mobile applications, online games
  • Use vert.x with any JS toolkit.
slide-14
SLIDE 14

Examples

slide-15
SLIDE 15

JavaScript

load('vertx.js’) vertx.createHttpServer().requestHandler(function(req) { var file = req.path === '/' ? 'index.html' : req.path; req.response.sendFile('webroot/' + file); }).listen(8080)

slide-16
SLIDE 16

Ruby

require "vertx" Vertx::HttpServer.new.request_handler do |req| file = req.uri == "/" ? "index.html" : req.uri req.response.send_file "webroot/#{file}" end.listen(8080)

slide-17
SLIDE 17

Python

import vertx server = vertx.create_http_server() @server.request_handler def request_handler(req): file = "index.html" if req.uri == "/" else req.uri req.response.send_file("webroot/%s"%file) server.listen(8080)

slide-18
SLIDE 18

Groovy

vertx.createHttpServer().requestHandler { req -> def file = req.uri == "/" ? "index.html" : req.uri req.response.sendFile "webroot/$file" }.listen(8080)

slide-19
SLIDE 19

Java

import org.vertx.java.core.Handler; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.deploy.Verticle; public class Server extends Verticle { public void start() { vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>(){ public void handle(HttpServerRequest req) { String file = req.path.equals("/") ? "index.html" : req.path; req.response.sendFile("webroot/" + file); } }).listen(8080); } }

slide-20
SLIDE 20

Architecture

slide-21
SLIDE 21

Key Dependencies

  • Java 7
  • Netty
  • Hazelcast
  • Jackson
  • JRuby
  • Jython
  • Rhino
slide-22
SLIDE 22

Key API Methods

vertx.createXXXServer vertx.createXXXClient vertx.fileSystem vertx.sharedData vertx.eventBus vertx.setPeriodic vertx.setTimer

slide-23
SLIDE 23

Servers

  • NetServer
  • HttpServer

– RouteMatcher – ServerWebSocket

  • SockJSServer
slide-24
SLIDE 24

Clients

  • NetClient
  • HttpClient

– WebSocket

  • SockJSSocket
slide-25
SLIDE 25

EventBus

eventBus.send(‘address’, message, replyHandler) eventBus.publish(‘address’, message)

  • EventBus Bridge – send messages direct to JavaScript in

web browsers

  • Messages are strongly typed in Java
  • JSON in JavaScript
  • Hash in Python, Ruby
slide-26
SLIDE 26

Cluster

  • Hazelcast manages event bus address map and

cluster membership

  • Explain cache, listeners
  • vert.x NetServer & NetClient used for comms
  • Fully configurable network ports
  • Use SSL for security
slide-27
SLIDE 27

vert.x Applications

slide-28
SLIDE 28

Event Loop

8

Worker Pool

20

Server Verticle EventBus Modules Worker Modules Init Verticle

Application Components

slide-29
SLIDE 29

Classloaders & Messages

slide-30
SLIDE 30

launcher.js

load('vertx.js') var config = { "address": "org.pidster.foobar.control", ”port": 8081 } vertx.deployModule('org.pidster.foobar-v1.0', config, 1, function(id) { // called when deployed }); function vertxStop() { // stop & clean up }

slide-31
SLIDE 31

Modules

  • Authentication Manager
  • Form Upload
  • JDBC Persistor
  • Mailer
  • Mongo Persistor
  • Session Manager
  • Web Server
  • Work Queue
  • AMQP
slide-32
SLIDE 32

mod-web-server

load('vertx.js’) var config = { "web_root": <web_root>, "port", <port>, "ssl": <ssl>, "key_store_password": <key_store_password>, "key_store_path": <key_store_path>, } vertx.deployModule('vertx.web-server-v1.0', config, 1, function() { // deployed });

slide-33
SLIDE 33

Module

  • Simple structure
  • Optional lib dir
  • Lazy installation
  • Modules can include other modules
  • mod.json

{ “main”:”org.pidster.jdbc.Main”, “worker”: “true”, “includes: “org.pidster-foobar-v1.0” }

slide-34
SLIDE 34

Developing & Testing

  • Gradle or Maven (or Ant!)
  • Use a verticle script to launch tests
  • vertx-junit-annotations

src/main/groovy src/test/groovy src/vertxInteg/groovy

slide-35
SLIDE 35

Best Practice

  • Verticle script to manage lifecycle
  • Define configuration in JSON
  • Compose from modules
  • Never block the event loop!
slide-36
SLIDE 36

What’s next?

slide-37
SLIDE 37

Languages

  • Languages as modules
  • Lazy installation
  • Scala
  • Clojure
slide-38
SLIDE 38

Scala

  • Basic implementation complete
  • TODO: examples and docs
  • github.com/swilliams-vmw/vertx-lang-scala
slide-39
SLIDE 39

Scala Web Server

vertx.createHttpServer .requestHandler({ req: HttpServerRequest => val file : String = if (req.path == “/”) “/index.html” else req.uri req.response.sendFile("webroot/" + file) }).listen(8080)

slide-40
SLIDE 40

Management

  • JMX API
  • Publish metrics on EventBus in JSON
  • Configurable sample time & filters
  • GUI
  • github.com/swilliams-vmw/mod-management
slide-41
SLIDE 41

Developers & API

  • IDE support
  • Build config and plugins
  • Better, easier testing
  • More examples
  • Promises API?
  • Direct-style API using continuations?
slide-42
SLIDE 42

Project

slide-43
SLIDE 43

Community

  • vert.x is Open Source
  • Active community
  • Contributions welcome
  • Module repository is growing
  • 8th most popular Java project on Github
slide-44
SLIDE 44

Project

  • http://vertx.io
  • @timfox – project lead
  • @pidster – project lurker
  • Uses Gradle for build
  • https://github.com/vert-x
  • Google Group: vertx
slide-45
SLIDE 45

Summary

  • Polyglot – use the languages you want
  • Simple concurrency – wave goodbye to most

race conditions

  • Leverage existing Java library ecosystem
  • Powerful distributed event bus which spans

client and server

  • Flexible module system
slide-46
SLIDE 46

Questions?