garci560
garci560

Reputation: 3213

Why does Vert.x create a new event loop for an http server?

I have a very simple Vert.x application that exposes a ping endpoint:

LauncherVerticle.java

public class LauncherVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        DeploymentOptions options = new DeploymentOptions();
        options.setConfig(config());
        options.setInstances(1);

        String verticleName = Example1HttpServerVerticle.class.getName();
        vertx.deployVerticle(verticleName, options, ar -> {
            if (ar.succeeded()) {
                future.complete();
            } else {
                future.fail(ar.cause());
            }
        });
    }
}

PingVerticle.java

public class PingVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        Router router = Router.router(vertx);

        router.get("/ping").handler(context -> {
            String payload = new JsonObject().put("hey", "ho").encode();
            context.response().putHeader("content-type", "application/json").end(payload);
        });
    }
}

As expected, by default Vert.x creates two event loop threads that I can see with VisualVM:

2 event loop threads

Of course, the application doesn't do anything, so I know go and add an http server to PingVerticle:

    String host = "0.0.0.0";
    int port = 7777;
    vertx.createHttpServer().requestHandler(router::accept).listen(port, host, ar -> {
        if (ar.succeeded()) {
            future.complete();
        } else {
            future.fail(ar.cause());

        }
    });

Now I see in VisualVM that there are two new threads, an acceptor-thread, that I can more or less understand, and another eventloop-thread:

3 eventloop threads

Why is this third eventloop-thread created?

Upvotes: 2

Views: 1521

Answers (2)

VisalA
VisalA

Reputation: 21

Not much documentation on vert.x architecture but there is an interesting read on Understanding Vert.x Architecture

BTW, I have four core machine and i see the same number of threads at the application startup. I noticed increase in number of eventloop threads as more load is generated, while other threads remain single per vert.x process.

In short,

  1. vert.x-acceptor-thread-0
    • Always there when HttpServer is created
  2. vert.x-eventloop-thread-0
  3. vert.x-eventloop-thread-1
    • Vert.x app starts with two eventloop threads and adds more dynamically as needed until double the number of cores i.e. 2 * cores as per documentation.
  4. vert.x-blocked-thread-checker
    • Always there to detect blocking routine on eventloop for more than 2000 milliseconds.

Upvotes: 2

Nolequen
Nolequen

Reputation: 4245

According to vert.x javadoc:

The default number of event loop threads to be used = 2 * number of cores on the machine.

It seems, you have more than 1 core.

Upvotes: 3

Related Questions