posts - 12,  comments - 2,  trackbacks - 0

    很早以前就接触到了JMX,最近又要用到它。但是启动服务,远程调用的关键代码一时还真想不起来。上网google一圈还是没找到答案。最终还是本人反复调试,才搞定。现贡献源码如下:
    JMX Server实现:

public class ToolsJMXServer {
    
    
public void startUp() {
        
try {
            
//加载配置
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(
                
"com/shrcn/jmx/RegistBeanInfo.properties");
            
if(is.available()>0{
                proCfg.load(is);
            }

            
if(proCfg.contains(KEY_PORT)) {
                RMI_PORT 
= Integer.parseInt(proCfg.getProperty(KEY_PORT));
            }

            
//启动命名服务
            LocateRegistry.createRegistry(RMI_PORT);
            registerMBeans();
            JMXServiceURL url 
= new JMXServiceURL(
                    
"service:jmx:rmi:///jndi/rmi://localhost:" + RMI_PORT + 
                    
"/" + JNDI_NAME);
            JMXConnectorServer cs 
= JMXConnectorServerFactory
                .newJMXConnectorServer(url,     
// url
                                    null,         // environment map
                                    server);     // MBeanServer
            
//启动JMXServer
            cs.start();
        }
 catch (RemoteException e) {
            e.printStackTrace();
        }
 catch (MalformedURLException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }

    
    
private void registerMBeans() {
        
try {
            Iterator
<Entry<Object, Object>> it = proCfg.entrySet().iterator();
            
while(it.hasNext()) {
                Entry
<Object, Object> entry = it.next();
                String beanName 
= (String)entry.getKey();
                
if(KEY_PORT.equals(beanName))
                    
continue;
                String clazz 
= (String)entry.getValue();
                ObjectName on 
= new ObjectName(DOMAIN + ":" + KEY + "=" + beanName);
                Class
<?> cl = Class.forName(clazz);
                Object obj 
= cl.newInstance();
                server.registerMBean(obj, on);
            }

        }
 catch (Exception e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    
}

    JMX客户端实现:

public class MBeanCaller {
    
    
public static Object invokeRemote(String host, int port, ObjectName objectName,
            String methodName, Object[] params, String[] signature)
            
throws RemoteException {
        JMXConnector connector 
= null;
        MBeanServerConnection connection 
= null;
        
try {
            JMXServiceURL url 
= new JMXServiceURL(
                
"service:jmx:rmi:///jndi/rmi://" + host + 
                    
":" + port + "/" + ToolsJMXServer.JNDI_NAME);
            connector 
= JMXConnectorFactory.connect(url);
            connection 
= connector.getMBeanServerConnection();
            
return connection.invoke(objectName, methodName, params, signature);
        }
 catch (Exception e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 finally {
            
try {
                connector.close();
            }
 catch (IOException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        
return null;
    }

    
}


    虽然代码简练,但很实用,希望对各位读者有益。[源码]

posted on 2008-12-23 21:08 远帆 阅读(401) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: