4

I am using vertx 3.0 to run some mysql queries. Because that code is not designed for async operation I am using executeBlocking. Even though the code runs, when I use jconsole to monitor my server I find over 100 threads most being named vert.x-worker-thread-0, vert.x-eventloop-thread-1 and vertx-blocked-thread-checker. Is this caused by anything wrong with my code?

    public Handler<RoutingContext> getById() {
    return (routingContext) -> {
        HttpServerResponse response = routingContext.response();
        String id = routingContext.request().getParam("id");
        int idAsInt = Integer.parseInt(id);
        Vertx.vertx().executeBlocking(future -> {
                Person p = svc.getPerson(idAsInt);
                PersonDTO dto = new PersonDTO(p.getId(), p.getFirstname(), p.getName());
                future.complete(dto);
            }, res -> {
                if (res.succeeded()) {
                    JsonUtil.setJsonResponse(response, 200, Json.encodePrettily(res.result()));
                }
                else {
                    JsonUtil.setJsonResponse(response, 404, new JsonObject().put("failure", res.cause().getMessage()).toString());
                }
                response.end();
            }
        );
    };
}
Stephan Kennedy
  • 105
  • 1
  • 8

2 Answers2

5

You are doing the same blunder as I did once. Vertx guys must update this explicitly in their document

Vertx.vertx() everytime launches a new vertx instance and you end up having new event loop threads with every request.

Cache your Vertx.vertx() and use cached vertx instance every time.

Vertx cachedVertx = Vertx.vertx();
...
cachedVertx.executeBlocking(future -> {...});
prasun
  • 7,073
  • 9
  • 41
  • 59
  • You may also be interested in checking my post for similar mistake but, different error http://stackoverflow.com/questions/34293532/vert-x-getting-failed-to-create-a-child-event-loop – prasun Jan 05 '16 at 17:17
  • If you are in some routing code, pass the routing context which has this cached current instance: `final Vertx vertx = routingCtx.vertx();`, where routingCtx is of type `io.vertx.ext.web.RoutingContext;` – Christophe Roussy Feb 19 '18 at 09:53
0

I normally do this :

fun vertxInstance(): Vertx {
    return Vertx.currentContext().owner()
}

//so call vertx this way 
vertxInstance().executeBlocking(future -> {},{})
Big Zak
  • 1,040
  • 12
  • 17