2016-12-23 26 views
0

我無法獲得「.listen()」方法之外的任何配置讀數。無法在「.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插件。

有人知道爲什麼嗎?

+0

您能否顯示整個Verticle代碼?你如何打包?你如何運行它?沒有更多的上下文很難幫助 – tsegismont

+0

@tsegismont我增加了更多的代碼。這真的很簡單。我用'java -jar test.jar'運行jar – perotom

回答

1

這是因爲您的AbstractVerticle沒有收到任何部署選項。這是定製的。你可以看到例如它是如何在入門級完成:

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的第二個參數傳遞

相關問題