2009-08-13 218 views
8

我正在使用JBoss運行客戶端/服務器應用程序。訪問遠程MBean服務器

如何連接到服務器JVM的MBeanServer?我想使用MemoryMX MBean來跟蹤內存消耗。

我可以使用JNDI查找連接到JBoss MBeanServer,但java.lang.MemoryMX MBean未註冊到JBoss MBeanServer。

編輯:需求是編程訪問從客戶端的內存使用情況。

回答

1

您是否嘗試啓動JConsole(即$JAVA_HOME/bin)與服務器連接?您應該能夠從那裏

+0

沒錯,就是工作。但我想從我的客戶端應用程序進行編程訪問。我的客戶端可以連接到JBoss MBean服務器,但我不知道如何連接到平臺MBean服務器。 – parkr 2009-08-13 07:00:07

+0

道歉 - 從您的問題中不清楚,程序訪問是一項要求 – 2009-08-13 07:26:44

+1

我的歉意:) – parkr 2009-08-13 07:47:58

4

一個代碼示例查看內存統計從IBM文章: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 (...) { } 
15

我寫了一個類是這樣的:

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

只需要解決JMX服務URL *我的問題 - 謝謝! – 2013-03-22 17:10:55