Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

java - Accessing a remote MBean server

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
    }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...