softshipper
softshipper

Reputation: 34119

Does the http server run on verticle and own event loop?

I am trying to understand https://vertx.io/ Verticle system and the event loop thread.

Consider the following code:

public class MyVerticle extends AbstractVerticle {
  public void start() {
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8080);
  }
}

The code above is going to create a new Verticle(MyVerticle) that also owns event loop thread.

When the HTTP server is created with vertx.createHttpServer(), does it spread a new Verticle for HTTP server? If correct, the HTTP server runs on own Verticle with event loop thread and two verticles are active.

Does the MyVerticle event loop thread:

requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }

execute the registered request handler? If yes, how does MyVerticle receive the events from Http server to run the handler when a request comes in?

The code above is not clear, how the two verticles communicate with each other. Would be great if someone could clarify it.

Update

I am trying to depict the scenario:

enter image description here

Assume, I deploy two instances of the same verticle, then each verticle will have its own event-loop and the HTTP server will be started twice.

When the user sends the first request, then it will process on the Verticle 1 and the second request on the Verticle 2. When my assumption is correct, then the event loop threads are independent from each other. For me, that means for me it is no more single threaded.

For example:

public class MyVerticle extends AbstractVerticle {
  
  final int state;

  public void start() {
    vertx.createHttpServer().requestHandler(req -> {
      state = state + 1;
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8080);
  }
}

When I change the state, then I have to sychronize between Verticles?

I pretty sure I am wrong, that means I not understand the concept of verticle yet.

Upvotes: 2

Views: 819

Answers (1)

Julien Viet
Julien Viet

Reputation: 335

A verticle is a deployment unit associated with an event-loop. In your example the verticle controls the HTTP server (with listen and close methods). The HTTP server will uses the event-loop of the verticle that controls it.

When you deploy two instances of the same verticle (setting the deployment options to two), each verticle will have its own event-loop and the HTTP server will be started twice. The first verticle that binds the HTTP server triggers the server bind operation, the second verticle instead will register its request handler on the actual server started by the first verticle (since they use the same port). When the server accepts a new connection it will then load balance the connection on the two verticle instances that it is aware of. This is explained in this section of the documentation https://vertx.io/docs/vertx-core/java/#_server_sharing.

The recommended way for verticle to communicate is the event-bus. It provides a lighweight, fast and asynchronous message passing between verticles. A shared data structure can also be appropriate depending on the use case.

Upvotes: 5

Related Questions