1、簡介java
上一節介紹的是經過Web方式訪問和操控MBean,本節所要介紹的是經過RMI方式,實現客戶端與服務器端的通訊,即客戶端獲取MBeanServerConnection,而後對服務器端被註冊的MBean進行操做。web
2、代碼實例編程
2.1 mserver代碼服務器
package com.muyu.jmx; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import javax.management.remote.JMXAuthenticator; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXPrincipal; import javax.management.remote.JMXServiceURL; import javax.security.auth.Subject; public class MServer { public static void main(String[] args) { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName configName; try { configName = new ObjectName("LuisFigo:name=config"); Config config = new Config(); server.registerMBean(config, configName); //註冊RMI端口號 Registry registry = LocateRegistry.createRegistry(9999); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/mserver"); //權限驗證 Map env = new HashMap(); env.put(JMXConnectorServer.AUTHENTICATOR, new JMXAuthenticator() { public Subject authenticate(Object credentials) { String[] sCredentials = (String[]) credentials; String userName = sCredentials[0]; String password = sCredentials[1]; if ("admin".equals(userName) && "admin".equals(password)) { Set principals = new HashSet(); principals.add(new JMXPrincipal(userName)); return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET); } else throw new SecurityException("Authentication failed! "); } } ); JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, server); cs.start(); System.out.println("rmi start....."); } catch (Exception e) { e.printStackTrace(); } } }
2.2 mclient代碼框架
package com.muyu.jmx; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.management.Attribute; import javax.management.MBeanServerConnection; import javax.management.MBeanServerInvocationHandler; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class MClient { public static void main(String[] args) { try { JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:9999/mserver"); Map env = new HashMap(); env.put(JMXConnector.CREDENTIALS, new String[]{"admin", "admin"}); JMXConnector jmxc = JMXConnectorFactory.connect(url, env); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); Set names = mbsc.queryNames(null, null); for (Iterator i = names.iterator(); i.hasNext();) { System.out.println("\tObjectName = " + (ObjectName) i.next()); } ObjectName stdMBeanName = new ObjectName("LuisFigo:name=config"); mbsc.invoke(stdMBeanName, "printConfigLocation", new String[]{"helloworld"}, new String[]{"java.lang.String"}); mbsc.setAttribute(stdMBeanName, new Attribute("ConfigLocation", "LuisFigo, this is a new configLocation")); ConfigMBean proxy = (ConfigMBean) MBeanServerInvocationHandler .newProxyInstance(mbsc, stdMBeanName, ConfigMBean.class, false); System.out.println(proxy.getConfigLocation()); proxy.setConfigLocation("Beckham"); System.out.println(proxy.getConfigLocation()); proxy.printConfigLocation(); jmxc.close(); } catch (Exception e) { e.printStackTrace(); } } }
說明分佈式
運行MServer,其中LocateRegistry.createRegistry(9999)這段代碼是爲了註冊RMI端口,下面JMXServiceURL會用 到9999這個端口。Env用於控制新鏈接器服務器的行爲的一組屬性,上面的部分代碼是爲了實現權限驗證。若是客戶端不輸入正確的用戶名和密碼,則獲取MServer服務端的鏈接。執行MClient,會發現一些有意思的現象,執行mbsc.invoke(stdMBeanName, "printConfigLocation", new String[]{"helloworld"}, new String[]{"java.lang.String"});這段代碼時,實際上是MServer端的MBean在執行printConfigLocation方法。假如MServer部署在一個JVM上,而MClient部署在另外一個不一樣的JVM上,會獲得一樣的效果。不難看出利用遠程調用方法也能夠輕鬆實現分佈式處理。ui
3、 總結this
使用JMX框架,能夠用SUN自帶的web方式管理MBean,也能夠經過RMI接口本身實現對服務端MBean進行操做。下一節還會詳細介紹Spring怎樣集成MX4J實現JMX編程,MX4J本身實現了一套比較實用的界面來管理MBean。url