Verticles

Verticles are instantiated by Vert.x, can use the Vert.x API's to compose applications. The start method is called when an instance is created. From here, Verticles will typically call various Vert.x APIs to set up handlers.

package org.pjug.vertxpres; import org.vertx.java.platform.Verticle; // * A Java class can be run from the command line // | vertx run org/pjug/vertxpres/TestVerticle1.java // | (Compilation will happen in vertx) // v public class TestVerticle1 extends Verticle { public void start() { getContainer().logger().info("Test Verticle Started!"); } }

Create Servers, Set up Timers, use the Vert.x API

package org.pjug.vertxpres; import org.vertx.java.core.Handler; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.platform.Verticle; public class TestVerticle2 extends Verticle { public void start() { // * vertx Variable comes from the Verticle class that TestVerticle2 // | extends. From here we have access to the rich Async APIs that // | Vert.x provides. This is anywhere from network clients, servers // | file I/O, timers and the shared data structures. // v vertx.createHttpServer() // * Here's where the Async concepts become // | clearly visible. There are optional // | methods on most of the API's the provide // | a user to pass in a callback. // v .requestHandler(new Handler<HttpServerRequest>() { // * The APIs are designed with Java 8 and other languages // | that support "functional interfaces" that is interfaces // | with a single method. Languages like Java 8 and Groovy // | can instead use a type-hinted closure instead of all // | of the usual boiler plate code. // v @Override public void handle(HttpServerRequest request) { // * The response is part of the request object // | think of this as similar streamlining we get // | from the concepts behind Vert.x - to present // | a simpler application development platform. // | This is a concept Node.js started, and Vert.x // | takes to heart. // | * For more methods available in the // | | Vert.x API, please see the manual. // v v request.response().end("Thanks!"); } }).listen(8080); vertx.setTimer(1000, new Handler<Long>() { @Override public void handle(Long event) { container.logger().info("1 second elapsed"); } }); } }

Worker Verticles

Worker verticles are similar to normal Verticles, but are processed on threads that are blocking sensitive. If many worker threads being to block, it does not halt processing of other waiting work. Worker verticles are generally considered a plan-B for using APIs that have no async capabilities. It's always considered a better option to find an API that supports Async.

In addition to supporting blocking calls, worker verticles are also able to make network connections. Normal verticles will throw an exception if a network connection is made in a non-async way.