【JMX】7. 连接MBean:Java代码

前面用Html、jconsole等方法连接上了MBeanServer,并能够通过这些界面来操纵MBean。但有时我们需要不借助这些客户端,而是在自己的程序来操纵这些MBean,这就要求我们知道如何在代码里连接MBean。

客户端代码如下:

public class Client {   
    public static void main(String[] args) throws Exception {   
   

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");      
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);       
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();     
        ObjectName mbeanName = new ObjectName("alpha:name=HelloWorld");   

        
        // 把所有Domain都打印出来   
        System.out.println("Domains:---------------");      
        String domains[] = mbsc.getDomains();       
        for (int i = 0; i < domains.length; i++) {       
            System.out.println("\tDomain[" + i + "] = " + domains[i]);    
        }   
        // MBean的总数   
        System.out.println("MBean count = " + mbsc.getMBeanCount());   
        // 对name属性的操作(==============属性名的第一个字母要大写==============)   
        mbsc.setAttribute(mbeanName, new Attribute("Name", "PANDA"));// 设值   
        System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));// 取值   
        
        // 得到proxy代理后直接调用的方式   
        HelloMBean proxy = MBeanServerInvocationHandler.
        		newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);        
        proxy.printHello();        
        proxy.printHello("Raymend");          
        // 远程调用的方式   
        mbsc.invoke(mbeanName, "printHello", null, null);         
        mbsc.invoke(mbeanName, "printHello", new Object[] { "熊猫烧香" }, new String[] { String.class.getName() });   
 
        // 得mbean的信息   
        MBeanInfo info = mbsc.getMBeanInfo(mbeanName);         
        System.out.println("Hello Class: " + info.getClassName());      
        System.out.println("Hello Attriber:" + info.getAttributes()[0].getName());     
        System.out.println("Hello Operation:" + info.getOperations()[0].getName());   
        // 得到所有的MBean的ObjectName   
        System.out.println("all ObjectName:---------------");        
        Set set = mbsc.queryMBeans(null, null);       
        for (Iterator it = set.iterator(); it.hasNext();) {        
            ObjectInstance oi = (ObjectInstance) it.next();        
            System.out.println("\t" + oi.getObjectName());        
            }   
        // 注销   
        // mbsc.unregisterMBean(mbeanName);   
        // 关闭MBeanServer连接   
        jmxc.close();     
}   
}   


运行上一篇中的HelloAgentRmi


Client控制台信息:

Domains:---------------
	Domain[0] = JMImplementation
	Domain[1] = HelloAgent
	Domain[2] = alpha
MBean count = 3
Name = PANDA
Hello Class: no6_jmx.jconsole.Hello
Hello Attriber:Name
Hello Operation:printHello
all ObjectName:---------------
	JMImplementation:type=MBeanServerDelegate
	HelloAgent:name=htmladapter,port=8082
	alpha:name=HelloWorld

Agent控制台信息:

start.....
rmi start.....
Hello World, PANDA
Hello , Raymend
Hello World, PANDA
Hello , 熊猫烧香


你可能感兴趣的:(jmx)