If I'm inside JBoss, how can I retrieve the MBeanServer directly, without using the RMI/JNDI lookup mentioned in the JBoss docs?
Naive Method
Retrieve the list of available MBeanServers and pick the use the first in the list. Note that this method works OK with Java VM's where JBoss is likely to be the first (and often only) MBeanServer. Other VM's, such as BEA's JRockit, will likely have more than one MBeanServer in the list and JBoss is unlikely to be the first. Note that you can still check the class types / names of the returned MBeanServer(s) to find JBoss by brute force.
MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
Direct lookup using JBoss-specific class(es).
More reliably you can use the JBoss code:
MBeanServer server = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
If you extend org.jboss.system.ServiceMBeanSupport just use the method getServer().
-
My client is behind a firewall, only port 8080 (HTTP) is opened, how can I access the MBeanServer?
You need to expose the jmx invoker service interface via HTTP
/deploy/jmx-invoker-service.xml
<!-- Expose the jmx invoker service interface via HTTP --> <mbean code="org.jboss.invocation.http.server.HttpProxyFactory" name="jboss.jmx:type=adaptor,name=Invoker,protocol=http,service=proxyFactory"> <attribute name="InvokerURL">http://localhost:8080/invoker/JMXInvokerServlet</attribute> <!-- The target MBean is the InvokerAdaptorService configured below --> <depends optional-attribute-name="InvokerName">jboss.jmx:type=adaptor,name=Invoker</depends> <attribute name="ExportedInterface">org.jboss.jmx.adaptor.rmi.RMIAdaptorExt</attribute> <attribute name="JndiName">jmx/invoker/HttpAdaptor</attribute> <attribute name="ClientInterceptors"> <interceptors> <interceptor>org.jboss.proxy.ClientMethodInterceptor</interceptor> <interceptor>org.jboss.proxy.SecurityInterceptor</interceptor> <interceptor>org.jboss.jmx.connector.invoker.client.InvokerAdaptorClientInterceptor</interceptor> <interceptor>org.jboss.invocation.InvokerInterceptor</interceptor> </interceptors> </attribute> </mbean>
In the client code you need to lookup the HttpAdaptor bound at jmx/invoker/HttpAdaptor
InitialContext ctx = new InitialContext(); // From jndi.properties MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/HttpAdaptor"); System.out.println("Version = " + (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("Version"))); Note: The url provider should point to the JNDIFactory servlet that exposes the JBoss JNDI Naming service stub through http jndi.properties java.naming.factory.initial=org.jboss.naming.HttpNamingContextFactory java.naming.provider.url=http://localhost:8080/invoker/JNDIFactory java.naming.factory.url.pkgs=org.jboss.naming.client
Although it is not good practice, this can also be done programmatically :
Hashtable table = new Hashtable(); table.put("java.naming.factory.initial","org.jboss.naming.HttpNamingContextFactory"); table.put("java.naming.provider.url","http://localhost:8080/invoker/JNDIFactory"); table.put("java.naming.factory.url.pkgs","org.jboss.naming.client"); InitialContext ctx = new InitialContext(table); // From table MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/HttpAdaptor"); System.out.println("Version = " + (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("Version")));
Comments