Exposing the database service from the database verticle

As most of the database handling code has been moved to WikiDatabaseServiceImpl, the WikiDatabaseVerticle class now consists of 2 methods: the start method to register the service and a utility method to load SQL queries:

  1. public class WikiDatabaseVerticle extends AbstractVerticle {
  2. public static final String CONFIG_WIKIDB_JDBC_URL = "wikidb.jdbc.url";
  3. public static final String CONFIG_WIKIDB_JDBC_DRIVER_CLASS = "wikidb.jdbc.driver_class";
  4. public static final String CONFIG_WIKIDB_JDBC_MAX_POOL_SIZE = "wikidb.jdbc.max_pool_size";
  5. public static final String CONFIG_WIKIDB_SQL_QUERIES_RESOURCE_FILE = "wikidb.sqlqueries.resource.file";
  6. public static final String CONFIG_WIKIDB_QUEUE = "wikidb.queue";
  7. @Override
  8. public void start(Promise<Void> promise) throws Exception {
  9. HashMap<SqlQuery, String> sqlQueries = loadSqlQueries();
  10. JDBCClient dbClient = JDBCClient.createShared(vertx, new JsonObject()
  11. .put("url", config().getString(CONFIG_WIKIDB_JDBC_URL, "jdbc:hsqldb:file:db/wiki"))
  12. .put("driver_class", config().getString(CONFIG_WIKIDB_JDBC_DRIVER_CLASS, "org.hsqldb.jdbcDriver"))
  13. .put("max_pool_size", config().getInteger(CONFIG_WIKIDB_JDBC_MAX_POOL_SIZE, 30)));
  14. WikiDatabaseService.create(dbClient, sqlQueries, ready -> {
  15. if (ready.succeeded()) {
  16. ServiceBinder binder = new ServiceBinder(vertx);
  17. binder
  18. .setAddress(CONFIG_WIKIDB_QUEUE)
  19. .register(WikiDatabaseService.class, ready.result()); (1)
  20. promise.complete();
  21. } else {
  22. promise.fail(ready.cause());
  23. }
  24. });
  25. }
  26. /*
  27. * Note: this uses blocking APIs, but data is small...
  28. */
  29. private HashMap<SqlQuery, String> loadSqlQueries() throws IOException {
  30. String queriesFile = config().getString(CONFIG_WIKIDB_SQL_QUERIES_RESOURCE_FILE);
  31. InputStream queriesInputStream;
  32. if (queriesFile != null) {
  33. queriesInputStream = new FileInputStream(queriesFile);
  34. } else {
  35. queriesInputStream = getClass().getResourceAsStream("/db-queries.properties");
  36. }
  37. Properties queriesProps = new Properties();
  38. queriesProps.load(queriesInputStream);
  39. queriesInputStream.close();
  40. HashMap<SqlQuery, String> sqlQueries = new HashMap<>();
  41. sqlQueries.put(SqlQuery.CREATE_PAGES_TABLE, queriesProps.getProperty("create-pages-table"));
  42. sqlQueries.put(SqlQuery.ALL_PAGES, queriesProps.getProperty("all-pages"));
  43. sqlQueries.put(SqlQuery.GET_PAGE, queriesProps.getProperty("get-page"));
  44. sqlQueries.put(SqlQuery.CREATE_PAGE, queriesProps.getProperty("create-page"));
  45. sqlQueries.put(SqlQuery.SAVE_PAGE, queriesProps.getProperty("save-page"));
  46. sqlQueries.put(SqlQuery.DELETE_PAGE, queriesProps.getProperty("delete-page"));
  47. return sqlQueries;
  48. }
  49. }
  1. We register the service here.

Registering a service requires an interface class, a Vert.x context, an implementation and an event bus destination.

The WikiDatabaseServiceVertxEBProxy generated class handles receiving messages on the event bus and then dispatching them to the WikiDatabaseServiceImpl. What it does is actually very close to what we did in the previous section: messages are being sent with a action header to specify which method to invoke, and parameters are encoded in JSON.