JMX的一个链接类

package com.pachira.oamp.jmxServer;

import java.util.HashMap;
import java.util.Map;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.cxf.common.util.StringUtils;
import com.pachira.oamp.utils.ConfigUtils;
import com.pachira.utils.SrvLogger;

public class ClientMBServerManage {
	private static MBeanServer server=null;
	private static ClientMBServerManage instance=null;
	private static MBeanServerConnection mbsc=null;

	private ClientMBServerManage(){
		server=getLocalMBServer();
		mbsc=this.getRemoteMBConn();
	}

	public static ClientMBServerManage getInstance(){
		if(instance==null){
			 synchronized (ClientMBServerManage.class) {
		          if (instance == null) {
		        	  instance = new ClientMBServerManage();
		          }
             }
		}
		return instance;
	}

	/**
	 * 注册MBean
	 * @param obj
	 * @param className
	 * @param name
	 */
	public void register(Object obj,String className,String name){
		try {
			ObjectName objectName = getObjectName(name);
			this.registerLocal(obj, objectName);
			this.registerRemote(className, objectName);
		} catch(Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to register MBean", e);
		}
	}

	/**
	 * 根据名称注销远程的MBean
	 *
	 * @param name
	 */
	public void unRegisterRemote(String name){
		try {
			if(mbsc==null){
				return ;
			}
			if(StringUtils.isEmpty(name)){
				name="*";
			}
			ObjectName objectName =getObjectName(name);
			 for (ObjectInstance object : mbsc.queryMBeans(objectName, null)) {//服务器上的所有MBean
				 ObjectName on=object.getObjectName();
				 if(mbsc.isRegistered(on)){
					mbsc.unregisterMBean(on);
				 }
			 }
		} catch(Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to register MBean", e);
		}
	}

	/**
	 * 获取远程的对象
	 * @param name
	 * @param cla
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object getRemoteMBean(String name,Class cla){
		 ObjectName objectName = getObjectName(name);
		 try {
			if(mbsc!=null&&objectName!=null&&mbsc.isRegistered(objectName)){
				return JMX.newMBeanProxy(mbsc, objectName,cla);
			 }
		} catch (Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to getRemoteMBean", e);
		}
		return null;
	}

	/**
	 * 执行远程方法
	 * @param name
	 * @param operationName
	 * @param params
	 * @param signature
	 */
	public void invokeRemote(String name,String operationName,Object[] params,String[] signature){
		try {
			if(mbsc==null){
				return ;
			}
			ObjectName objectName=this.getObjectName(name);
			if(StringUtils.isEmpty(name)||StringUtils.isEmpty(operationName)||objectName==null){
				return ;
			}
			mbsc.invoke(objectName, operationName, params, signature);
		} catch (Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to invokeRemote", e);
		}
	}

	/**
	 * 本地注册MBean,没有则创建
	 * @param obj
	 * @param objectName
	 */
	private void registerLocal(Object obj,ObjectName objectName){
		if(obj==null||objectName==null){
			return ;
		}
		try {
			if(!server.isRegistered(objectName)){
				server.registerMBean(obj, objectName);
			}
		} catch(Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to register MBean", e);
		}
	}

	/**
	 * 根据name获取ObjectName
	 * @param name
	 * @return
	 */
	private ObjectName getObjectName(String name){
		if(StringUtils.isEmpty(name)){
			return null;
		}
		String ip=ConfigUtils.getInstance().getOampConfig("IP");
		ObjectName objectName=null;
		try {
			objectName = new ObjectName("QianYu:type=oamp,name=" +ip+name);
		} catch (Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to get ObjectName", e);
		}
		return objectName;
	}

	/**
	 * 远程注册MBean,没有则创建
	 * @param className
	 * @param objectName
	 */
	private void registerRemote(String className,ObjectName objectName){
		if(StringUtils.isEmpty(className)||objectName==null||mbsc==null){
			return ;
		}
		try {
			if(!mbsc.isRegistered(objectName)){
				mbsc.createMBean(className, objectName,null,null);
			}
		} catch(Exception e) {
//			e.printStackTrace();
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to register MBean", e);
		}
	}


	/**
	 * 获取本地MBeanServer
	 * @return
	 */
	private MBeanServer getLocalMBServer(){
		MBeanServer mBeanServer = null;
		if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
			mBeanServer =(MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
		} else {
			mBeanServer = MBeanServerFactory.createMBeanServer();
		}
		return mBeanServer;
	}

	/**
	 * 获取远程MBeanServerConnection
	 * @return
	 */
	private MBeanServerConnection getRemoteMBConn(){
		// provide a valid username and password (e.g., via program arguments)
	    String user = "monitorRole";
	    String pw = "password";
	    // place the username and password in a string array of credentials that
	    // can be used when making the connection to the remote JMX agent
	    String[] credentials = new String[] { user, pw };
	    // the string array of credentials is placed in a map keyed against the
	    // well-defined credentials identifier string
	    Map<String, String[]> props = new HashMap<String, String[]>();
	    props.put("jmx.remote.credentials", credentials);
	    // supply the map of credentials to the connect call
	    JMXServiceURL address;
	    MBeanServerConnection mbs=null;
		try {
			address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi");
			JMXConnector connector = JMXConnectorFactory.connect(address, props);
			mbs= connector.getMBeanServerConnection();
		    connector.connect();
		} catch (Exception e) {
			SrvLogger.error("oamp", ClientMBServerManage.class, "Fail to getRemoteMBConn", e);
		}

	    return mbs;
	}
}

你可能感兴趣的:(apache)