现在的问题是:我怎么检索播放监听的端口号,不管它是如何定义(配置,命令行参数,或者根本没有)。
这个答案使用播放框架中的应用程序检索端口号必须是玩V1,因为该代码不能编译。
在播放2 Java的有:
Integer port = Play.application().configuration().getInt("http.port");
不返回的实际端口号,至少不会在所有情况下。 如果游戏运行为:
run 80 -Dhttp.address=example.com
然后,它返回null
。
如果游戏运行如
run -Dhttp.port 80 -Dhttp.address=example.com
然后播放默认端口(9000)上启动。
作为biesior指出它的工作原理是提口两次:
play -Dhttp.port=80 "run 80"
因为一个是玩什么实际使用,另一种是什么报告,其中当然不是最佳的。
但它会回答我的开发模式问题。 然而,随着文档中说,玩不应该使用运行run
中刺。 那么什么是等效的start
命令,有没有更好的,更安全的方法? (我感兴趣的Java版本,但其他人可能想了解斯卡拉过。)
运行在开发者模式,你需要使用一倍符号
play -Dhttp.port=80 "run 80"
不幸的是你必须做的默认端口相同
play -Dhttp.port=9000 "run 9000"
所以,我只是建议写易起动一切必要的PARAMS shell脚本(或.bat在Windows中)。
我使用这个斯卡拉片段,以确定播放的监听端口。 不幸的是,我不得不硬编码的一些预设值的情况下, -Dhttp.port=...
未指定。
val listenPort =
if (!Play.isTest) {
System.getProperty("http.port", "9000")
}
else {
// Not on classpath: play.api.test.Helpers.testServerPort
// Instead, duplicate its implementation here:
System.getProperty("testserver.port", "19001")
}
我也想知道有没有更好的办法,如Play.port
但我没有发现任何东西-可以,除了你的问题:-)
(在你的榜样,它应该是-Dhttp.port=80
不-Dhttp.port 80
)。
我刚刚找到一种方式来获得该端口无论怎么被指定的端口。 使用反向路由得到一个呼叫对象,并使用absoluteUrl()方法来获得你的行动,其中包括端口的绝对URL。 代码片段:
public class RouteController extends Controller {
public Promise<Result> getPortAction() {
Call call = com.mycom.routes.RouteController.getPortAction();
String absoluteUrl = call.absoluteURL(request());
//Here absoluteUrl would be like http://localhost:9002/...., 9002 would be the port.
...
} }