Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Tomcat Monitors using Java code

Tags:

java

jmx

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

like image 740
user3635320 Avatar asked Apr 10 '26 06:04

user3635320


1 Answers

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.

like image 114
piet.t Avatar answered Apr 11 '26 19:04

piet.t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!