【发布时间】:2014-12-14 03:43:08
【问题描述】:
我使用的是 Glassfish 3.1.2.2。是否可以从 java (EJB) 代码中确定 http 端口号 (http-listener-1)?我没有任何连接或请求,所以阅读 ServletRequest 不是一种选择。
【问题讨论】:
标签: java jakarta-ee glassfish glassfish-3
我使用的是 Glassfish 3.1.2.2。是否可以从 java (EJB) 代码中确定 http 端口号 (http-listener-1)?我没有任何连接或请求,所以阅读 ServletRequest 不是一种选择。
【问题讨论】:
标签: java jakarta-ee glassfish glassfish-3
是的,这是可能的。您可以使用 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 请求中发送授权参数。
另请参阅:
【讨论】: