2010-03-16 71 views

回答

3

你可以很容易地看到這個。

  • 步驟1:下載JConsole
  • 步驟2:啓動一個Java程序(Java 5或更高版本)
  • 步驟3:連接到Java過程與JConsole的
  • 步驟4:查看的MBean爲觸發堆轉儲事件,垃圾收集請求,線程信息,裝載的類等

請告訴我特別有趣的是,你可以編寫代碼來訪問一個運行Java程序的MBean:

訪問管理界面有三種不同的方式。在同一個Java虛擬機內直接調用 MXBean中的方法 。

RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); 

    // Get the standard attribute "VmVendor" String vendor = mxbean.getVmVendor(); 

經過的MBeanServerConnection 連接到平臺上運行的虛擬機的MBeanServer的 。

MBeanServerConnection mbs; 

    // Connect to a running JVM (or itself) and get MBeanServerConnection // that has the JVM MXBeans registered in it ... 

    try { 
     // Assuming the RuntimeMXBean has been registered in mbs 
     ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME); 

     // Get standard attribute "VmVendor" 
     String vendor = (String) mbs.getAttribute(oname, "VmVendor"); } catch (....) { 
     // Catch the exceptions thrown by ObjectName constructor 
     // and MBeanServer.getAttribute method 
     ... } 

使用MXBean代理。

MBeanServerConnection mbs; 

    // Connect to a running JVM (or itself) and get MBeanServerConnection // that has the JVM MBeans registered in it ... 

    // Get a MBean proxy for RuntimeMXBean interface RuntimeMXBean proxy = 
     ManagementFactory.newPlatformMXBeanProxy(mbs, 
               ManagementFactory.RUNTIME_MXBEAN_NAME, 
               RuntimeMXBean.class); // Get standard attribute "VmVendor"  String vendor = proxy.getVmVendor(); 

又見The Java Language Management API