[JMX一步步来] 8、编写程序来连接MBean

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

 
  基于上一篇为jconsole而修改的例子,给出一个示例的客户端程序,基本的操作都有了:

import java.util.Iterator;
import java.util.Set;
import javax.management.Attribute;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
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("chengang: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", "Chen.Gang"));//设值
        System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));//取值
        //得到proxy代理后直接调用的方式
        HelloMBean proxy = (HelloMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
        proxy.printHello();
        proxy.printHello("陈刚");
        //远程调用的方式
        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();
    }
}
 
运行后的效果如下:
Domains:---------------
 Domain[0] = HelloAgent
 Domain[1] = JMImplementation
 Domain[2] = chengang
MBean count = 3
Name = Chen.Gang
Hello Class: mbean.connector.Hello
Hello Attriber:Name
Hello Operation:printHello
all ObjectName:---------------
 chengang:name=HelloWorld
 JMImplementation:type=MBeanServerDelegate
 HelloAgent:name=htmladapter,port=8082
 
 
它有两个Console输出,这里是另一个
Hello World, Chen.Gang
Hello , 陈刚
Hello World, Chen.Gang
Hello , 子在川上曰
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值