Obtaining a database service proxy

The final steps to refactoring to Vert.x services is to adapt the HTTP server verticle to obtain a proxy to the database service and use it in the handlers instead of the event bus.

First, we need to create the proxy when the verticle starts:

  1. private WikiDatabaseService dbService;
  2. @Override
  3. public void start(Promise<Void> promise) throws Exception {
  4. String wikiDbQueue = config().getString(CONFIG_WIKIDB_QUEUE, "wikidb.queue"); (1)
  5. dbService = WikiDatabaseService.createProxy(vertx, wikiDbQueue);
  6. HttpServer server = vertx.createHttpServer();
  7. // (...)
  1. We just need to make sure to use the same event bus destination as the service that was published by WikiDatabaseVerticle.

Then, we need to replace calls to the event bus with calls to the database service:

  1. private void indexHandler(RoutingContext context) {
  2. dbService.fetchAllPages(reply -> {
  3. if (reply.succeeded()) {
  4. context.put("title", "Wiki home");
  5. context.put("pages", reply.result().getList());
  6. templateEngine.render(context.data(), "templates/index.ftl", ar -> {
  7. if (ar.succeeded()) {
  8. context.response().putHeader("Content-Type", "text/html");
  9. context.response().end(ar.result());
  10. } else {
  11. context.fail(ar.cause());
  12. }
  13. });
  14. } else {
  15. context.fail(reply.cause());
  16. }
  17. });
  18. }
  19. private void pageRenderingHandler(RoutingContext context) {
  20. String requestedPage = context.request().getParam("page");
  21. dbService.fetchPage(requestedPage, reply -> {
  22. if (reply.succeeded()) {
  23. JsonObject payLoad = reply.result();
  24. boolean found = payLoad.getBoolean("found");
  25. String rawContent = payLoad.getString("rawContent", EMPTY_PAGE_MARKDOWN);
  26. context.put("title", requestedPage);
  27. context.put("id", payLoad.getInteger("id", -1));
  28. context.put("newPage", found ? "no" : "yes");
  29. context.put("rawContent", rawContent);
  30. context.put("content", Processor.process(rawContent));
  31. context.put("timestamp", new Date().toString());
  32. templateEngine.render(context.data(), "templates/page.ftl", ar -> {
  33. if (ar.succeeded()) {
  34. context.response().putHeader("Content-Type", "text/html");
  35. context.response().end(ar.result());
  36. } else {
  37. context.fail(ar.cause());
  38. }
  39. });
  40. } else {
  41. context.fail(reply.cause());
  42. }
  43. });
  44. }
  45. private void pageUpdateHandler(RoutingContext context) {
  46. String title = context.request().getParam("title");
  47. Handler<AsyncResult<Void>> handler = reply -> {
  48. if (reply.succeeded()) {
  49. context.response().setStatusCode(303);
  50. context.response().putHeader("Location", "/wiki/" + title);
  51. context.response().end();
  52. } else {
  53. context.fail(reply.cause());
  54. }
  55. };
  56. String markdown = context.request().getParam("markdown");
  57. if ("yes".equals(context.request().getParam("newPage"))) {
  58. dbService.createPage(title, markdown, handler);
  59. } else {
  60. dbService.savePage(Integer.valueOf(context.request().getParam("id")), markdown, handler);
  61. }
  62. }
  63. private void pageCreateHandler(RoutingContext context) {
  64. String pageName = context.request().getParam("name");
  65. String location = "/wiki/" + pageName;
  66. if (pageName == null || pageName.isEmpty()) {
  67. location = "/";
  68. }
  69. context.response().setStatusCode(303);
  70. context.response().putHeader("Location", location);
  71. context.response().end();
  72. }
  73. private void pageDeletionHandler(RoutingContext context) {
  74. dbService.deletePage(Integer.valueOf(context.request().getParam("id")), reply -> {
  75. if (reply.succeeded()) {
  76. context.response().setStatusCode(303);
  77. context.response().putHeader("Location", "/");
  78. context.response().end();
  79. } else {
  80. context.fail(reply.cause());
  81. }
  82. });
  83. }

The WikiDatabaseServiceVertxProxyHandler generated class deals with forwarding calls as event bus messages.

Tip
It is still perfectly possible to consume a Vert.x service directly via event bus messages since this is what generated proxys do.