无法在“.listen()”方法之外获取任何配置读数
Unable to get any configuration readings outside the ".listen()" method
我无法在“.listen()”方法之外获取任何配置读数。
public class APIVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) {
System.out.println("Config: " + config().getInteger("http.port")); //prints null
vertx
.createHttpServer(serverOptions)
.requestHandler(router::accept)
.listen(
config().getInteger("http.port", 8080), //gets 8082
result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
}
);
}
}
我的配置文件:
{
"http.port": 8082
}
它被打包成带有 maven-shade-plugin
插件的 fat jar。
有人知道为什么吗?
那是因为您的 AbstractVerticle 没有收到任何部署选项。它是定制的。您可以在 Starter class:
中查看如何完成的示例
String confArg = args.map.get("-conf");
JsonObject conf;
if (confArg != null) {
try (Scanner scanner = new Scanner(new File(confArg)).useDelimiter("\A")){
String sconf = scanner.next();
try {
conf = new JsonObject(sconf);
} catch (DecodeException e) {
log.error("Configuration file " + sconf + " does not contain a valid JSON object");
return;
}
} catch (FileNotFoundException e) {
try {
conf = new JsonObject(confArg);
} catch (DecodeException e2) {
log.error("-conf option does not point to a file and is not valid JSON: " + confArg);
return;
}
}
} else {
conf = null;
}
...
deploymentOptions = new DeploymentOptions();
deploymentOptions.setConfig(conf)
HttpServer 默认接收那些 DeploymentOptions。
如果您有传递给自定义 Verticle 的选项,请将它们作为 .deployVerticle
的第二个参数传递
我无法在“.listen()”方法之外获取任何配置读数。
public class APIVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) {
System.out.println("Config: " + config().getInteger("http.port")); //prints null
vertx
.createHttpServer(serverOptions)
.requestHandler(router::accept)
.listen(
config().getInteger("http.port", 8080), //gets 8082
result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
}
);
}
}
我的配置文件:
{
"http.port": 8082
}
它被打包成带有 maven-shade-plugin
插件的 fat jar。
有人知道为什么吗?
那是因为您的 AbstractVerticle 没有收到任何部署选项。它是定制的。您可以在 Starter class:
中查看如何完成的示例 String confArg = args.map.get("-conf");
JsonObject conf;
if (confArg != null) {
try (Scanner scanner = new Scanner(new File(confArg)).useDelimiter("\A")){
String sconf = scanner.next();
try {
conf = new JsonObject(sconf);
} catch (DecodeException e) {
log.error("Configuration file " + sconf + " does not contain a valid JSON object");
return;
}
} catch (FileNotFoundException e) {
try {
conf = new JsonObject(confArg);
} catch (DecodeException e2) {
log.error("-conf option does not point to a file and is not valid JSON: " + confArg);
return;
}
}
} else {
conf = null;
}
...
deploymentOptions = new DeploymentOptions();
deploymentOptions.setConfig(conf)
HttpServer 默认接收那些 DeploymentOptions。 如果您有传递给自定义 Verticle 的选项,请将它们作为 .deployVerticle
的第二个参数传递