2012-08-07 56 views
0

如果我知道MBean的名稱,但是我試圖監視的應用程序生成隨機命名的MBean,並且因此需要能夠獲取屬性信息向JMX查詢在域中註冊的MBean列表。使用Jython提取在JMX中的域中註冊的MBean列表

以下是JConsole中的視圖示例,以及我試圖動態獲取的示例。

JConsole Screenshot

下面是我寫的代碼,這是能夠拉屬性,註冊的域名,但我似乎無法擺脫的MBean的實際列表中的一個域。

import javax.management.remote.JMXConnector; 
    import javax.management.remote.JMXConnectorFactory; 
    import javax.management.remote.JMXServiceURL; 
    import java.lang.management.ManagementFactory; 

    import sys, cmd, socket, optparse 
    from urlparse import urljoin 
    from cmd import Cmd 

    def get_connection(host, port): 
     serviceURL = str() 
     serviceURL = "service:jmx:rmi:///jndi/rmi://" 
     serviceURL = serviceURL + host + ":" + str(port) + "/jmxrmi" 

     url = javax.management.remote.JMXServiceURL(serviceURL); 
     connector = javax.management.remote.JMXConnectorFactory.connect(url); 
     global remote 
     remote = connector.getMBeanServerConnection(); 


    def get_domain_list(): 
     domainList = [] 
     get_connection('dt2apps2102', 8004) 
     domainList = remote.getDomains() 
     return domainList 

    def print_domain_list(): 
     domainList = get_domain_list() 
     for element in domainList: 
      print element 

    def get_domain_objects(domain): 
     pass 

    def get_object_list(): 
     dmiDomains = {} 
     domainList = get_domain_list() 
     for element in domainList: 
      if element in ('SystemJobControl', 'MediaStore', 'fabric'): 
       dmiDomains[element] = None 
     return dmiDomains 

    def get_attribute(domain, mbean, attribute): 
     obn = javax.management.ObjectName(domain + ':' + 'name=' + mbean) 
     return remote.getAttribute(obn, attribute) 

    print get_object_list() 
    get_object_list() 
+0

打印get_attribute( 'SystemJobControl',「聽衆','ListenerEventsProcessed') >>> 294L – casibbald 2012-08-07 15:45:30

回答

5

這應該給你的所有對象名稱:

remote.queryNames(new ObjectName(domain + ":*"), null)

,這將獲得實際的MBean:

remote.queryMBeans(new ObjectName(domain + ":*"), null)