如何在已经部署了jboss服务器的应用程序/ war内以编程方式找到jboss端口?使用Java。这是一个正在运行的Web服务,我们没有任何用户界面。
一旦您获得了连接到 JBoss MBeanServer 的 MBeanServerConnection,此语句将返回正确的端口:
String port = server.queryNames(
new ObjectName("jboss.web:type=Connector,address=*,port=*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")))
.iterator().next().getKeyProperty("port");
<%@page contentType="text/html" import="java.util.*,org.jboss.mx.util.*,javax.management.*" %>
<html><head><title>JBoss Web Server Port</title></head><body>
<%
try {
MBeanServerConnection server = MBeanServerLocator.locateJBoss();
String port = server.queryNames(
new ObjectName("jboss.web:type=Connector,address=*,port=*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")))
.iterator().next().getKeyProperty("port");
out.println("<p>Port:" + port + "</p>");
} catch (Exception e) {
e.printStackTrace(System.err);
}
%></body></html>
import javax.management.*;
import javax.management.remote.*;
conn = null;
try {
url = new JMXServiceURL("service:jmx:rmi://njw810/jndi/rmi://njw810:1090/jmxconnector");
conn = JMXConnectorFactory.connect(url);
server = conn.getMBeanServerConnection();
objectName = new ObjectName("jboss.web:type=Connector,address=*,port=*"); // HTTP/1.1
println server.queryNames(objectName, Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))).iterator().next().getKeyProperty("port");
} finally {
try { conn.close(); println "Connection Closed"; } catch (Exception e) {}
}