转载

jmx

jmx是java程序之间互为通信,rpc调用风格.应用场景:集群环境内快速度部署java 程序,可发布分析数据程序

package testplus2.game;  import java.util.Set;  import javax.management.ObjectName; /**  * jmx 服务接口 服务端与客户端  *   * @author solq  * */ public interface IJmxService {  public void close();   // /////////服务端部分//////////////    /**   * 注册MBEAN   * */  public void register(Object mbean);   /**   * 启动jxm服务端   * */  public void start();   // /////////客户端部分//////////////  /**   * 连接jxm服务端   * */  public void connect();   /**   * 获取mbean proxy   * @param clz 注意是接口   * */  public <T> T getRpcProxy(Class<T> clz);  /**   * 获取mbean proxy   * @param clz 注意是接口   * @param impClz 实现类   * */  public <T> T getRpcProxy(Class<T> clz, Class<? extends T> impClz);  /**   * 获取jxm服务端所有注册mbean    * */  public Set<ObjectName> getAllRegisterMBean(); } 
package testplus2.game;  import java.io.IOException; import java.rmi.registry.LocateRegistry; import java.util.Set;  import javax.management.JMX; import javax.management.MBeanServer; import javax.management.MBeanServerConnection; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL;  import com.sun.jdmk.comm.HtmlAdaptorServer;  /**  * jmx 服务实现  *   * @author solq  * */ public class JmxService implements IJmxService {   private boolean startServer;   private String host;  private int port;   // server  private MBeanServer mbeanServer;  private JMXConnectorServer jmxConnectorServer;   // client  private JMXConnector jxmConnector;  private MBeanServerConnection beanServerConnection;   public JmxService(String host, int port) {   this.host = host;   this.port = port;  }   @Override  public void register(Object obj) {   initServer();   try {    ObjectName connectorName = new ObjectName(JxmUtli.getObjectName(obj      .getClass()));    mbeanServer.registerMBean(obj, connectorName);   } catch (Exception e) {    e.printStackTrace();   }  }   private void initServer() {   if (mbeanServer == null) {    mbeanServer = MBeanServerFactory.createMBeanServer();   }  }   @Override  public void start() {   if (startServer) {    System.out.println("已启动JMX :" + port);    return;   }   this.startServer = true;   initServer();   try {     // 启动server    LocateRegistry.createRegistry(port);    final JMXServiceURL url = new JMXServiceURL(      "service:jmx:rmi://localhost/jndi/rmi://" + host + ":"        + port + "/jmxrmi");    jmxConnectorServer = JMXConnectorServerFactory      .newJMXConnectorServer(url, null, mbeanServer);    jmxConnectorServer.start();     // //创建适配器,用于能够通过浏览器访问MBean     HtmlAdaptorServer adapter = new HtmlAdaptorServer();     adapter.setPort(9797);     register(adapter);     adapter.start();   } catch (Exception e) {    e.printStackTrace();   }   }   @Override  public void connect() {   try {    JMXServiceURL jmxServiceUrl = new JMXServiceURL(      "service:jmx:rmi://localhost/jndi/rmi://" + host + ":"        + port + "/jmxrmi");    JMXConnector connector = JMXConnectorFactory.connect(jmxServiceUrl);    beanServerConnection = connector.getMBeanServerConnection();   } catch (Exception e) {    e.printStackTrace();   }  }   @Override  public <T> T getRpcProxy(Class<T> clz) {   return getRpcProxy(clz, clz);  }   @Override  public <T> T getRpcProxy(Class<T> clz, Class<? extends T> impClz) {   try {    ObjectName hwName = new ObjectName(JxmUtli.getObjectName(impClz));    return JMX.newMXBeanProxy(beanServerConnection, hwName, clz);   } catch (Exception e) {    e.printStackTrace();   }   return null;  }   @Override  public void close() {   if (jmxConnectorServer != null) {    try {     jmxConnectorServer.stop();    } catch (IOException e) {     e.printStackTrace();    }   }    if (jxmConnector != null) {    try {     jxmConnector.close();    } catch (IOException e) {     e.printStackTrace();    }   }  }   @Override  public Set<ObjectName> getAllRegisterMBean() {   try {    ObjectName filterName = new ObjectName("*:*");    Set<ObjectName> objectNames = beanServerConnection.queryNames(      filterName, null);    return objectNames;   } catch (Exception e) {    e.printStackTrace();   }   return null;  }   // getter  public int getPort() {   return port;  }   public String getHost() {   return host;  }   public boolean isStartServer() {   return startServer;  }   public MBeanServer getMbeanServer() {   return mbeanServer;  }   public JMXConnectorServer getJmxConnectorServer() {   return jmxConnectorServer;  }   public JMXConnector getJxmConnector() {   return jxmConnector;  }   public MBeanServerConnection getBeanServerConnection() {   return beanServerConnection;  }  } 

package testplus2.game;

public abstract class JxmUtli {

public static String getObjectName(Class<?> clz) {  String name = clz.getName() + ":type=" + clz.getSimpleName();

return name;

}

}

package testplus2.jmx;  import java.io.IOException; import java.io.ObjectInputStream.GetField; import java.rmi.registry.LocateRegistry;  import javax.management.InstanceAlreadyExistsException; 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.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL;  import testplus2.game.GamePlus; import testplus2.game.GamePlusMBean; import testplus2.game.JmxService; import testplus2.game.JxmUtli;  public class TestJmxServer {  private static int port = 5555;   public static void main(String[] args) {   test();  }   static void test(){   JmxService jmxService = new JmxService("localhost",port);   //注册mbean 实例   GamePlus gamePlus = new GamePlus();     jmxService.register(gamePlus);   jmxService.start();   } } 
package testplus2.jmx;  import java.util.Set;  import javax.management.MBeanServerConnection; import javax.management.ObjectName;  import testplus2.game.GamePlus; import testplus2.game.GamePlusMBean; import testplus2.game.JmxService;  public class JmxClient {   private static int port = 5555;   public static void main(String args[]) throws Exception {   test0();  }   private static void test0() throws Exception {    JmxService jmxService = new JmxService("localhost",port);   jmxService.connect();   GamePlusMBean proxy = jmxService.getRpcProxy(GamePlusMBean.class,     GamePlus.class);   System.out.println(proxy.getAllConsoleMessage("abc"));    Set<?> objectNames = jmxService.getAllRegisterMBean();    for (Object name : objectNames) {    ObjectName objName = (ObjectName) name;    System.out      .println("    Object Name: " + objName.getCanonicalName());   }       jmxService.close();  }  }
正文到此结束
Loading...