Vert.x is an open source framework developed by the Eclipse Foundation. Vert.x is an event-driven framework for the Java Virtual Machine.
What is vert.x?
First, let’s take a look at the definition of vert.x.
Vert.x is a polyglot, event driven and non blocking framework, which enables you to write reactive applications.
But what does polyglot, event driven, non blocking or reactive mean?
polyglot
Polyglot says that you can use vert.x in different languages. This is possible because the most common APIs for the supported languages are provided. So you can decide for yourself based on your requirements in which language you would like to implement them. Assuming that the selected language is one of the following languages: “Java, Javascript, Groovy, Ruby, Ceylon, Scala or Kotlin”. ;-))
Event driven
Don’t call us, we’ll call you.
As soon as an event of your choice occurs, Vert.x will notify you by sending events for this case. This means you can perform tasks asynchronously and still be notified when the task is done.
Non blocking
If software gets stuck during an essential calculation or some other computationally intensive operation, in most cases it is a worst-case scenario. The consequence of this is a long waiting time for the user before he receives his answer. Vert.x does not block threads. This makes it possible to take advantage of concurrency with a smaller number of threads.
Reactive
Reactive is the consequence of event driven and non blocking. This means that the program is built with asynchronous data streams. You can learn more about reactive programming in Andre Staltz’s Reactive tutorial .
Properties of vert.x
- lightweight
- Core Vert.x is 650kb
- fast
- You can find many benchmarks that confirm this.
- no application server
- no deployment necessary. Run your code whenever you want.
- modular
- There are many components available, so you can decide what you need.
- simple, but not too simple
- ideal choice for building lightweight, high-performance microservices
- polyglot
Our first verticle
Before we dive into the wonderful world of vert.x, let’s clarify the term Verticle
. A Verticle
is any component that has the following properties:
- single-threaded
- event-driven
- non-blocking
First, this is all you need to know about it. The following example shows you how easy it is to write a full web application.
|
|
Now let’s take a closer look at the code.
- First the class
BNovaVerticle
is created and extended by the classAbstractVerticle
. - To implement our web server, we must first override the
start()
method of theAbstractVerticle
class. - Then we create the HttpServer and register the RequestHandler. As soon as a request arrives, a response with “Hello from b-nova” is sent.
- Then the port of the web server is set to 8080. As far as the server can be started on this port, we made it.
EventBusReader
|
|
- First, the
Consumer
is registered on theEvent Bus
. The consumer then listens to the specified address. (myCustomAddress ist just a String which you can freely choose)
Publisher
Now let’s take a look at the Publisher page. We modify our BNovaVerticle
so that it publishes a message every time a request is processed.
|
|
Every time a request is received, a message is sent over the event bus. If you register several recipients at the same address, the message will be sent to all recipients.
Conclusion
Note that this is just a small sample of what the vert.x framework offers. If you are building a lightweight and high-performance application, first take a look at all the practical modules that Vert.x provides .
This text was automatically translated with our golang markdown translator.