Reputation: 105
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();
}
);
};
}
Upvotes: 4
Views: 3208
Reputation: 1100
I normally do this :
fun vertxInstance(): Vertx {
return Vertx.currentContext().owner()
}
//so call vertx this way
vertxInstance().executeBlocking(future -> {},{})
Upvotes: 0
Reputation: 7343
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 -> {...});
Upvotes: 5