hello world

          隨筆 - 2, 文章 - 63, 評(píng)論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          JMX下RMI遠(yuǎn)程調(diào)用的入門(環(huán)境變量的設(shè)置)

          RMI是用來坐遠(yuǎn)程管理的,如可以是在C/S模式下負(fù)責(zé)客戶端和服務(wù)器端的通信。
          參照sun提供的jmxremote-1_0_1-bin文檔,在\examples\Basic\rmi里我們可以找到5個(gè).JAVA文件
          Server.javaSimpleStandardMBean.javaSimpleStandard.javaClientListener.javaClient.java
          除了運(yùn)行上邊5個(gè)文件以外,為了客戶端和服務(wù)器端能夠連上,還要設(shè)置環(huán)境變量,以及在DOS下運(yùn)行一個(gè)注冊(cè)端口的命令,后面再加以詳細(xì)說明。

          源代碼如下:
          ****************************************************************
          第一個(gè)源文件 Server.java
          **************************************
          import javax.management.MBeanServer;
          import javax.management.MBeanServerFactory;
          import javax.management.remote.JMXConnectorServer;
          import javax.management.remote.JMXConnectorServerFactory;
          import javax.management.remote.JMXServiceURL;

          public class Server {

              public static void main(String[] args) {
                  try {
                      // Instantiate the MBean server
                      //
                      System.out.println("\nCreate the MBean server");
                      MBeanServer mbs = MBeanServerFactory.createMBeanServer();

                      // Create an RMI connector server
                      //
                      System.out.println("\nCreate an RMI connector server");
                      JMXServiceURL url = new JMXServiceURL(
                  "service:jmx:rmi:///jndi/rmi://219.242.119.89:9999/server");
                      JMXConnectorServer cs =
                          JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);

                      // Start the RMI connector server
                      //
                      System.out.println("\nStart the RMI connector server");
                      cs.start();
                      System.out.println("\nRMI connector server successfully started");
                      System.out.println("\nWaiting for incoming connections...");
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }

          ************************************************************************
          第2個(gè)源文件SimpleStandardMBean.java
          *******************************

          import javax.management.AttributeChangeNotification;
          import javax.management.NotificationBroadcasterSupport;

          public class SimpleStandard
              extends NotificationBroadcasterSupport
              implements SimpleStandardMBean {

              /*
               * -----------------------------------------------------
               * CONSTRUCTORS
               * -----------------------------------------------------
               */

              /* "SimpleStandard" does not provide any specific constructors.
               * However, "SimpleStandard" is JMX compliant with regards to
               * contructors because the default contructor SimpleStandard()
               * provided by the Java compiler is public.
               */

              /*
               * -----------------------------------------------------
               * IMPLEMENTATION OF THE SimpleStandardMBean INTERFACE
               * -----------------------------------------------------
               */

              /**
               * Getter: get the "State" attribute of the "SimpleStandard" standard MBean.
               *
               * @return the current value of the "State" attribute.
               */
              public String getState() {
                  return state;
              }

              /**
               * Setter: set the "State" attribute of the "SimpleStandard" standard MBean.
               *
               * @param <VAR>s</VAR> the new value of the "State" attribute.
               */
              public void setState(String s) {
                  state = s;
                  nbChanges++;
              }

              /**
               * Getter: get the "NbChanges" attribute of the "SimpleStandard" standard
               * MBean.
               *
               * @return the current value of the "NbChanges" attribute.
               */
              public int getNbChanges() {
                  return nbChanges;
              }

              /**
               * Operation: reset to their initial values the "State" and "NbChanges"
               * attributes of the "SimpleStandard" standard MBean.
               */
              public void reset() {
           AttributeChangeNotification acn =
               new AttributeChangeNotification(this,
                   0,
                   0,
                   "NbChanges reset",
                   "NbChanges",
                   "Integer",
                   new Integer(nbChanges),
                   new Integer(0));
           state = "initial state";
                  nbChanges = 0;
           nbResets++;
           sendNotification(acn);
              }

              /*
               * -----------------------------------------------------
               * METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT
               * -----------------------------------------------------
               */

              /**
               * Return the "NbResets" property.
               * This method is not a Getter in the JMX sense because it
               * is not exposed in the "SimpleStandardMBean" interface.
               *
               * @return the current value of the "NbResets" property.
               */
              public int getNbResets() {
           return nbResets;
              }

              /*
               * -----------------------------------------------------
               * ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
               * -----------------------------------------------------
               */

              private String state = "initial state";
              private int nbChanges = 0;

              /*
               * -----------------------------------------------------
               * PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
               * -----------------------------------------------------
               */

              private int nbResets = 0;
          }



          ************************************************************************
          第3個(gè)源文件 SimpleStandard.java
          *******************************

          /**
           * This is the management interface explicitly defined for the
           * "SimpleStandard" standard MBean.
           *
           * The "SimpleStandard" standard MBean implements this interface
           * in order to be manageable through a JMX agent.
           *
           * The "SimpleStandardMBean" interface shows how to expose for management:
           * - a read/write attribute (named "State") through its getter and setter
           *   methods,
           * - a read-only attribute (named "NbChanges") through its getter method,
           * - an operation (named "reset").
           */
          public interface SimpleStandardMBean {

              /**
               * Getter: set the "State" attribute of the "SimpleStandard" standard
               * MBean.
               *
               * @return the current value of the "State" attribute.
               */
              public String getState();

              /**
               * Setter: set the "State" attribute of the "SimpleStandard" standard
               * MBean.
               *
               * @param <VAR>s</VAR> the new value of the "State" attribute.
               */
              public void setState(String s);

              /**
               * Getter: get the "NbChanges" attribute of the "SimpleStandard" standard
               * MBean.
               *
               * @return the current value of the "NbChanges" attribute.
               */
              public int getNbChanges();

              /**
               * Operation: reset to their initial values the "State" and "NbChanges"
               * attributes of the "SimpleStandard" standard MBean.
               */
              public void reset();
          }


          ************************************************************************
          第4個(gè)源文件ClientListener.java
          *******************************

          import javax.management.Notification;
          import javax.management.NotificationListener;

          public class ClientListener implements NotificationListener {
              public void handleNotification(Notification notification, Object handback) {
           System.out.println("\nReceived notification: " + notification);
              }
          }
          *************************************************************************
          第5個(gè)源文件Client.java
          ********************************

          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 Client {

              public static void main(String[] args) {
                  try {
                      // Create an RMI connector client and
                      // connect it to the RMI connector server
                      //
                      System.out.println("\nCreate an RMI connector client and " +
                                         "connect it to the RMI connector server");
                      JMXServiceURL url = new JMXServiceURL(
                  "service:jmx:rmi:///jndi/rmi://219.242.119.89:9999/server");
                      JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

                      // Get an MBeanServerConnection
                      //
                      System.out.println("\nGet an MBeanServerConnection");
                      MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

                      // Get domains from MBeanServer
                      //
                      System.out.println("\nDomains:");
                      String domains[] = mbsc.getDomains();
                      for (int i = 0; i < domains.length; i++) {
                          System.out.println("\tDomain[" + i + "] = " + domains[i]);
                      }

                      // Create SimpleStandard MBean
                      //
                      ObjectName mbeanName = new ObjectName("MBeans:type=SimpleStandard");
                      System.out.println("\nCreate SimpleStandard MBean...");
                      mbsc.createMBean("SimpleStandard", mbeanName, null, null);

                      // Get MBean count
                      //
                      System.out.println("\nMBean count = " + mbsc.getMBeanCount());

                      // Get State attribute
                      //
                      System.out.println("\nState = " +
                                         mbsc.getAttribute(mbeanName, "State"));

                      // Set State attribute
                      //
                      mbsc.setAttribute(mbeanName,
                                        new Attribute("State", "changed state"));

                      // Get State attribute
               //
               // Another way of interacting with a given MBean is through a
               // dedicated proxy instead of going directly through the MBean
               // server connection
               //
               SimpleStandardMBean proxy = (SimpleStandardMBean)
            MBeanServerInvocationHandler.newProxyInstance(
                    mbsc,
                    mbeanName,
                    SimpleStandardMBean.class,
                    false);
                      System.out.println("\nState = " + proxy.getState());

                      // Add notification listener on SimpleStandard MBean
                      //
                      ClientListener listener = new ClientListener();
                      System.out.println("\nAdd notification listener...");
                      mbsc.addNotificationListener(mbeanName, listener, null, null);

                      // Invoke "reset" in SimpleStandard MBean
                      //
                      // Calling "reset" makes the SimpleStandard MBean emit a
                      // notification that will be received by the registered
                      // ClientListener.
                      //
                      System.out.println("\nInvoke reset() in SimpleStandard MBean...");
                      mbsc.invoke(mbeanName, "reset", null, null);

                      // Sleep for 2 seconds in order to have time to receive the
                      // notification before removing the notification listener.
                      //
                      System.out.println("\nWaiting for notification...");
                      Thread.sleep(2000);

                      // Remove notification listener on SimpleStandard MBean
                      //
                      System.out.println("\nRemove notification listener...");
                      mbsc.removeNotificationListener(mbeanName, listener);

                      // Unregister SimpleStandard MBean
                      //
                      System.out.println("\nUnregister SimpleStandard MBean...");
                      mbsc.unregisterMBean(mbeanName);

                      // Close MBeanServer connection
                      //
                      System.out.println("\nClose the connection to the server");
                      jmxc.close();
                      System.out.println("\nBye! Bye!");
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }

          ************************************************************
          運(yùn)行前,還有兩件事要做:
          1、先設(shè)置6個(gè)環(huán)境變量:
          變量名                    變量值

          RJMX_HOME      user_dir/jmxremote-1_0_1-bin

          JMX_HOME        jmx_install_dir

          RJMXLIB            {RJMX_HOME}/lib

          JMXLIB                {JMX_HOME}/lib
          classp                    ${RJMXLIB}/jmxremote.jar:${JMXLIB}/jmxri.jar
          CLASSPATH        .:$classp
          其中user_dir為jmxremote-1_0_1-bin這個(gè)文件夾所在的目錄,如我的設(shè)置完為C:\Program Files\Java\otherlib\jmxremote-1_0_1-bin;
          jmx_install_dir 為JMX包的文件jmx-1_2_1-bin文件夾所在的目錄,我的為C:\Program Files\Java\otherlib\jmx-1_2_1-bin
          2、運(yùn)行一個(gè)命令。
          這個(gè)命令我也不太懂,要在DOS下運(yùn)行,命令行格式為rmiregistry 9999
          我的運(yùn)行時(shí)命令行整體為
          C:\Documents and Settings\tingfeng>rmiregistry 9999
          回車以后運(yùn)行,它就會(huì)停下來,然后就可以啟動(dòng)server端了,再啟動(dòng)Client。


          posted on 2008-03-21 20:46 聽風(fēng) 閱讀(3697) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA

          主站蜘蛛池模板: 明星| 云安县| 潼南县| 宁海县| 临澧县| 沂水县| 温宿县| 额尔古纳市| 奎屯市| 抚松县| 连云港市| 沅陵县| 晋宁县| 布拖县| 廉江市| 公安县| 德兴市| 木兰县| 丰原市| 华池县| 大邑县| 昭觉县| 金山区| 白沙| 岑巩县| 开化县| 甘泉县| 射阳县| 福贡县| 延寿县| 茂名市| 安康市| 晋江市| 松潘县| 台北市| 乌拉特中旗| 驻马店市| 刚察县| 永清县| 大石桥市| 建阳市|