子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數據加載中……

          jboss中JMX的連接與Mbean的獲取

          文/陳剛 from www.chengang.com.cn

          給出一個例程吧
          (1)它需要Jboss的jbossall-client.jar包的支持
          (2)運行環境為Jboss3.2.6 + jdk5.0 + eclipse3.2M2。jdk5.0內置了JMX支持,如果你不是jdk5.0的話eclipse可能會報有會類找不到的錯誤
          (3)之前必須選啟動Jboss

          import java.util.Iterator;
          import java.util.Properties;
          import java.util.Set;

          import javax.management.MBeanInfo;
          import javax.management.MBeanOperationInfo;
          import javax.management.MBeanParameterInfo;
          import javax.management.ObjectInstance;
          import javax.management.ObjectName;
          import javax.naming.InitialContext;

          import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

          public class TestJMX {

              
          public static void main(String[] args) throws Exception {
                  
          //Get RMIAdaptor Object
                  Properties pro = new Properties();
                  pro.setProperty(
          "java.naming.factory.initial""org.jnp.interfaces.NamingContextFactory");
                  pro.setProperty(
          "java.naming.provider.url""jnp://localhost:1099");
                  pro.setProperty(
          "java.naming.factory.url.pkgs""org.jboss.naming:org.jnp.interfaces");
                  InitialContext ic 
          = new InitialContext(pro);
                  RMIAdaptor server 
          = (RMIAdaptor) ic.lookup("jmx/rmi/RMIAdaptor");

                  
          // Get the MBeanInfo for the JNDIView MBean
                  ObjectName name = new ObjectName("jboss:service=JNDIView");
                  MBeanInfo info 
          = server.getMBeanInfo(name);
                  System.out.println(
          "JNDIView Class: " + info.getClassName());
                  MBeanOperationInfo[] opInfo 
          = info.getOperations();
                  System.out.println(
          "JNDIView Operations: ");
                  
          for (int o = 0; o < opInfo.length; o++) {
                      MBeanOperationInfo op 
          = opInfo[o];
                      String returnType 
          = op.getReturnType();
                      String opName 
          = op.getName();
                      System.out.print(
          " + " + returnType + " " + opName + "(");
                      MBeanParameterInfo[] params 
          = op.getSignature();
                      
          for (int p = 0; p < params.length; p++) {
                          MBeanParameterInfo paramInfo 
          = params[p];
                          String pname 
          = paramInfo.getName();
                          String type 
          = paramInfo.getType();
                          
          if (pname.equals(type))
                              System.out.print(type);
                          
          else
                              System.out.print(type 
          + " " + name);
                          
          if (p < params.length - 1)
                              System.out.print(
          &apos;,&apos;);
                      }
                      System.out.println(
          ")");
                  }

                  
          //Get all MBeans
                  Set mbSet = server.queryMBeans(nullnull); //
                  for (Iterator it = mbSet.iterator(); it.hasNext();) {
                      ObjectInstance oi 
          = (ObjectInstance) it.next();
                      System.out.println(oi.getObjectName());
                  }
              }

          這里有一段網上找到的代碼,正好是實現了配置文件修改后重讀的功能.

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.net.URL;
          import java.util.Properties;

          /**
          * @author Robbin Fan
          */
          public class ConfigUtil {

          private static Properties props = null;
          private static File configFile = null;
          private static long fileLastModified = 0L;

          private static void init() {
          URL url = ConfigUtil.class.getClassLoader().getResource("global.properties");
          configFile = new File(url.getFile());
          fileLastModified = configFile.lastModified();
          props = new Properties();
          load();
          }

          private static void load() {
          try {
          props.load(new FileInputStream(configFile));
          fileLastModified = configFile.lastModified();
          } catch (IOException e) {
          throw new RuntimeException(e);
          }
          }

          public static String getConfig(String key) {
          if ((configFile == null) || (props == null))
          init();
          if (configFile.lastModified() > fileLastModified)
          load();
          return props.getProperty(key);
          }

          }

          如果MBean就在本地,則可以用下面的方法來獲得:

          ThreadPoolMBean poolMBean = (ThreadPoolMBean) MBeanProxyExt.create(ThreadPoolMBean.class, objName);

          還有一種遠程獲得的方法(從開源代碼中看到的,未驗證)

          ThreadPoolMBean poolMBean = (ThreadPoolMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, objName, ThreadPoolMBean.class, false);

          posted on 2006-03-07 14:54 陳剛 閱讀(5324) 評論(2)  編輯  收藏 所屬分類: JMX

          評論

          # re: jboss中JMX的連接與Mbean的獲取  回復  更多評論   

          有在tomcat下的例子嗎?

          還有,您這個博客上的網站怎么都無法顯示呢?郁悶
          2006-11-01 22:58 | 過客[匿名]

          # re: jboss中JMX的連接與Mbean的獲取  回復  更多評論   

          fasfds
          2007-03-15 13:59 | aaa
          主站蜘蛛池模板: 清流县| 凤山市| 郓城县| 瑞昌市| 崇文区| 罗源县| 全南县| 永城市| 广安市| 大方县| 贵阳市| 庄河市| 邹平县| 兖州市| 福鼎市| 曲阳县| 罗甸县| 邵阳县| 利津县| 彰化县| 广安市| 宁安市| 昆明市| 阜宁县| 个旧市| 永康市| 黎平县| 阿巴嘎旗| 乐山市| 扎兰屯市| 信宜市| 阿克陶县| 东阿县| 探索| 闸北区| 新郑市| 邵阳县| 泸溪县| 华容县| 沁阳市| 郓城县|