3
votes

I am very new to JMX. I am trying to log the tomcat statistics like threads used, cache, sessions and other standard values. I am trying to achieve this with java code.

I have done the following things as of now. (I am trying to access the values of a local tomcat 6.0 monitor on windows)

1)I have added the following options in the catalina.bat set CATALINA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

After that I restarted the tomcat server.

2) Then I wrote the following code.

package com.ss.fg;

import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

public class SystemConfigManagement
{
    static MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
    public static void main(String[] args) throws Exception {
        getActiveSession();    
    }  
    public static void getActiveSession()throws Exception
    {
        ObjectName name=new ObjectName("Catalina:type=Manager,path=/MMDisplay,host=localhost");
        String attrValue = ManagementFactory.getPlatformMBeanServer().getAttribute(name, "activeSessions").toString();
        System.out.println(attrValue);

    }
}

I even tried context instead of path.

I am getting the following exception

Exception in thread "main" javax.management.InstanceNotFoundException: Catalina:type=Manager,path=/MMDisplay,host=localhost
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(Unknown Source)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(Unknown Source)
    at com.softsmith.floodgates.SystemConfigManagement.getActiveSession(SystemConfigManagement.java:15)
    at com.softsmith.floodgates.SystemConfigManagement.main(SystemConfigManagement.java:10)

How can I resolve this issue?

Should I add Some jar files, or should I do some other settings..

Please help

2
I have error like this : Exception in thread "main" javax.management.InstanceNotFoundException: Catalina:type=Manager,path=/,host=localhost - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

2 Answers

1
votes

When using

 MBeanServer connection = ManagementFactory.getPlatformMBeanServer();

you are actually connectiong to the MBean-Server of the JVM running your program, not to that of your tomcat-instance, so it doesn't know of the Catalina-MBeans.

To establish a connection to a remote jvm try something like:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://servername:9999/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(url);
connector.connect();
MBeanServerConnection server = connector.getMBeanServerConnection();
//do work here
ObjectName name = new ObjectName("Catalina:type=Manager,path=/manager,host=localhost");
String attrValue = mb.getAttribute(name, "activeSessions").toString();
System.out.println(attrValue);
//..and don't forget to close the connection
connector.close();

If the error is still there make sure you are using the correct object-name.

1
votes

When i using

 MBeanServer connection = ManagementFactory.getPlatformMBeanServer();

i am actually connectiong to the MBean-Server of the JVM running my program, not to that of my tomcat-instance, so it doesn't know of the Catalina-MBeans. so i try to connected remotly to jmx but have error like this :

java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is....

and now i solve :

for first i added catalina-jmx-remote.jar to TOMCAT_HOME/lib directory of Tomcat and then configure the listener on server.xml i added the following snippet under the tag :

<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="10001" rmiServerPortPlatform="10002" useLocalPorts="true" />

and finally i set the following sentence in my catalina.bat :

JAVA_OPTS="$JAVA_OPTS -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"