JMX RMI连接器使用

  1. package com.fanshadoop.rmi;
  2. import javax.management.InstanceAlreadyExistsException;
  3. import javax.management.MBeanRegistrationException;
  4. import javax.management.MBeanServer;
  5. import javax.management.MBeanServerFactory;
  6. import javax.management.MalformedObjectNameException;
  7. import javax.management.NotCompliantMBeanException;
  8. import javax.management.ObjectName;
  9. import javax.management.remote.JMXConnectorServer;
  10. import javax.management.remote.JMXConnectorServerFactory;
  11. import javax.management.remote.JMXServiceURL;
  12. import com.fanshadoop.notification.HelloWorld;
  13. import com.sun.jdmk.comm.HtmlAdaptorServer;
  14. /**
  15.  * JMX RMI连接器使用
  16.  */
  17. public class JMXBookAgent {
  18.     private MBeanServer server = null;
  19.     /**
  20.      * 构建MBeanServer
  21.      */
  22.     public JMXBookAgent(){
  23.         System.out.println(“\n\tCREATE the MBeanServer.”);
  24.         server = MBeanServerFactory.createMBeanServer(“JMXBookAgent”);
  25.     }
  26.     /**
  27.      * 1)注册MBean
  28.      * 2)注册HTMLAdapter
  29.      * 3)注册RMIConnection
  30.      * 注意:HTMLAdapter和RMIConnection都是MBean的实现
  31.      */
  32.     public void start() throws MalformedObjectNameException, NullPointerException,
[java][/java] view plaincopyprint?

  1. InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
  2.         ObjectName mxName = new ObjectName(“com.fansjmx:name=helloworld”);
  3.         server.registerMBean(new HelloWorld(), mxName);
  4.         startHTMLAdapter();
  5.         startRMIConnector();
  6.     }
  7.     protected void startRMIConnector() {
  8.         ObjectName connectorName = null;
  9.         try {
  10.             JMXServiceURL url = new JMXServiceURL(“service:jmx:rmi:///jndi/rmi://localhost:9999/server”);
  11.             JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
  12.             connectorName = new ObjectName(“JMXBookAgent:name=RMIConnector”);
  13.             server.registerMBean(cs, connectorName);
  14.             cs.start();
  15.         } catch (Exception e) {
  16.         }
  17.     }
  18.     protected void startHTMLAdapter() {
  19.         HtmlAdaptorServer adapter = new HtmlAdaptorServer();
  20.         ObjectName adapterName = null;
  21.         try {
  22.             adapter.setPort(9092);
  23.             // create the HTML adapter
  24.             adapterName = new ObjectName(“JMXBookAgent:name=html,port=9092”);
  25.             server.registerMBean(adapter, adapterName);
  26.             adapter.start();
  27.         } catch (Exception e) {
  28.             System.out.println(“Error Starting HTML Adapter for Agent”);
  29.         }
  30.     }
  31.     public static void main(String[] args) {
  32.         System.out.println(“\n>>> START of JMXBook Agent”);
  33.         System.out.println(“\n>>> CREATE the agent…”);
  34.         try {
  35.             JMXBookAgent agent = new JMXBookAgent();
  36.             System.out.println(“\nAgent is Ready for Service…\n”);
  37.             agent.start();
  38.         } catch (MalformedObjectNameException e) {
  39.             e.printStackTrace();
  40.         } catch (InstanceAlreadyExistsException e) {
  41.             e.printStackTrace();
  42.         } catch (MBeanRegistrationException e) {
  43.             e.printStackTrace();
  44.         } catch (NotCompliantMBeanException e) {
  45.             e.printStackTrace();
  46.         } catch (NullPointerException e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50. }

 

[java][/java] view plaincopyprint?

  1. package com.fanshadoop.rmi;
  2. import java.io.IOException;
  3. import javax.management.InstanceNotFoundException;
  4. import javax.management.MBeanException;
  5. import javax.management.MalformedObjectNameException;
  6. import javax.management.ObjectName;
  7. import javax.management.ReflectionException;
  8. import javax.management.remote.JMXConnector;
  9. import javax.management.remote.JMXConnectorFactory;
  10. import javax.management.remote.JMXServiceURL;
  11. public class RMIClientFactory {
  12.     public static JMXConnector getClient() throws IOException {
  13.         JMXServiceURL url = new JMXServiceURL(
  14.                 “service:jmx:rmi:///jndi/rmi://localhost:9999/server”);
  15.         JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
  16.         return jmxc;
  17.     }
  18.     public static void main(String[] args) {
  19.         try {
  20.             JMXConnector jmxc = RMIClientFactory.getClient();
  21.             ObjectName mxName;
  22.             mxName = new ObjectName(“com.fansjmx:name=helloworld”);
  23.             jmxc.getMBeanServerConnection().invoke(mxName, “printGreeting”,
  24.                     null, null);
  25.         } catch (MalformedObjectNameException e) {
  26.             e.printStackTrace();
  27.         } catch (NullPointerException e) {
  28.             e.printStackTrace();
  29.         } catch (InstanceNotFoundException e) {
  30.             e.printStackTrace();
  31.         } catch (MBeanException e) {
  32.             e.printStackTrace();
  33.         } catch (ReflectionException e) {
  34.             e.printStackTrace();
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39. }

在运行时需要运行:rmiregistry 9999

标签