Search code examples
jakarta-eeweblogicweblogic12c

WebLogic Introspection/Query domain information from a running servlet


Is there a way, from within a running web app, to query meta information about the application from the domain — specifically, I'd like to be able to detect the port and hostname of WebLogic's admin server dynamically, as it varies from one environment to another.

I'm using WebLogic 12c, but information for over versions would be useful as well. Thanks!


Solution

  • You can query this information from an incoming request, in a Servlet it would work like this:

        @Override
        public void service(final HttpServletRequest req, final HttpServletResponse res)
                throws ServletException, IOException {
    
            System.out.println("Server name " + req.getServerName());
            System.out.println("Server port " + req.getServerPort());
         }
    

    Or you could get it via JMX directly from the domain (no need for a request. See also http://docs.oracle.com/middleware/1212/wls/JMXCU/accesswls.htm#JMXCU144 ):

        private void printHostAndPort() throws Exception {
            final InitialContext ctx = new InitialContext();
    
            final MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("java:comp/env/jmx/domainRuntime");
            final ObjectName runtimeservicebean = new ObjectName(
                    "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
    
            // Here can be several
            final ObjectName[] serverRuntimeMBeans = (ObjectName[]) server.getAttribute(runtimeservicebean,
                    "ServerRuntimes");
    
            final String name = (String) server.getAttribute(serverRuntimeMBeans[0], "Name");
            System.out.println(name);
    
            final String address = (String) server.getAttribute(serverRuntimeMBeans[0], "ListenAddress");
            System.out.println(address);
    
            final Integer port = (Integer) server.getAttribute(serverRuntimeMBeans[0], "ListenPort");
            System.out.println(port);
        }