开发者

Obtain information about jboss

开发者 https://www.devze.com 2023-03-09 05:34 出处:网络
How would one find out the jboss port programatically within application/war that is deployed on that jboss ser开发者_开发知识库ver? Using Java

How would one find out the jboss port programatically within application/war that is deployed on that jboss ser开发者_开发知识库ver? Using Java

It is a web service running, we don't have any user interface


I am assuming you want the HTTP port.

JBoss publishes a Tomcat connector MBean for each web listener. The naming convention of the mbeans' ObjectNames is:

  • Domain: jboss.web
  • Attributes:
    • address: The binding address
    • port: The listening port
    • type: connector

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:

  1. The known domain name: jboss.web
  2. The known type: connector
  3. A wild card for the address: *****
  4. A wild card for the port: *****
  5. 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"))

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");

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().

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>

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.

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) {}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消