无需请求即可从java代码确定Glassfish HTTP端口号

时间:2014-12-13 13:18:00

标签: java java-ee glassfish glassfish-3

我正在使用Glassfish 3.1.2.2。是否可以从java(EJB)代码中确定http端口号(http-listener-1)?我没有任何联系或请求,所以阅读ServletRequest不是一种选择。

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。您可以使用Glassfish Admin REST API来检索服务器的几乎所有属性。

对于http-listener-1的属性,请使用以下网址:http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1

正常的GET请求会为您提供HTML响应,将Accept标头设置为application/json以检索JSON中的响应。

在您的EJB中,您只需使用正确的标头向上面的URL发出HTTP请求。这是一个例子:

String url = "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

con.setRequestProperty("Accept", "application/json");

BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

System.out.println(response.toString());

这会给你一个类似的结果:

{
    "message": "",
    "command": "http-listener-1",
    "exit_code": "SUCCESS",
    "extraProperties": {
        "commands": [
            {
                "path": "create-ssl",
                "method": "POST",
                "command": "create-ssl"
            }
        ],
        "methods": [
            {
                "name": "GET"
            },
            {},
            {
                "messageParameters": {
                    "address": {
                        "defaultValue": "0.0.0.0",
                        "optional": "true",
                        "type": "string",
                        "key": "false"
                    },
                    "enabled": {
                        "defaultValue": "true",
                        "optional": "true",
                        "type": "boolean",
                        "key": "false"
                    },
                    "jkConfigurationFile": {
                        "defaultValue": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
                        "optional": "true",
                        "type": "string",
                        "key": "false"
                    },
                    "jkEnabled": {
                        "defaultValue": "false",
                        "optional": "true",
                        "type": "boolean",
                        "key": "false"
                    },
                    "name": {
                        "optional": "false",
                        "type": "string",
                        "key": "true"
                    },
                    "port": {
                        "optional": "false",
                        "type": "int",
                        "key": "false"
                    },
                    "protocol": {
                        "optional": "false",
                        "type": "string",
                        "key": "false"
                    },
                    "threadPool": {
                        "optional": "true",
                        "type": "string",
                        "key": "false"
                    },
                    "transport": {
                        "optional": "false",
                        "type": "string",
                        "key": "false"
                    }
                },
                "name": "POST"
            },
            {
                "messageParameters": {
                    "target": {
                        "acceptableValues": "",
                        "defaultValue": "server",
                        "optional": "true",
                        "type": "string"
                    }
                },
                "name": "DELETE"
            }
        ],
        "entity": {
            "address": "0.0.0.0",
            "enabled": "true",
            "jkConfigurationFile": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
            "jkEnabled": "false",
            "name": "http-listener-1",
            "port": "8080",
            "protocol": "http-listener-1",
            "threadPool": "http-thread-pool",
            "transport": "tcp"
        },
        "childResources": {
            "find-http-protocol": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/find-http-protocol",
            "property": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/property"
        }
    }
}

如您所见,结果包含端口(在本例中为8080)。

您可以从响应字符串手动解析值,也可以使用一些JSON库将响应转换为JSON对象,您可以从中轻松检索该属性。

如果您的Glassfish管理界面不受保护,此程序应该有效,如果您启用了安全管理,则可能必须使用HTTP请求发送授权参数。

另见: