I come from concept one request = one thread and now i want to undertand the concept of event loop using Vertx. According to docs if we use blocking operation we should pass it to executeBlocking method.
Verticles use the Vert.x worker pool for executing blocking actions, i.e executeBlocking or worker verticle.
I have some questions related to this paragraph from http://vertx.io/docs/vertx-core/groovy/
This is my code:
public class JdbcVertx extends AbstractVerticle{
public static void main(String[] args) {
Vertx v = Vertx.vertx();
v.deployVerticle(new JdbcVertx(),new DeploymentOptions().setWorkerPoolSize(10));
}
@Override
public void start() throws Exception {
JDBCClient client = JDBCClient.createNonShared(this.vertx, new JsonObject()
.put("url", "jdbc:postgresql://localhost:5432/test")
.put("user", "postgres")
.put("password", "pw")
.put("driver_class", "org.postgresql.Driver")
.put("max_pool_size", 30));
this.vertx.createHttpServer()
.requestHandler(r -> {
int i = (int) (Math.random() * 100);
System.out.println("Req id: " + i+ " "+Thread.currentThread().getName());
client.getConnection(handler -> {
if (handler.failed()) {
throw new RuntimeException(handler.cause());
} else {
final SQLConnection connection = handler.result();
this.vertx.executeBlocking(blockingCodeHandler->{
connection.execute(execute(), hndlr -> {
connection.close(closehndlr -> {
if (closehndlr.failed()) {
throw new RuntimeException(closehndlr.cause());
} else {
System.out.println("CON end: "+i+" "+Thread.currentThread().getName());
r.response().putHeader("content-type", "text/html").end("Response: " + i);
}
});
});
},false,as->{});
}
});
}).listen(8080);
}
private String execute(){
return "insert into rubbish (name) values ('test')";
}
I send 100 requests to http server this is output:
Req id: 45 vert.x-eventloop-thread-0
Req id: 22 vert.x-eventloop-thread-0
Req id: 96 vert.x-eventloop-thread-0
Req id: 85 vert.x-eventloop-thread-0
Req id: 33 vert.x-eventloop-thread-0
CON end: 22 vert.x-eventloop-thread-0
CON end: 33 vert.x-eventloop-thread-0
CON end: 45 vert.x-eventloop-thread-0
CON end: 85 vert.x-eventloop-thread-0
1) How event loop switched between requests (from output it firstly get some requests and then give them responses)?
2)I use executeBlocking method but how you can see code above use only event loop thread named vert.x-eventloop-thread-0
3)According to first question vertx event loop switched between request but if i change method execute to:
private String execute(){
Thread.sleep(3000);
return "insert into rubbish (name) values ('test')";
}
It will stop the event loop until time out is not finished
Thank you in advance