访问远程 MBean 服务器

发布于 2024-07-30 11:12:44 字数 235 浏览 15 评论 0原文

我正在使用 JBoss 运行客户端/服务器应用程序。

如何连接到服务器 JVM 的 MBeanServer? 我想使用 MemoryMX MBean 来跟踪内存消耗。

我可以使用 JNDI 查找连接到 JBoss MBeanServer,但 java.lang.MemoryMX MBean 未在 JBoss MBeanServer 中注册。

编辑:要求是从客户端以编程方式访问内存使用情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见 2024-08-06 11:12:44

我写了一个这样的类:

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

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);
    }
   }
}
情栀口红 2024-08-06 11:12:44

与 JBoss 服务器的 MBeanServer 不同,JVM 的 MBean 服务器默认不允许远程监控。 您需要设置各种系统属性以允许:

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

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

属性 2024-08-06 11:12:44

IBM 文章中的代码示例:链接

    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 (...) { }

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 (...) { }
断肠人 2024-08-06 11:12:44

您是否尝试过启动 JConsole (是 $JAVA_HOME/bin)来连接服务器? 您应该能够从那里查看内存统计信息

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

述情 2024-08-06 11:12:44

以下代码列出了给定(启用 jmx 的)java 应用程序的所有 mbean 及其按域分组的属性和操作。 只需启动您想要使用固定 jmx 端口监控的 java 应用程序,例如使用以下 vm 参数:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9000
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

然后运行这个 main:

import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;

public class JmxListAll {

    public static void main(String[] args) throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException {

        /*
         1. JMXServiceURL.
        */
        String jmxHost = "localhost:9000"; // exactly like  jconsole localhost:9026
        String url = "service:jmx:rmi:///jndi/rmi://" + jmxHost + "/jmxrmi";
        JMXServiceURL serviceURL = new JMXServiceURL(url);

        /*
         2. JMXConnector and the actual serverConnection
         */
        JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
        MBeanServerConnection serverConnection = connector.getMBeanServerConnection();

        /*
         3. Walk through the domains and their objects
         */
        System.out.println("\n     Now we have a look at " + serverConnection.getMBeanCount() + " mbeans!");
        int objectCount = 0;
        for (String domain : serverConnection.getDomains()) {
            System.out.println("\n***********************************************************************************");
            System.out.println("DOMAIN: " + domain);

            // query all the beans for this domain using a wildcard filter
            for (ObjectName objectName : serverConnection.queryNames(new ObjectName(domain + ":*"), null)) {
                System.out.println("    objectName " + ++objectCount + ": " + objectName);
                MBeanInfo info = serverConnection.getMBeanInfo(objectName);
                for (MBeanAttributeInfo attr : info.getAttributes()) {
                    System.out.print("        attr: " + attr.getDescription());
                    try {
                        String val = serverConnection.getAttribute(objectName, attr.getName()).toString();
                        System.out.println(" -> " + abbreviate(val));
                    } catch (Exception e) {
                        System.out.println(" FAILED: " + e);
                    }
                }

                for (MBeanOperationInfo op : info.getOperations()) {
                    System.out.println("        op: " + op.getName());
                }
            }
        }
    }

    static String abbreviate(String text) {
        if (text != null && text.length() > 42) {
            return text.substring(0, 42) + "...";
        } else {
            return text;
        }
    }
}

正如您应该看到的,在 java.lang 域中有几个与内存相关的 mbean。 选择您需要的那个。

The following code lists all mbeans of a given (jmx enabled) java application with their attributes and operations grouped by the domain. Just start the java app you wanna monitor with a fixed jmx port, e.g. by using these vm parameters:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9000
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Then run this main:

import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;

public class JmxListAll {

    public static void main(String[] args) throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException {

        /*
         1. JMXServiceURL.
        */
        String jmxHost = "localhost:9000"; // exactly like  jconsole localhost:9026
        String url = "service:jmx:rmi:///jndi/rmi://" + jmxHost + "/jmxrmi";
        JMXServiceURL serviceURL = new JMXServiceURL(url);

        /*
         2. JMXConnector and the actual serverConnection
         */
        JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
        MBeanServerConnection serverConnection = connector.getMBeanServerConnection();

        /*
         3. Walk through the domains and their objects
         */
        System.out.println("\n     Now we have a look at " + serverConnection.getMBeanCount() + " mbeans!");
        int objectCount = 0;
        for (String domain : serverConnection.getDomains()) {
            System.out.println("\n***********************************************************************************");
            System.out.println("DOMAIN: " + domain);

            // query all the beans for this domain using a wildcard filter
            for (ObjectName objectName : serverConnection.queryNames(new ObjectName(domain + ":*"), null)) {
                System.out.println("    objectName " + ++objectCount + ": " + objectName);
                MBeanInfo info = serverConnection.getMBeanInfo(objectName);
                for (MBeanAttributeInfo attr : info.getAttributes()) {
                    System.out.print("        attr: " + attr.getDescription());
                    try {
                        String val = serverConnection.getAttribute(objectName, attr.getName()).toString();
                        System.out.println(" -> " + abbreviate(val));
                    } catch (Exception e) {
                        System.out.println(" FAILED: " + e);
                    }
                }

                for (MBeanOperationInfo op : info.getOperations()) {
                    System.out.println("        op: " + op.getName());
                }
            }
        }
    }

    static String abbreviate(String text) {
        if (text != null && text.length() > 42) {
            return text.substring(0, 42) + "...";
        } else {
            return text;
        }
    }
}

As you should see, in the java.lang domain are several memory related mbeans. Pick the one you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文