很早以前就接触到了JMX,最近又要用到它。但是启动服务,远程调用的关键代码一时还真想不起来。上网google一圈还是没找到答案。最终还是本人反复调试,才搞定。现贡献源码如下:
JMX Server实现:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class ToolsJMXServer
{
![](http://www.blogjava.net/Images/dot.gif)
![](http://www.blogjava.net/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void startUp()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
//加载配置
InputStream is = this.getClass().getClassLoader().getResourceAsStream(
"com/shrcn/jmx/RegistBeanInfo.properties");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(is.available()>0)
{
proCfg.load(is);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RemoteException e)
{
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (MalformedURLException e)
{
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
e.printStackTrace();
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void registerMBeans()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Iterator<Entry<Object, Object>> it = proCfg.entrySet().iterator();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
![](http://www.blogjava.net/Images/dot.gif)
![](http://www.blogjava.net/Images/dot.gif)
}
![](/Images/OutliningIndicators/None.gif)
JMX客户端实现:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class MBeanCaller
{
![](http://www.blogjava.net/Images/dot.gif)
![](http://www.blogjava.net/Images/dot.gif)
public static Object invokeRemote(String host, int port, ObjectName objectName,
String methodName, Object[] params, String[] signature)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws RemoteException
{
JMXConnector connector = null;
MBeanServerConnection connection = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
connector.close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
![](http://www.blogjava.net/Images/dot.gif)
![](http://www.blogjava.net/Images/dot.gif)
}
![](/Images/OutliningIndicators/None.gif)
虽然代码简练,但很实用,希望对各位读者有益。[源码]
posted on 2008-12-23 21:08
远帆 阅读(401)
评论(0) 编辑 收藏 所属分类:
Java