如果我知道 MBean 的名称,我可以获取属性信息,但是我尝试监控的应用程序会生成随机命名的 MBean,因此需要能够查询 JMX 以获取在域中注册的 MBean 列表。
这是 JConsole 中的视图示例,以及我试图动态获取的内容。
下面是我编写的代码,它能够提取属性、注册的域,但我似乎无法得到域中 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()