I have a class ConfigFactory
, it can give me some configuration from JSON files by vert.x conf modules.
public class ConfigFactory {
private static JsonObject result = new JsonObject();
static {
ConfigStoreOptions fileStore = new ConfigStoreOptions()
.setType("file")
.setOptional(true)
.setFormat("json")
.setConfig(new JsonObject().put("path", "conf/config.json"));
ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(fileStore);
ConfigRetriever retriever = ConfigRetriever.create(VertxSingleton.VERTX, options);
retriever.getConfig(ar -> {
if (ar.failed()) {
throw new RuntimeException("Config get error! Something went wring during getting the config");
} else {
result.mergeIn(ar.result());
}
});
}
public static JsonObject getHttpConfig() {
BiFunction<Integer, String, JsonObject> httpConfigFile = (port, host) -> new JsonObject()
.put("port", port).put("host", host);
if (!result.isEmpty()) {
JsonObject http = result.getJsonObject("http");
return httpConfigFile.apply(http.getInteger("port"), http.getString("host"));
} else {
throw new RuntimeException("HTTP Config get error! Something went wring during getting the config");
}
}
}
But in Verticle,I use JsonObject httpConfig = ConfigFactory.getHttpConfig();
,it will give me exception
HTTP Config get error! Something went wring during getting the config
.In this time, the result
was empty.
I found the static methods getHttpConfig
run before static code blocks. About one second, the static code blocks will run. At that time, the result
was not empty.
Can you help me? Thanks!