Note that there are some explanatory texts on larger screens.

plurals
  1. POTomcat JMX - Connecting to server but cant find the MBean i want
    text
    copied!<p>I am trying to write a client utility that to connect to Tomcat via JMX and look at the status of the connection datasource. I set the following VM arguments in $CATALINA_HOME/bin/setenv.bat and restarted Tomcat</p> <p>set JAVA_OPTS=-Xms512M -Xmx1024M -XX:MaxPermSize=256M %JAVA_OPTS% set CATALINA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false %CATALINA_OPTS%</p> <p>I am not very familiar with JMX so i am just having a play with it to get the feel of it. The utility i am writing will be running outside of Tomcat. I wrote the following test to try and access datasource Mbean object in Tomcat but for some reason it is not finding it. </p> <pre><code> public class GuiMonitor { public static void main(String[] args){ try{ JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:9004/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); final List&lt;MBeanServer&gt; servers = new LinkedList&lt;MBeanServer&gt;(); servers.add(ManagementFactory.getPlatformMBeanServer()); servers.addAll(MBeanServerFactory.findMBeanServer(null)); System.out.println("MbeanServers " + servers.size()); for(final MBeanServer server : servers){ System.out.println("Server : " + server.getClass().getName()); } MBeanServer mbsc = ManagementFactory.getPlatformMBeanServer(); System.out.println(mbsc.queryMBeans(null, null)); ObjectName on = new ObjectName("Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\""); System.out.println("ObjectName : " + on.toString()); System.out.println(mbsc.getAttribute(on, "Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\"")); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>I have a JSP page that i found on the internet which when i upload onto the webapps folder and run it, it displays all of the available MBeans in Tomcat. The object string/name i used above came from the name that was reported on both the jsp page i used and Jconsole so it does exist. </p> <p>The output to the above program is shown below</p> <pre><code> MbeanServers 2 Server : com.sun.jmx.mbeanserver.JmxMBeanServer Server : com.sun.jmx.mbeanserver.JmxMBeanServer [com.sun.management.OperatingSystem[java.lang:type=OperatingSystem], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Tenured Gen], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Perm Gen], java.util.logging.Logging[java.util.logging:type=Logging], sun.management.CompilationImpl[java.lang:type=Compilation], javax.management.MBeanServerDelegate[JMImplementation:type=MBeanServerDelegate], sun.management.MemoryImpl[java.lang:type=Memory], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Survivor Space], sun.management.RuntimeImpl[java.lang:type=Runtime], sun.management.GarbageCollectorImpl[java.lang:type=GarbageCollector,name=Copy], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Eden Space], sun.management.GarbageCollectorImpl[java.lang:type=GarbageCollector,name=MarkSweepCompact], sun.management.ThreadImpl[java.lang:type=Threading], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Perm Gen [shared-ro]], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Perm Gen [shared-rw]], sun.management.HotSpotDiagnostic[com.sun.management:type=HotSpotDiagnostic], sun.management.ClassLoadingImpl[java.lang:type=ClassLoading], sun.management.MemoryManagerImpl[java.lang:type=MemoryManager,name=CodeCacheManager], sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Code Cache]] ObjectName : Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name="jdbc/appdb" javax.management.InstanceNotFoundException: Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name="jdbc/appdb" at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:662) at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:638) at com.bt.c21.c21mon.C21GuiMonitor.main(C21GuiMonitor.java:39) </code></pre> <p>A couple of questions</p> <ul> <li>Is the URL correct? I know the port number is correct but i am not sure of the service name. The service name "jmxrmi" i am using on the URL is just one that i saw in one of the examples i have been looking at. </li> </ul> <p>I have a feeling that this is connecting to a different MBeanServer. I suspect this because if you look at the output of mbsc.queryMBeans(null, null), there is nothing tomcat specific. What service name do i use for the Tomcat instance?</p> <ul> <li><p>If the URL is correct then is the service name always jmxrmi? And why does it not find the "Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\"" entry?</p></li> <li><p>I have seen a lot of examples of how to do this and most use a different method to get teh MbeanServer. A couple of examples i have seen are </p> <p>ManagementFactory.getPlatformMBeanServer() MBeanServerFactory.findMBeanServer(null) getMBeanServerConnection() </p></li> <li><p>As mentioned earlier, the utility i am writing is a normal java application that will be running outside of tomcat. Is there any other configuration that i have missed out? I have been looking at several examples and the majority talk about creating MBeans and there is usually references to Listeners. As i am not creating any new Mbeans but only reading the values of existing ones, do i need to configure a listener? </p></li> </ul> <h2>Edit</h2> <p>It seems that getPlatformMbeanServer() is not returning the correct JVM Instance. I tried the following</p> <pre><code>MBeanServerConnection conn = jmxc.getMBeanServerConnection(); System.out.println("Query2 : " + conn.queryMBeans(null, null)); </code></pre> <p>And this does return some Tomcat specific values. But i am still unable to get the jdbc/appdb datasource. </p> <p>krtek - I wont be able to use JMX Console as i plan to do it all manually with the intention of automating it. </p> <h2>Edit 2</h2> <p>Ok, i figured out what i was doing wrong. Initially i was trying to retrieve the values as </p> <pre><code>MBeanServerConnection conn = jmxc.getMBeanServerConnection(); ObjectName on = new ObjectName("Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\""); mbsc.getAttribute(on, "Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\"")); </code></pre> <p>The above is wrong because the second parameter for mbsc.getAttribute is supposed to be the attribute in the Mbean not the String name. </p> <p>This gave me the correct attribute values</p> <pre><code>MBeanServerConnection conn = jmxc.getMBeanServerConnection(); ObjectName on = new ObjectName("Catalina:type=DataSource,path=/appdb,host=localhost,class=javax.sql.DataSource,name=\"jdbc/appdb\""); mbsc.getAttribute(on, "numIdle") </code></pre> <p>And i also changed the MBeanServer i was using from getPlatformMbeanServer() to getMBeanserverConnection(). I must admit i dont quite understand the difference because since Tomcat is running on the same JVM as the one returned by getPlatformMbeanServer(). Does it mean that getPlatformMbeanServer() will only return sun specific Mbeans? and getMBeanserverConnection() will include both? </p> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload