<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hello world

    隨筆 - 2, 文章 - 63, 評論 - 0, 引用 - 0
    數據加載中……

    JMX下RMI遠程調用的入門(環境變量的設置)

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

    源代碼如下:
    ****************************************************************
    第一個源文件 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個源文件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個源文件 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個源文件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個源文件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();
            }
        }
    }

    ************************************************************
    運行前,還有兩件事要做:
    1、先設置6個環境變量:
    變量名                    變量值

    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這個文件夾所在的目錄,如我的設置完為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、運行一個命令。
    這個命令我也不太懂,要在DOS下運行,命令行格式為rmiregistry 9999
    我的運行時命令行整體為
    C:\Documents and Settings\tingfeng>rmiregistry 9999
    回車以后運行,它就會停下來,然后就可以啟動server端了,再啟動Client。


    posted on 2008-03-21 20:46 聽風 閱讀(3692) 評論(0)  編輯  收藏 所屬分類: JAVA

    主站蜘蛛池模板: 大片免费观看92在线视频线视频 | 精品免费国产一区二区| 亚洲美女大bbbbbbbbb| 久久久久久一品道精品免费看| 国产亚洲一区二区手机在线观看| 一个人看的hd免费视频| 精品亚洲视频在线观看| 99re6在线精品免费观看| 精品国产综合成人亚洲区| a级片免费在线播放| 亚洲AV福利天堂一区二区三| 精品无码国产污污污免费网站| 亚洲AV成人无码久久精品老人| 亚洲综合免费视频| 亚洲videos| 免费在线观看黄色毛片| 一区二区3区免费视频| 亚洲精品国产精品乱码不卡√| 人妻丰满熟妇无码区免费| 亚洲成年人电影网站| 四虎www免费人成| 一级一级毛片免费播放| 亚洲av无码不卡一区二区三区| 69国产精品视频免费| 亚洲国产成人无码AV在线| 国产乱辈通伦影片在线播放亚洲| a级毛片100部免费观看| 亚洲明星合成图综合区在线| 日本特黄特黄刺激大片免费| 一级黄色免费大片| 亚洲国色天香视频| 免费人成网站7777视频| 99久久国产免费-99久久国产免费| 亚洲综合中文字幕无线码| 国产L精品国产亚洲区久久| 人妻丰满熟妇无码区免费| 含羞草国产亚洲精品岁国产精品| 久久亚洲高清观看| 免费看的成人yellow视频| a毛片在线免费观看| 亚洲色大成网站www|