JMX、MXBean学习

package jmx;

/**
 * 接口命名规则:xxxMBean
 * 实现类:xxx
 */
public interface HelloMBean {

	public void setName(String name);

	public String getName();

	public void print();

	public void addName();
}

package jmx;

public class Hello implements HelloMBean {
	private String name = "";

	public Hello() {

	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void print() {
		System.out.println("Hello, " + name + "!");
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void addName() {
		this.name = "zml";

	}

}

package jmx;

import java.rmi.registry.LocateRegistry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class JmxBeanDemo {

	/**
	 * localhost:9998
	 * -Dcom.sun.management.jmxremote.port=9998
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false
	 */
	public static void main(String[] args)
			throws InstanceAlreadyExistsException, MBeanRegistrationException,
			NotCompliantMBeanException, MalformedObjectNameException,
			NullPointerException, InterruptedException, IntrospectionException, InstanceNotFoundException, ReflectionException {
//		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
//		
//		ObjectName objectName = new ObjectName("jmx:type=hello");
//
//		server.registerMBean(new Hello(), objectName);
//
//		MBeanInfo meanInfo = server.getMBeanInfo(objectName);
		
		try {
            String DOMAIN = "localhost";
            MBeanServer server = MBeanServerFactory.createMBeanServer(DOMAIN);            
            server.registerMBean(new Hello(), new ObjectName("jmx:type=hello"));
            JMXServiceURL url = new JMXServiceURL("rmi", null, 9998, "/jndi/rmi://localhost:" + 1099 + "/zml");
            // start()和stop()来启动和停止 JMXConnectorServer
            
          //得到存储jmx用户信息的文件
//            String userFile =context.getRealPath("/")+"/Web-INF/classes/"+Constants.JMX_USERS_FILE;
//            //创建authenticator并且初始化RMI服务器
//            Map<string> env = new HashMap<string>();
//            env.put("jmx.remote.x.password.file", userFile);
//            env = null;
            JMXConnectorServer jmxServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
            //service:jmx:rmi://localhost:9589/jndi/rmi://localhost:1099/zml
            //service:jmx:rmi:///jndi/rmi://localhost:1099/zml
            System.out.println(url);
            // 在RMI上注册
            LocateRegistry.createRegistry(1099);

            jmxServer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
		
		Thread.sleep(Long.MAX_VALUE);

	}

}

package jmx;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMXProxyTest {
	public static void main(String[] args) {
		JMXWrapper jmxWrapper = new JMXWrapper(HelloMBean.class,
				"jmx:type=hello");
		HelloMBean mbean = (HelloMBean) jmxWrapper.getProxy();
		mbean.addName();
		mbean.print();
	}

}

class JMXInvokerHandler implements InvocationHandler {
	private String name;

	private MBeanServerConnection mbsc = null;
	
	public JMXInvokerHandler(String name) {
		this.name = name;
	}

	private MBeanServerConnection getMBeanServerConnection() {
		if(null == mbsc){
			JMXServiceURL url = null;
			JMXConnector jmxc = null;
			try {
				url = new JMXServiceURL(
						"service:jmx:rmi://localhost/jndi/rmi://localhost:1099/zml");
				jmxc = JMXConnectorFactory.connect(url, null);
				mbsc = jmxc.getMBeanServerConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
		return mbsc;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		MBeanServerConnection connection = getMBeanServerConnection();
		ObjectName objectName = new ObjectName(name);
		String operationName = method.getName();
		Class classes[] = method.getParameterTypes();
		String[] signature = new String[classes.length];
		for (int i = 0; i < signature.length; i++) {
			signature[i] = classes[i].getName();
		}
		Object object = connection.invoke(objectName, operationName, args,
				signature);
		return object;
	}
}

class JMXWrapper {
	private Class fInterface;
	private String name;
	private JMXInvokerHandler handler;

	public JMXWrapper(Class interfaces, String name) {
		this.fInterface = interfaces;
		this.name = name;
	}

	public Object getProxy() {
		handler = new JMXInvokerHandler(name);
		Object object = Proxy.newProxyInstance(fInterface.getClassLoader(),
				new Class[] { fInterface }, handler);
		return object;
	}

}

你可能感兴趣的:(java,jmx,mxbean)