子在川上曰

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

          jboss中JMX的連接與Mbean的獲取

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

          給出一個例程吧
          (1)它需要Jboss的jbossall-client.jar包的支持
          (2)運行環(huán)境為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());
                  }
              }

          這里有一段網(wǎng)上找到的代碼,正好是實現(xiàn)了配置文件修改后重讀的功能.

          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 陳剛 閱讀(5323) 評論(2)  編輯  收藏 所屬分類: JMX

          評論

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

          有在tomcat下的例子嗎?

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

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

          fasfds
          2007-03-15 13:59 | aaa
          主站蜘蛛池模板: 陇西县| 简阳市| 桑日县| 宁都县| 陇西县| 乐平市| 荆门市| 织金县| 福建省| 花垣县| 邮箱| 葫芦岛市| 雷山县| 广宁县| 屯门区| 夏津县| 弥渡县| 丘北县| 搜索| 澄城县| 孟津县| 黄石市| 绥化市| 大安市| 本溪市| 芷江| 中宁县| 宜城市| 泾源县| 丘北县| 江北区| 通化市| 白水县| 泰州市| 佛教| 崇仁县| 齐河县| 九寨沟县| 侯马市| 财经| 保德县|