My application, an API server, is thought to be organized as follows:
MainVerticle
is called on startup and should create all necessary objects for the application to work. Mainly a mongoDB pool of connections (MongoClient.createShared(...)
) and a global configuration object available instance-wide. It also starts the HTTP Listener, several instances of aHttpVerticle
.HttpVerticle
is in charge of receiving requests and, based the commandxxx
in the payload, execute theXxxHandler.handle(...)
method.Most of the
XxxHandler.handle(...)
methods will need to access the database. In addition, some others will also deploy additional verticles with parameters from the global conf. For exampleLoginHandler.handle(...)
will deploy a verticle to keep user state while he's connected and this verticle will be undeployed when the user logs out.
I can't figure out how to get the global configuration object while being in XxxHandler.handle(...)
or in a "sub"-verticle. Same for the mongo client.
Q1: For configuration data, I tried to use SharedData
. In `MainVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
lm.put("var", "val");
and in `HttpVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
log.debug("var={}", lm.get("var"));
but the log output is var=null
.... What am I doing wrong ?
Q2: Besides this basic example with a <String, String>
map type, what if the value is a mutable Object like JsonObject
which actually is what I would need ?
Q3: Finally how to make the instance of the mongo client available to all verticles?
Instead of
getLocalMap()
you should be usinggetClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.Be aware that the shared operations are async and the code might look like (code in Groovy):
You should be able to use any
Serializable
objects in your map.