JMX(七)编写程序来连接MBean

 

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

 

 

1.HelloMBean

 

package jmx.mbean;

/* HelloMBean.java - MBean interface describing the management  
 operations and attributes for the Hello World MBean.  In this case  
 there are two operations, "sayHello" and "add", and two attributes,  
 "Name" and "CacheSize". */
/**
 * HelloMBean定义方法及属性由Hello实现
 */
public interface HelloMBean {
	public String getName();

	public void setName(String name);

	public void printHello();

	public void printHello(String whoName);
}

 

 2.Hello

 

package jmx.mbean;

public class Hello implements HelloMBean{
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void printHello() {
        System.out.println("Hello World, " + name);
    }
    public void printHello(String whoName) {
        System.out.println("Hello , " + whoName);
    }
} 

 

 3.HelloAgent

 

package jmx.mbean;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent {
	public static void main(String[] args) throws Exception {
		MBeanServer server = MBeanServerFactory.createMBeanServer();
        ObjectName helloName = new ObjectName("jmx:name=HelloWorld");
        Hello hello = new Hello();
        server.registerMBean(hello, helloName);
        ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
        HtmlAdaptorServer adapter = new HtmlAdaptorServer();
        server.registerMBean(adapter, adapterName);
        adapter.start();
        System.out.println("start.....");         
        //创建rmi连接并开启
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
        cs.start();
        System.out.println("rmi start.....");
    }

}

 

 4.Client

 

package jmx.mbean;

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("jmx: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", "yuwen"));// 设值
		System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));// 取值
		// 得到proxy代理后直接调用的方式
		HelloMBean proxy = (HelloMBean) MBeanServerInvocationHandler
				.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
		proxy.printHello();
		proxy.printHello("yuwen");
		// 远程调用的方式
		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();
	}
}

 

 5.运行

(1)在Dos运行一个命令:rmiregistry 9999

(2)运行HelloAgent

(3)运行Client

  运行结果有两个

 HelloAgent控制台

 

Hello World, yuwen

Hello , yuwen

Hello World, yuwen

Hello , 你好

------------------------------------

Client控制台

 

Domains:---------------

Domain[0] = JMImplementation

Domain[1] = jmx

Domain[2] = HelloAgent

MBean count = 3

Name = yuwen

Hello Class: jmx.mbean.Hello

Hello Attriber:Name

Hello Operation:printHello

all ObjectName:---------------

JMImplementation:type=MBeanServerDelegate

HelloAgent:name=htmladapter,port=8082

jmx:name=HelloWorld


 

 

本人开了个充值淘宝网店。有需要的朋友请访问的店铺并拍下所充值的话费,

本店已加入消费保障服务计划,货源来源于淘宝充值平台,安全可靠便捷,

支付过后立即到账

JMX(七)编写程序来连接MBean

http://xiaowen168.taobao.com

 

 

 

你可能感兴趣的:(Mbean)