I’ve recently onboarded into a vertx codebase and have seen usage of singletons like:
class MainVerticle extends AbstractVerticle {
...
@Override
public void start(Promise<Void> startPromise) {
DatabaseClientWrapper.initialize(vertx);
vertx.deployVerticle(new OtherVerticle(...));
}
}
Where a singleton is initialized like the above, and then is later used like so:
class OtherVerticle extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) {
DbResult<T> result = DatabaseClientWrapper.getInstance().queryDatabase(...)
.onComplete(ar -> {
try {
processResult(ar);
} catch (Exception e) {
handleFailure(e);
}
});
}
}
My first question is if its ok to use a singleton like this? Will it cause any issues with multithreading?
Secondly, wouldn’t it be better to create a verticle for the database client, and then a verticle for the database service, and access the client around the application over the event bus?
Let me know what you think.
Tried researching different vertx patterns
TravisWarner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.