获取jboss的信息 [英] Obtain information about jboss
问题描述
如何在部署在该 jboss 服务器上的 application/war 中以编程方式找出 jboss 端口?使用 Java
How would one find out the jboss port programatically within application/war that is deployed on that jboss server? Using Java
这是一个运行的网络服务,我们没有任何用户界面
It is a web service running, we don't have any user interface
推荐答案
我假设您想要 HTTP 端口.
I am assuming you want the HTTP port.
JBoss 为每个 Web 侦听器发布一个 Tomcat 连接器 MBean.mbeans 的 ObjectNames 的命名约定是:
JBoss publishes a Tomcat connector MBean for each web listener. The naming convention of the mbeans' ObjectNames is:
- 域:jboss.web
- 属性:
- address:绑定地址
- 端口:监听端口
- 类型:连接器
诀窍是,无需对绑定地址或端口(绑定地址可以是 127.0.0.1、0.0.0.0 或主机名)做任何假设,找到正确的 MBean.为此,您可以使用指定的 JMX 查询:
The trick is, without making any assumptions about the bind address or port (the bind address could be 127.0.0.1, or 0.0.0.0 or a host name), finding the correct MBean. To do this, you can use a JMX Query that specifies:
- 已知域名:jboss.web
- 已知类型:连接器
- 地址的通配符:*****
- 端口的通配符:*****
- 一个属性值表达式,指定您正在查找 HTTP/1.1 协议端口(与 AJP 端口相反):Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))
- The known domain name: jboss.web
- The known type: connector
- A wild card for the address: *****
- A wild card for the port: *****
- An attribute value expression that specifies you are looking for the HTTP/1.1 protocol port (as opposed to the AJP port): Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))
一旦你有一个到 JBoss MBeanServer 的 MBeanServerConnection,这个语句将返回正确的端口:
Once you have an MBeanServerConnection to the JBoss MBeanServer, this statement will return the correct port:
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");
如果您试图通过在 JBoss JVM 中内部运行的代码确定端口,则获取 MBeanServerConnection 是微不足道的,因为您可以静态调用 org.jboss.mx.util.MBeanServerLocator.locateJBoss().
If you are attempting to determine the port from code running inside the JBoss JVM, acquiring the MBeanServerConnection is trivial since you can make a static call to org.jboss.mx.util.MBeanServerLocator.locateJBoss().
下面是一个简单的 JSP 打印 HTTP 端口号的例子:
Here is an example of a simple JSP to print the HTTP port number:
<%@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>
如果需要远程获取,需要使用JBoss客户端RMIAdaptor.参考 就是一个很好的例子,说明了如何做到这一点.
If you need to acquire this remotely, you need to use the JBoss client RMIAdaptor. The reference provided by Nayan Wadekar is a good example of how to do this.
如果您的 JBoss 服务器有一个 org.jboss.mx.remoting.service.JMXConnectorServerService 已部署,或者您正在使用 平台 MBeanServer 运行 JBoss,您也可以使用本机 JMX 远程处理.这是一个 Groovy 示例:
If your JBoss server has a org.jboss.mx.remoting.service.JMXConnectorServerService deployed or you are running JBoss using the platform MBeanServer, you can also use the native JMX remoting. Here's a Groovy example of that:
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) {} }
这篇关于获取jboss的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!