子在川上曰

  逝者如斯夫不舍昼夜
随笔 - 71, 文章 - 0, 评论 - 915, 引用 - 0
数据加载中……

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

文/陈刚  from www.chengang.com.cn at 2005-12-26
 
  前面用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 , 子在川上曰
 

作者简介

陈刚,广西桂林人,著作有《Eclipse从入门到精通》
您可以通过其博客了解更多信息和文章:http://www.chenGang.com.cn

posted on 2006-03-07 15:17 陈刚 阅读(5149) 评论(2)  编辑  收藏 所属分类: JMX

评论

# re: [JMX一步步来] 8、编写程序来连接MBean  回复  更多评论   

在项目中通过用RMI方式连接JMX 的mbeans,如果JDK是1.5的话连接不会出错,可是换到jdk1.4的话会报错,我jmx的5个jar包都在环境里面.
想问下有没有能在jdk1.4中也能正常运行的办法,

付上exception:
java.rmi.ServerError: Error occurred in server thread; nested exception is:
java.lang.NoSuchMethodError: javax.management.MBeanServer.getClassLoaderRepository()Ljavax/management/loading/Cl
assLoaderRepository;
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:289)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)


2006-12-19 17:11 | jmx初学者

# re: [JMX一步步来] 8、编写程序来连接MBean[未登录]  回复  更多评论   

Refer to http://forum.java.sun.com/thread.jspa?threadID=5229139
2008-05-15 09:58 | Tim

只有注册用户登录后才能发表评论。


网站导航: