获取有关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
这是一个正在运行的Web服务,我们没有任何用户界面
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
- 属性:
- 地址:绑定地址
- 端口:监听端口
- 类型:连接器
- Domain: jboss.web
- Attributes:
- address: The binding address
- port: The listening port
- type: connector
诀窍在于,无需对绑定地址或端口做任何假设(绑定地址可以是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客户端参考(由Nayan Wadekar提供)是如何做到这一点的一个很好的例子.
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屋!