Reactive Polyglot Programming With

Organizer: Java Users Group of Greater Louisville (JUGGL)

Deven Phillips

Senior Software Engineer

Sungard Availability Services

Twitter: @jugglou

Facebook @JUGGLou

G+: JugglUs

http://linkd.in/1DcYOww

Reactive Polyglot Programming With

http://bit.ly/1PScOS7

Vert.x is a tool-kit for building reactive applications on the JVM.

is event driven and non-blocking. This means you can handle a lot of concurrency in your apps using a small number of threads. Vert.x lets your apps scale with minimal hardware.

You can use Vert.x with multiple JVM languages including Java, JavaScript and Groovy. Vert.x doesn't care what language is best — you use what language(s) that you want depending on the task at hand and the skill-set of your team.

We don't just force you to use our Java API in other languages - we provide idiomatic APIs for every language that Vert.x supports.

You can use Vert.x for all kinds of applications, for example: web applications and enterprise back-end applications.

 

Vert.x is not a framework or a container and we don't tell you a correct way to write an application. Instead we give you a lot of useful bricks and let you create your app the way you want to.

  • Lightweight - Vert.x core is around 650kB in size.

  • Fast.

  • An ideal choice for creating light-weight, high-performance, micro-services.

  • Not an application server. There's no monolithic Vert.x instance into which you deploy applications. You just run your apps wherever you want to.

  • Modular - when you need more bits just add the bits you need and nothing more.

  • Simple but not simplistic. Vert.x allows you to create powerful apps, simply.

The main building blocks of Vert.x applications are Modules and Verticles

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

Modules are a collection of Verticles which are packaged together and can be deployed either stand-alone or with other modules.

Verticles are typically small collections of code which serve a single purpose (Think 'particle of Vert.x code'). Verticles can be deployed and undeployed at runtime and can be network-wise distributed from one another and communicate over the Vert.x event bus.

Has clustering built-in at the root of the system. Currently, it uses HazelCast as the transport for clustering the event bus, but other implementations are coming.

The Event Bus is how Vert.x allows for simplified concurrency throughout the system. While Vert.x is multithreaded, each Verticle runs in isolation without shared state and they communicate via the Event Bus.

Unlike the design of Node.js/IO.js, Vert.x is NOT single-threaded. Vert.x allows you to choose how many instances of each Verticle is running concurrently. Since there is no shared-state and communication is via the event bus, this simplifies concurrency massively.

The Vert.x concurrency model resembles the Actor Model, where verticle instances correspond to actors. There are are however differences, for example, verticles tend to be more coarse grained than actors.

Internally, a Vert.x instance manages a small set of threads, matching the number of threads to the available cores on the server. We call these threads event loops, since they more or less just loop around seeing if there are any events to deliver and if so, delivering them to the appropriate handler. Examples of events might be some data has been read from a socket, a timer has fired, or an HTTP response has ended.

The Golden Rule - Don't block the event loop!

Blocking the event loops causes the whole application to hang, but there are ways to do blocking operations when needed. You can use Worker Verticles . . .

A worker verticle is just like a standard verticle but it’s executed not using an event loop, but using a thread from the Vert.x worker thread pool.

Another way to run blocking operations is to use a callback in conjunction with Futures/Promises

It’s done by calling executeBlocking specifying both the blocking code to execute and a result handler to be called back asynchronous when the blocking code has been executed.

vertx.executeBlocking(future -> {
  // Call some blocking API that takes a significant amount of time to return
  String result = someAPI.blockingMethod("hello");
  future.complete(result);
}, res -> {
  System.out.println("The result is: " + res.result());
});

That's the basic overview

Now, let's write some code!

Getting Started

  1. Install Java 8 JDK: http://www.java.com/jdk/
  2. Install Maven: http://maven.apache.org/
  3. Install Vert.x Distribution: http://vert-x3.github.io/
  4. Generate a project using the Maven Archetype

 

mvn archetype:generate 
    -DarchetypeGroupId=com.zanclus.codepalousa 
    -DarchetypeArtifactId=vertx-js-archetype

Documentation

Reactive Polyglot Programming With Vert.x

By Deven Phillips

Reactive Polyglot Programming With Vert.x

Vert.x is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications.

  • 5,003