10,boss中JMX的连接与Mbean的获取

给出一个例程吧
(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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值