Skip to content

Instantly share code, notes, and snippets.

@bsandhu
Created December 29, 2017 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsandhu/2342d17f057e526c78ac48ec1997af2d to your computer and use it in GitHub Desktop.
Save bsandhu/2342d17f057e526c78ac48ec1997af2d to your computer and use it in GitHub Desktop.
Vert.x verticle with executeBlocking
public class MainVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
vertx.createHttpServer().requestHandler(req -> {
vertx.executeBlocking(future -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
future.complete(new User("First", "Last", "122-100-1221"));
},
false,
res -> {
try {
req.response()
.setStatusCode(200)
.putHeader("content-type", "text/html")
.end("<!DOCTYPE html><html><body>" +
"<h1>Vert.x worker " + Thread.currentThread().getName() + "</h1>" +
"<hr/>" +
"<h2>" + objectMapper.writeValueAsString(res.result()) + "</h2>" +
"</body></html>");
} catch (JsonProcessingException e) {
e.printStackTrace();
req.response()
.setStatusCode(500)
.end();
}
});
}).listen(8080);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment