gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:14 27 4
gpt4 key购买 nike

在 Java Spark(不是 Apache spark)的 API 文档中,您可以指定一个端口 0,让它自动选择一个端口。太棒了!

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

15:41:12.459 [Thread-2] INFO  spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:63134

但我需要能够以编程方式访问它,以便我的集成测试每次都能可靠地运行。

那么我如何获得该端口?

最佳答案

我找不到在 API 中获取此信息的方法,因此我提交了 issue on their github .

我能够通过一堆丑陋的反射(reflection)得到它:

/**
* 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,它确实通过反射抓取 protected 字段深入到 API 中大约两个级别。我找不到任何其他方法来做到这一点。很高兴被证明是错误的。

关于java - 如何获取 Spark Java 应用程序自动定义的端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com