Vert.x

High Performance Application Platform for the JVM

Created by Daniel Jahre

Features

  • Polyglott programming
  • Concurrency
  • Scalability

NodeJS

  • Inspired by ...
    • Event driven programming
    • Super Simple Concurrency model
    • Event Bus
    • Module System & Public Module Repository

History

  • started in 2011 by Tim Fox (VMware, now works at RedHat) as Node.x
  • 2012 Release 1.0
  • 2013 Vert.x moved to Eclipse Foundation and release 2.0 got ready.
  • Nov 2014 Release 2.1.5
  • Release 3 WIP (planned for June 2015
  • All code is on github.

Used Libraries

  • Netty
  • Hazelcast

Languages

  • Java
  • Groovy
  • Ruby
  • Python
  • Scala
  • Clojure
  • Ceylon

Vert.x Terminology

Verticle

  • "Class with a main method"
  • Can include other scripts referred to by the main method
  • Can include jar files and resources
  • An application consists of one or more Verticles communicating over the event bus.
  • Worker Verticles are intended for blocking operations, using different thread pool

Vert.x Instance

  • Basically the instance of the jvm
  • Each verticle can have its own classloader
  • Verticles can run on a single instance or on multiple instances

Concurrency

  • Each verticle runs in a single thread
  • A Verticle can be started with multiple instances.
  • Verticles communicate by message passing

Shared data

  • Global accessible shared map
  • Shared maps can only be shared in one instance

Event based programming & Event loops

  • Event handlers
  • Each thread executes Event loop, to call handlers
  • Blocking operations are done in Worker verticles, using a different threadpool
  • Event loop should never be blocked

Event bus

  • Verticles send and receive messages over the event bus
  • Point to point, Publish/Subscribe, Request/Response
  • strings, buffers, primitive types
  • Recommended: JSON, can be consumed by every supported language

Clustered Event Bus

  • Connects Verticles on different JVM instances
  • using Hazelcast as a default, other implementations planned for 3.x
  • Event bus extends to Client side too via Javascript

Modules

  • Encapsulate code and resources
  • One or more modules per application
  • mod.json
  • Can be runnable or non-runnable
  • vendor~module-name~version

Fatjar

  • combines vert.x runtime, dependencies and the module into a jar
  • can be executed with java -jar
  • no other deployment needed

Hello World (Javascript)

  • Simple HTTP Server
    
    						      var vertx = require('vertx');
    
    vertx.createHttpServer().requestHandler(function(req) {
      req.response.end("Hello World!");
    }).listen(8080, 'localhost');
    
    					
  • Just run from the commandline
    
    						       vertx run server.js
    					
  • SockJS - Example

    Prerequisites

    DateSender Verticle

    
    						                 package linuxwochen
    
    def eb = vertx.eventBus
    
    def timerId = vertx.setPeriodic(1000) {
        timerId ->
            eb.publish("datetimeupdate", new Date().toString())
        }
    
    					

    SockSJServer Verticle

    
    						package linuxwochen
    
    def server = vertx.createHttpServer()
    
    def config = ["prefix": "/eventbus"]
    def eb = vertx.eventBus
    
    container.deployVerticle("DateSender.groovy")
    
    eb.registerHandler("datetimeupdate", {
        message -> println("${message.body}")
    } )
    
    eb.registerHandler("browser", {
        message -> println("browser connected:" +
    				new Date().toString() +
    				": ${message.body}")
    
    })
    
    server.requestHandler { req  ->
        def file = req.path == "/" ? "sockjsclient.html" : req.path
        //attention, might be escaped ..
        req.response.sendFile("./" + file)
    }
    
    vertx.createSockJSServer(server).bridge(config, [[:]], [[:]])
    
    server.listen(8080)
    					

    Manual example for securing a bridge

    						
    							    def server = vertx.createHttpServer()
    
    def config = ["prefix": "/eventbus"]
    
    // This defines the matches for client --> server traffic
    def inboundPermitted = []
    
    // Let through any messages sent to 'demo.orderMgr'
    inboundPermitted << ["address": "demo.orderMgr"]
    
    // Allow messages to the address 'demo.persistor' as long as the messages
    // have an action field with value 'find' and a collection field with value
    // 'albums'
    inboundPermitted << ["address": "demo.persistor",
                         "match": [ "action": "find",
                                    "collection": "albums"]]
    
    // Allow through any message with a field `wibble` with value `foo`.
    inboundPermitted << ["match", ["wibble": "foo"]]
    
    // This defines the matches for server --> client traffic
    def outboundPermitted = []
    
    // Let through any messages from address "ticker.mystock"
    outboundPermitted << ["address": "ticker.mystock"]
    
    // Let through any messages from addresses starting with "news." (e.g. news.europe, news.usa, etc)
    outboundPermitted << ["address_re": "news\\..+"]
    
    vertx.createSockJSBridge(server).bridge(config, inboundPermitted, outboundPermitted)
    
    server.listen(8080)
    						
    					

    Client side Javascript code

    
    						 
    
    
    

    Vert.x Example

    Programming comfort

    • Vert.x can run Java source files
    • Hot reloading of classes can be activated for module development
    • Maven and Gradle templates available

    Vert.x 3

    • Apex - Webframework around Vert.x
    • RxJava API
    • New cluster managers
    • Roadmap

    Links

    THE END