jboss中JMX的连接与Mbean的获取

时间:2021-09-07 14:58:02
文/陈刚 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( null null );  //
         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);