Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Spark Java应用程序获取自动定义的端口?_Java_Spark Java - Fatal编程技术网

如何为Spark Java应用程序获取自动定义的端口?

如何为Spark Java应用程序获取自动定义的端口?,java,spark-java,Java,Spark Java,在Java Spark(不是Apache Spark)的API文档中,可以指定端口0,使其自动选择端口。太好了 但是,我不知道在服务器启动后如何获取该端口。我可以在日志中看到: 15:41:12.459 [Thread-2] INFO spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:63134 但是我需要能够以编程的方式进行测试,这样我的集成测试每次都能够可靠地运行 那么如何获得该端口呢?我无法在API中获得该

在Java Spark(不是Apache Spark)的API文档中,可以指定端口0,使其自动选择端口。太好了

但是,我不知道在服务器启动后如何获取该端口。我可以在日志中看到:

15:41:12.459 [Thread-2] INFO  spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:63134
但是我需要能够以编程的方式进行测试,这样我的集成测试每次都能够可靠地运行


那么如何获得该端口呢?

我无法在API中获得该信息,因此我提交了一份申请

我能通过一堆丑陋的倒影找到它:

/**
 * Meant to be called from a different thread, once the spark app is running
 * This is probably only going to be used during the integration testing process, not ever in prod!
 *
 * @return the port it's running on
 */
public static int awaitRunningPort() throws Exception {
    awaitInitialization();
    //I have to get the port via reflection, which is fugly, but the API doesn't exist :(
    //Since we'll only use this in testing, it's not going to kill us
    Object instance = getInstance();
    Class theClass = instance.getClass();
    Field serverField = theClass.getDeclaredField("server");
    serverField.setAccessible(true);
    Object oneLevelDeepServer = serverField.get(instance);

    Class jettyServerClass = oneLevelDeepServer.getClass();
    Field jettyServerField = jettyServerClass.getDeclaredField("server");
    jettyServerField.setAccessible(true);
    //Have to pull in the jetty server stuff to do this mess
    Server jettyServer = (Server)jettyServerField.get(oneLevelDeepServer);

    int acquiredPort = ((ServerConnector)jettyServer.getConnectors()[0]).getLocalPort();

    log.debug("Acquired port: {}", acquiredPort);
    return acquiredPort;
}

在我们的集成测试中,这对我来说很好,但我不使用https,而且它通过获取反射保护字段深入API大约两个层次。我找不到其他方法来做这件事。很高兴被证明是错误的。

这将在Spark 2.6.0上起作用:

public static int start (String keystoreFile, String keystorePw)
{
    secure(keystoreFile, keystorePw, null, null);
    port(0);

    staticFiles.location("/public");

    get(Path.CLOCK, ClockController.time);
    get(Path.CALENDAR, CalendarController.date);

    // This is the important line. It must be *after* creating the routes and *before* the call to port()
    awaitInitialization();

    return port();
}

如果不调用waitingInitialization(),port()将返回0。

Sparks自己的测试中有什么内容吗?他们在测试中使用硬编码端口,而不是随机端口:(