Understanding Event Loop
The goal of Event Loop is to continuously check for new events in a queue, and each time a new event comes in, to quickly dispatch it to a function that knows how to handle it. This way, a single thread or a very limited number of threads can handle a huge number of events.
In the case of web frameworks such as Vert.x, events may be requests to our server.
To understand the concept of the Event Loop better, let’s go back to our server code and attempt to implement an endpoint for deleting a cat:
val db = Db.connect(vertx)
router.delete("/:id").handler { ctx ->
val id = ctx.request().getParam("id").toInt()
db.preparedQuery("DELETE FROM cats WHERE ID = $1")
.execute(Tuple.of(id))
.await()
ctx.end()
}
This code is very similar to what we’ve written in our tests in the previous section. We read the URL parameter from the request using the getParam()
function, then we pass...