向左走,向右走。。。
          永遠(yuǎn)不回頭
          posts - 16,comments - 17,trackbacks - 0
              看了兩天文檔,總算對(duì)JMX有了一個(gè)整體的認(rèn)識(shí)。發(fā)現(xiàn)使用mx4j實(shí)現(xiàn)JMX還是相當(dāng)?shù)妮p松的。MBeans可以使用mx4j-tools中的 Xdoclet偷一下懶,讓它自動(dòng)的生成MBeans和Descriptions,ant有相應(yīng)的支持,還是比較方便的,對(duì)于MBean接口的實(shí)現(xiàn),自己寫了。

              對(duì)于如何產(chǎn)生和注冊(cè)MBeans,mx4j提供了一個(gè)相當(dāng)方便的工具,為什么說(shuō)相當(dāng)方便,是因?yàn)樗娴膶?shí)在是太方便了。通過(guò)寫一個(gè)xml配置文件可以完成所有的工作。比起M-LET確實(shí)是強(qiáng)了不少。下面就是一個(gè)在MBean Server產(chǎn)生注冊(cè)一個(gè)NamingService、JMXConnectorServer和一個(gè)自寫的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方法調(diào)用配置文件的startup部分完成創(chuàng)建和注冊(cè),shutdown方法調(diào)用配置文件的shutdown部分釋放相應(yīng)的資源。通過(guò)調(diào)用 startup方法就可以起動(dòng)MBeanServer提供服務(wù)了。對(duì)于shutdown開(kāi)始搞了我半天startup后 ConfigurationLoader都沒(méi)有創(chuàng)建一個(gè)偵聽(tīng)端口來(lái)接收shutdown命令,看了看mx4j的源碼發(fā)現(xiàn) ConfigurationLoader也沒(méi)有發(fā)現(xiàn)什么特殊地方。捉摸半天終于發(fā)現(xiàn)了自己放了一個(gè)愚笨的錯(cuò)誤,eclipse是用普通用戶權(quán)限開(kāi)的,沒(méi)有辦法創(chuàng)建偵聽(tīng),改成root后一切ok。

              接下就隨便寫了一個(gè)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是通過(guò)獲取一個(gè)MBeanServerConnection來(lái)實(shí)現(xiàn)遠(yuǎn)程調(diào)用的,運(yùn)行了一下一切順利通過(guò)。

          下載:MX4JExample.rar
          posted on 2005-02-05 19:07 非飛 閱讀(5709) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA 相關(guān)技術(shù)
          主站蜘蛛池模板: 玉环县| 浦东新区| 黄平县| 南投县| 江油市| 德庆县| 洛隆县| 隆回县| 托克逊县| 洪湖市| 房山区| 肥西县| 阿拉善左旗| 交口县| 富锦市| 磐石市| 息烽县| 双峰县| 惠来县| 桃源县| 攀枝花市| 巴里| 康定县| 大方县| 文登市| 牙克石市| 石首市| 英超| 西峡县| 瑞昌市| 淮南市| 资兴市| 宁蒗| 通河县| 虎林市| 修文县| 且末县| 阜宁县| 宣威市| 汉中市| 黔东|