📑 what is Vert.x with Demo
- 🏷️Tags : #30-05-2022, #pending
🔗 Links
Key Takeaways
Overview
- framework that provide tools for building reactive application on the JVM
what is Vert.x ?
- Each Vert.x application consists of Verticle
- Each Verticle can be deployed and scale independently
- Communication between verticles happens over Event Bus
Each Verticle can be build using different programming language such as Java, Javascript, Scala , Kotlin etc
Vert.x is unopiniated and you can build the application the way you wanted.
- Ideal use case is with [[event driven microservices architecture]]
- we can also build monolithic application using Vert.x or we can include it in the spring project.
Vert.x is event-driven and non-blocking
- we need to define event handlers that are called by Vert.x when a certain event occurs.
- to pass the event to the handlers Vert.x uses a thread which is called event-loop.
- never execute any blocking code in your handler since this would also blockl the event-loop and slow down the whole application.
Example of blocking code
- calling a database and then waiting for the response.
- Vert.x solution for this is to use some popular non blocking client code
- wrapping the blocking code in a special handler.
Pros and Cons
- non blocking code is better performant since we are using lesser number of threads
- Fewer threads means less overhead and better CPU Usage and better scaling on many parallel requests
- non blocking code is difficult to read and write and debug.
- each vericle is thread safe by default since they are only accessed by single thread.
- make sure no blocking code is written and also take care of nested callbacks
Make sure you are not using any library that uses blocking code
Less developers to be familiar with reactive programming and hence ever developer should atleast check this type of coding style to that is reactive programming.
Verticles
- piece of code that Vert.x engine execute
- Being polygot verticles can be written in any of the supported languages.
- an application will have many verticles running in the same Vert.x instance and communicate each other using events via the event bus
Event Bus
- nerve system of Vert.x
- Being reactive verticles will remain dormant until they receive an event
- for communication between verticles event bus will be used and the message can be string or complex json object
- mesage handling is async
- message are queued to the event bus and control is returned to the sender.
- Later dequeued to the listening verticle.
- Response is send using Future and Callback
Documentation
Community
Demo
Simple Vert.x Maven Project
Use that as a template for setting up a Maven project with Vert.x.
package io.vertx.example;
import io.vertx.core.Vertx;
public class HelloWorldEmbedded {
public static void main(String[] args) {
Vertx.vertx()
.createHttpServer()
.requestHandler(req -> req.response().end("Testing simple Vertx Application"))
.listen(8080);
}
}
Vert.x Simple Maven Verticle Project
- here we have to extends
AbstractVerticle
- Override the method start
You can run it directly in your IDE by creating a run configuration that uses the main class io.vertx.core.Launcher
, passes in the arguments run io.vertx.example.HelloWorldVerticle
, and specify using the classpath of module maven-verticle
.
The pom.xml uses the Maven shade plugin to assemble the application and all it’s dependencies into a single "fat" jar.
To build a "fat jar"
mvn package
To run the fat jar:
java -jar target/maven-verticle-4.3.1-fat.jar
package io.vertx.example;
import io.vertx.core.AbstractVerticle;
public class HelloWorldVerticle extends AbstractVerticle {
@Override
public void start() {
// Create an HTTP server which simply returns "Hello World!" to each request.
// If a configuration is set it get the specified name String name = config().getString("name", "World");
vertx.createHttpServer()
.requestHandler(req -> req.response().end("From Deployed Verticle " + name + "!"))
.listen(8080);
}
}
- we can change the port number using listen
vertx.createHttpServer()
.requestHandler(req -> req.response().end("testing myself"))
.listen(9090);