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:
public class WikiDatabaseVerticle extends AbstractVerticle {
public static final String CONFIG_WIKIDB_JDBC_URL = "wikidb.jdbc.url";
public static final String CONFIG_WIKIDB_JDBC_DRIVER_CLASS = "wikidb.jdbc.driver_class";
public static final String CONFIG_WIKIDB_JDBC_MAX_POOL_SIZE = "wikidb.jdbc.max_pool_size";
public static final String CONFIG_WIKIDB_SQL_QUERIES_RESOURCE_FILE = "wikidb.sqlqueries.resource.file";
public static final String CONFIG_WIKIDB_QUEUE = "wikidb.queue";
@Override
public void start(Promise<Void> promise) throws Exception {
HashMap<SqlQuery, String> sqlQueries = loadSqlQueries();
JDBCClient dbClient = JDBCClient.createShared(vertx, new JsonObject()
.put("url", config().getString(CONFIG_WIKIDB_JDBC_URL, "jdbc:hsqldb:file:db/wiki"))
.put("driver_class", config().getString(CONFIG_WIKIDB_JDBC_DRIVER_CLASS, "org.hsqldb.jdbcDriver"))
.put("max_pool_size", config().getInteger(CONFIG_WIKIDB_JDBC_MAX_POOL_SIZE, 30)));
WikiDatabaseService.create(dbClient, sqlQueries, ready -> {
if (ready.succeeded()) {
ServiceBinder binder = new ServiceBinder(vertx);
binder
.setAddress(CONFIG_WIKIDB_QUEUE)
.register(WikiDatabaseService.class, ready.result()); (1)
promise.complete();
} else {
promise.fail(ready.cause());
}
});
}
/*
* Note: this uses blocking APIs, but data is small...
*/
private HashMap<SqlQuery, String> loadSqlQueries() throws IOException {
String queriesFile = config().getString(CONFIG_WIKIDB_SQL_QUERIES_RESOURCE_FILE);
InputStream queriesInputStream;
if (queriesFile != null) {
queriesInputStream = new FileInputStream(queriesFile);
} else {
queriesInputStream = getClass().getResourceAsStream("/db-queries.properties");
}
Properties queriesProps = new Properties();
queriesProps.load(queriesInputStream);
queriesInputStream.close();
HashMap<SqlQuery, String> sqlQueries = new HashMap<>();
sqlQueries.put(SqlQuery.CREATE_PAGES_TABLE, queriesProps.getProperty("create-pages-table"));
sqlQueries.put(SqlQuery.ALL_PAGES, queriesProps.getProperty("all-pages"));
sqlQueries.put(SqlQuery.GET_PAGE, queriesProps.getProperty("get-page"));
sqlQueries.put(SqlQuery.CREATE_PAGE, queriesProps.getProperty("create-page"));
sqlQueries.put(SqlQuery.SAVE_PAGE, queriesProps.getProperty("save-page"));
sqlQueries.put(SqlQuery.DELETE_PAGE, queriesProps.getProperty("delete-page"));
return sqlQueries;
}
}
- 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.