Java Handbook
  • Introduction
  • Numbers
    • Big numbers
    • Binary representation of numbers
  • Operators
  • Hashing
    • Hash Code & Performance
    • How Hash Table Is Created
  • Java 9
    • Modules
    • Other Improvements
  • Concurrency
    • Locking
  • Reactive Programming
    • Event Driven Non-Blocking Frameworks
    • Vert.x
      • Making a game
      • SockJS
    • Netty
    • RxJava
  • Efficient Coding
    • Lombok
  • Quartz Scheduling
Powered by GitBook
On this page

Was this helpful?

  1. Reactive Programming

Vert.x

PreviousEvent Driven Non-Blocking FrameworksNextMaking a game

Last updated 5 years ago

Was this helpful?

Eclipse Vert.x is event driven _and _non blocking. This means your app can handle a lot of concurrency using a small number of kernel threads. Vert.x lets your app scale with minimal hardware. --

Vert.x is an entire toolkit and ecosystem of pluggable modules on top of Netty for building reactive applications on top of the JVM.

Issues with threads that Vert.x with Netty solved: A large block of memory has to be allocated and initialized for the thread stack. System calls need to be made to register the native thread with the host OS. Descriptors needs to be created, initialized and added to JVM internal data structures.

Hello world

Here are for Vert.x, but lets create hello world app as HTTP server.

Lets create a project in Gradle.

plugins {
  id 'java'
  id 'application'
}

ext {
  vertxVersion = '3.5.0'
}

repositories {
  mavenLocal()
  jcenter()
}

version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'

dependencies {
  compile "io.vertx:vertx-core:$vertxVersion"
  compile "io.vertx:vertx-unit:$vertxVersion"
}

mainClassName = 'com.example.demo.App'

task wrapper(type: Wrapper) {
  gradleVersion = '4.0'
}

Then we can create runnable class that starts up the server.

package com.example.demo;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;

public class App {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        HttpServerOptions options = new HttpServerOptions().setLogActivity(true);

        HttpServer httpServer = vertx.createHttpServer(options);
        httpServer.requestHandler(request -> {
            request.response().end("Hello World");
        });
        httpServer.listen(8888, res -> {
            if (res.succeeded()) {
                System.out.println("Server is now listening!");
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }
}

When start the server and execute HTTP request, we get this.

$ gradle run
Server is now listening!

$ curl localhost:888
Hello World
http://vertx.io
many use cases