posts - 16,comments - 17,trackbacks - 0
              看了兩天文檔,總算對JMX有了一個整體的認識。發現使用mx4j實現JMX還是相當的輕松的。MBeans可以使用mx4j-tools中的 Xdoclet偷一下懶,讓它自動的生成MBeans和Descriptions,ant有相應的支持,還是比較方便的,對于MBean接口的實現,自己寫了。

              對于如何產生和注冊MBeans,mx4j提供了一個相當方便的工具,為什么說相當方便,是因為它真的實在是太方便了。通過寫一個xml配置文件可以完成所有的工作。比起M-LET確實是強了不少。下面就是一個在MBean Server產生注冊一個NamingService、JMXConnectorServer和一個自寫的MBean的配置文件。

          <?xml version="1.0" encoding="UTF-8"?>
          <configuration port="9999">
             
          <startup>
                
          <create classname="mx4j.tools.naming.NamingService" objectname="naming:type=rmiregistry">
                   
          <arg type="int">1099</arg>
                
          </create>
                
          <call operation="start" objectname="naming:type=rmiregistry" />

                
          <object objectid="rmi">
                   
          <call classname="javax.management.remote.JMXConnectorServerFactory" method="newJMXConnectorServer">
                      
          <arg type="javax.management.remote.JMXServiceURL">
                         
          <new classname="javax.management.remote.JMXServiceURL">
                            
          <arg type="string">service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmx</arg>
                         
          </new>
                      
          </arg>
                      
          <arg type="java.util.Map" />
                      
          <arg type="javax.management.MBeanServer" />
                   
          </call>
                
          </object>
                
          <register objectname="connectors:type=rmi,protocol=jrmp">
                   
          <arg type="object" refobjectid="rmi" />
                
          </register>
                
          <call method="start" refobjectid="rmi" />
                
          <create classname="nsmp.examples.mbeans.rmi.MyRemoteServiceObject" objectname="services:type=my-remote" />
             
          </startup>

             
          <shutdown>
                
          <call operation="stop" objectname="services:type=my-remote" />
                
          <call method="stop" refobjectid="rmi" />
                
          <call operation="stop" objectname="naming:type=rmiregistry" />
                
          <unregister objectname="services:type=my-remote"/>
                
          <unregister objectname="connectors:type=rmi,protocol=jrmp" />
                
          <unregister objectname="naming:type=rmiregistry" />
             
          </shutdown>
          </configuration> 

          java代碼:

          package nsmp.agent;

          import java.io.BufferedReader;
          import java.io.FileReader;
          import java.io.Reader;
          import java.net.Socket;

          import javax.management.MBeanServer;
          import javax.management.MBeanServerFactory;
          import javax.management.ObjectName;

          import mx4j.tools.config.ConfigurationLoader;
          import nsmp.util.NsmpGlobals;

          /**
           * @version 1.0
           * @author tower
           *
           * TODO write the comment of this type
           
          */

          public class NsmpServer {
              
          public void startup() throws Exception {
                  MBeanServer server 
          = MBeanServerFactory.newMBeanServer();
                  ConfigurationLoader loader 
          = new ConfigurationLoader();

                  server.registerMBean(loader, ObjectName.getInstance(
          "config:service=loader"));
                  Reader reader 
          = new BufferedReader(new FileReader(NsmpGlobals.NSMP_HOME + "/conf/config.xml"));
                  
                  loader.startup(reader);
                  reader.close();
                  System.
          out.println("Start the nsmp server successfully!");
              }

              
          public void shutdown() throws Exception {
                    String shutdownCommand 
          = "shutdown";
                    Socket socket 
          = new Socket("127.0.0.1"9999);
                    socket.getOutputStream().write(shutdownCommand.getBytes());
                    socket.close();
              }

          }


              startup方法調用配置文件的startup部分完成創建和注冊,shutdown方法調用配置文件的shutdown部分釋放相應的資源。通過調用 startup方法就可以起動MBeanServer提供服務了。對于shutdown開始搞了我半天startup后 ConfigurationLoader都沒有創建一個偵聽端口來接收shutdown命令,看了看mx4j的源碼發現 ConfigurationLoader也沒有發現什么特殊地方。捉摸半天終于發現了自己放了一個愚笨的錯誤,eclipse是用普通用戶權限開的,沒有辦法創建偵聽,改成root后一切ok。

              接下就隨便寫了一個JMXConnector,代碼:

          /*
           * Copyright (C) The MX4J Contributors.
           * All rights reserved.
           *
           * This software is distributed under the terms of the MX4J License version 1.0.
           * See the terms of the MX4J License in the documentation provided with this software.
           
          */


          package nsmp.examples.mbeans.rmi;

          import java.util.Map;

          import javax.management.MBeanInfo;
          import javax.management.MBeanOperationInfo;
          import javax.management.MBeanServerConnection;
          import javax.management.ObjectName;
          import javax.management.remote.JMXConnector;
          import javax.management.remote.JMXConnectorFactory;
          import javax.management.remote.JMXServiceURL;


          /**
           * @version $Revision: 1.3 $
           
          */

          public class Client
          {
             
          public static void main(String[] args) throws Exception
             
          {
                     JMXServiceURL address 
          = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmx");
                     Map creationEnv 
          = null;
                     JMXConnector connector 
          = JMXConnectorFactory.newJMXConnector(address, creationEnv);
                     
                     Map connectionEnv 
          = null;
                     connector.connect(connectionEnv);
                     
                     MBeanServerConnection serverConnection 
          = connector.getMBeanServerConnection();
                     ObjectName name 
          = ObjectName.getInstance("services:type=my-remote");
                     MBeanInfo mbInfo 
          = serverConnection.getMBeanInfo(name);
                     MBeanOperationInfo[] operationInfo 
          = mbInfo.getOperations();
                     
                     
          for (int i = 0; i < operationInfo.length; i++{
                         System.
          out.println(operationInfo[i].getName());
                     }

                     
                     serverConnection.invoke(name, 
          "sayHello"new Object[] {"Tower He"}new String[] {"java.lang.String"});
             }

          }



          JMXConnector是通過獲取一個MBeanServerConnection來實現遠程調用的,運行了一下一切順利通過。

          下載:MX4JExample.rar
          posted on 2005-02-05 19:07 非飛 閱讀(5710) 評論(0)  編輯  收藏 所屬分類: JAVA 相關技術
          主站蜘蛛池模板: 商水县| 安多县| 长寿区| 克拉玛依市| 嘉义县| 三台县| 阳西县| 武宁县| 融水| 仙居县| 英德市| 浠水县| 青神县| 山阳县| 杭锦旗| 莆田市| 山阴县| 凤城市| 彰化县| 英吉沙县| 错那县| 含山县| 山西省| 垫江县| 修武县| 桐庐县| 龙里县| 定南县| 云南省| 玉环县| 崇州市| 通河县| 腾冲县| 侯马市| 崇左市| 托克逊县| 虞城县| 永靖县| 本溪市| 铜鼓县| 林州市|