Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain information about jboss

Tags:

java

jboss

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

like image 347
Gandalf StormCrow Avatar asked May 27 '11 14:05

Gandalf StormCrow


People also ask

What is JBoss and why it is used?

JBoss is a division of Red Hat that provides support for the WildFly open source Application Server program (formerly called JBoss AS) and related middleware services. JBoss is an open source alternative to commercial offerings from IBM WebSphere and SAP NetWeaver.

How do I know my JBoss version?

When the JBoss Enterprise Application Platform server is running you can retrieve its version information from the first page of the Web Console. This is located at http://localhost:8080/web-console/.

What is JBoss called now?

We've Moved! The JBoss AS community project has been renamed to the WildFly community project, which has a new home at wildfly.org. The JBoss name now only applies to the commercially supported product, called JBoss EAP, which is derived from the WildFly community project and is available here.

Is JBoss a application server?

JBoss Application Server (JBoss AS) is an open-source, cross-platform Java application server developed by JBoss, a division of Red Hat Inc. JBoss AS is an open-source implementation of Java 2 Enterprise Edition (J2EE) that is used for implementing Java applications and other Web-based applications and software.


1 Answers

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) {}
}
like image 76
Nicholas Avatar answered Nov 10 '22 09:11

Nicholas