views:

1296

answers:

4

I am running a client/server application using JBoss.

How can I connect to the server JVM's MBeanServer? I want to use the MemoryMX MBean to track the memory consumption.

I can connect to the JBoss MBeanServer using JNDI lookup but the java.lang.MemoryMX MBean is not registered with the JBoss MBeanServer.

EDIT: The requirement is for programmatic access to the memory usage from the client.

+1  A: 

Have you tried launching a JConsole (is $JAVA_HOME/bin) to connect with the server? You should be able to view memory stats from there

oxbow_lakes
Yes, that works. But I want programmatic access from my client application. My client can connect to the JBoss MBean server, but I don't know how to connect to the platform MBean server.
parkr
Apologies - it wasn't clear from your question that programmatic access was a requirement
oxbow_lakes
My apologies :)
parkr
+1  A: 

A code example from an IBM article: link

    MBeanServerConnection serverConn;

try {
   //connect to a remote VM using JMX RMI
   JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://<addr>");

   JMXConnector jmxConnector = JMXConnectorFactory.connect(url);

   serverConn = jmxConnector.getMBeanServerConnection();

   ObjectName objName = new 
   ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);

   // Get standard attribute "VmVendor"
   String vendor = 
   (String) serverConn.getAttribute(objName, "VmVendor");

} catch (...) { }
Shimi Bandiel
+2  A: 

Unlike the JBoss server's MBeanServer, the JVM's MBean server doesn't allow remote monitoring by default. You need to set various system properties to allow that:

http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html

skaffman
+1  A: 

I wrote a class like this:

import javax.management.remote.JMXServiceURL;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;

public class JVMRuntimeClient 
{
    static void main(String[] args) throws Exception 
    {
        if (args == null)
    {
        System.out.println("Usage: java JVMRuntimeClient HOST PORT");
    }
    if(args.length < 2)
    {
        System.out.println("Usage: java JVMRuntimeClient HOST PORT");
    }

    try
    {
        JMXServiceURL target = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+args[0]+":"+args[1]+"/jmxrmi");
        JMXConnector connector = JMXConnectorFactory.connect(target);
        MBeanServerConnection remote = connector.getMBeanServerConnection();

        /**
        * this is the part where you MUST know which MBean to get
        * com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics
        * YOURS WILL VARY!
        */
        ObjectName bean = new ObjectName("com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics");

        MBeanInfo info = remote.getMBeanInfo(bean);
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attr : attributes)
        {
            System.out.println(attr.getDescription() + " " + remote.getAttribute(bean,attr.getName()));
        }
        connector.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
        System.exit(0);
    }
   }
}
Krolique